forked from harpreetkhalsagtbit/country-state-city
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (44 loc) · 1.1 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
var countryList = require("./lib/country.json")
var stateList = require("./lib/state.json")
var cityList = require("./lib/city.json")
var country_state_city = {
getCountryById: function (id) {
return _findEntry(countryList, id);
},
getStateById: function (id) {
return _findEntry(stateList, id);
},
getCityById: function (id) {
return _findEntry(cityList, id);
},
getStatesOfCountry: function (countryId) {
var states = stateList.filter(function (value, index) {
return value.country_id == countryId
})
return states.sort(compare)
},
getCitiesOfState: function (stateId) {
var cities = cityList.filter(function (value, index) {
return value.state_id == stateId
})
return cities.sort(compare)
},
getAllCountries: function () {
return countryList;
}
}
let _findEntry = (source, id) => {
if (!isNaN(id) && source != null) {
let idx = source.findIndex((c, i) => c.id === id);
return (idx !== -1) ? source[idx] : "";
}
else return "";
}
function compare(a, b) {
if (a.name < b.name)
return -1;
if (a.name > b.name)
return 1;
return 0;
}
module.exports = country_state_city;