Created
October 8, 2020 22:40
-
-
Save buddyeorl/916a02ae66d5aa11cfe78bea11e9c139 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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