Skip to content

Instantly share code, notes, and snippets.

@buddyeorl
Created October 8, 2020 22:40
Show Gist options
  • Save buddyeorl/916a02ae66d5aa11cfe78bea11e9c139 to your computer and use it in GitHub Desktop.
Save buddyeorl/916a02ae66d5aa11cfe78bea11e9c139 to your computer and use it in GitHub Desktop.
// importing zipCode data file
let zipCodes = require('./allZipCodesFilesWithCityNamesShort').allZipCodesFilesWithCityNamesShort;
// importing calculate distance functions
const calculateDistance = require('./calculateDistance').calculateDistance;
module.exports.getRadius = (zipCode, limit, unit = 'M') => {
let results = [];
//check if received Zip Code exist in the zipCodes object
if (!zipCodes[zipCode]) {
return { error: `Zip Code: ${zipCode} not found` }
}
//iterate through zipcodes to calculate distance
for (data in zipCodes) {
//skip if same as received zicode
if (data === zipCode) {
continue;
}
let distance = calculateDistance(zipCode, data, unit);
// only add zipcodes within the radius given
if (distance <= limit) {
results.push({
zipcode: data,
distance: distance
});
}
}
//sort zipcodes, closest firsts
if (results.length > 0) {
results.sort((a, b) => a.distance - b.distance);
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment