-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
50 lines (46 loc) · 1.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var zipStateCounty = require("./zip_state_county.json");
var stateToCounties = {};
var zipToCounties = {};
zipStateCounty.zip_state_county.forEach(function(list) {
var zip = list[0];
var state = list[1];
var county = list[2];
stateToCounties[state] = stateToCounties[state] || {};
stateToCounties[state][county] = stateToCounties[state][county] || [];
stateToCounties[state][county].push(zip);
zipToCounties[zip] = {state: state, county: county}
});
// free this reference so it can be GC'ed.
zipStateCounty = null;
module.exports = {
getStates: function() {
return Object.keys(stateToCounties);
},
getCountiesByState: function(state) {
return Object.keys(stateToCounties[state]);
},
getCountyByZip: function(zip) {
return zipToCounties[zip];
},
find: function(obj) {
if (obj.state && obj.county && obj.zip) {
var zips = (stateToCounties[obj.state] || {})[obj.county] || [];
for (var i = 0; i < zips.length; i++) {
if (zips[i] === obj.zip) {
return true;
}
}
return false;
}
if (obj.state && obj.county) {
return (stateToCounties[obj.state] || {})[obj.county];
}
if (obj.state) {
return stateToCounties[obj.state];
}
if (obj.zip) {
return zipToCounties[obj.zip];
}
return undefined;
}
}