forked from mrafiqk/country-state-district
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (33 loc) · 941 Bytes
/
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
let getAllCountries = () => {
return require('./country.json');
}
module.exports.getAllCountries = getAllCountries;
let getAllStates = () => {
return require('./state.json');
}
module.exports.getAllStates = getAllStates;
let getStatesByCountryId = (id) => {
let states = indexBy(getAllStates(), 'country_id');
return states[id];
}
module.exports.getStatesByCountryId = getStatesByCountryId;
let getAllDistricts = () => {
return require('./city.json');
}
module.exports.getAllDistricts = getAllDistricts;
let getDistrictsByStateId = (id) => {
let district = indexBy(getAllDistricts(), 'state_id');
return district[id];
}
module.exports.getDistrictsByStateId = getDistrictsByStateId;
let indexBy = (array_val, key) => {
let returnVal = {};
array_val.forEach(item => {
if(returnVal[item[key]]) {
returnVal[item[key]].push(item)
} else {
returnVal[item[key]] = [item]
}
})
return returnVal;
}