forked from AnupKumarPanwar/country-state-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (38 loc) · 1.15 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
compare = function (country, arg) {
arg = arg.toLowerCase();
return country.code.toLowerCase() == arg || country.name.toLowerCase() == arg || country.dial_code.toLowerCase() == arg
};
exports.getCountries = function () {
let countries = require('./data/countries.json');
return countries;
};
exports.getCountry = function (arg) {
let countries = require('./data/countries.json');
for (let i = 0; i < countries.length; i++) {
if (compare(countries[i], arg)) {
return countries[i];
}
}
return null;
};
exports.getFilteredCountries = function (args) {
let countries = require('./data/countries.json');
let filteredCountries = [];
for (let arg of args) {
for (let i = 0; i < countries.length; i++) {
if (compare(countries[i], arg)) {
filteredCountries.push(countries[i]);
break;
}
}
}
return filteredCountries;
};
exports.getStates = function (countryCode) {
let states = require('./data/states.json');
try {
return states[countryCode];
} catch (error) {
return 'Invalid country code';
}
};