Skip to content

Instantly share code, notes, and snippets.

@marinalohova
Last active December 6, 2018 20:07
Show Gist options
  • Save marinalohova/0bf05af9b20f7b585c1758c6386227ed to your computer and use it in GitHub Desktop.
Save marinalohova/0bf05af9b20f7b585c1758c6386227ed to your computer and use it in GitHub Desktop.
//precalc all distances to major research cities after import and store to respondents database/elastic
function precalcDistance(respondents, majorCities) {
return respondents.map((respondent) => {
let distances = majorCities.reduce((memo, {location}) => {
let distance = haversine(location.location, respondent);
memo[location.city] = distance;
return memo;
}, {});
return { ...respondent, distances };
})
}
//assuming respondents have the precalculated 'distances' property, we can run search over and over without unnecessary recalulcating.
function byDistance(respondents, researchCities, range) {
return respondents
.filter(respondent => researchCities.some(city => range > respondent.distances[city.location.city]))
.sort((a, b) => a.firstName.toLowerCase() > b.firstName.toLowerCase());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment