forked from i-rocky/country-list-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (118 loc) · 4 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict';
var self = module.exports = {
all: {},
cache: {},
findByIso2: code => x(self.all[code]),
findByIso3: code => find('iso3', code),
findByName: name => find('name', name),
findByCapital: name => find('capital', name),
findByCurrency: code => find('currency', code),
findByProvince(name) {
if (!self.cache.province) self.cache.province = {};
if (self.cache.province[name])
return self.cache.province[name].map(o => x(o));
return self.cache.province[name] = Object.keys(self.all)
.map(k => self.all[k])
.filter(o => o.provinces)
.filter(o => o.provinces.filter(
o => o.name == name || (o.alias || []).indexOf(name) > -1
).length > 0
)
.map(o => x(o))
.unpack(undefined);
},
findByPhoneNbr(nbr) {
// make sure the phone number is clean
nbr = nbr.replace(/\D/g, '');
// now match prefixes against the phone number
return phones.filter(o => o.nbr && nbr.startsWith(o.nbr))
.map(o => x(self.all[o.code]))
.unpack(undefined);
},
ls(field) {
return Object.keys(this.all).map(k => this.all[k][field]);
},
continents() {
return this.ls('continent').unique();
},
names() {
return this.ls('name');
},
capitals() {
return this.ls('capital');
}
};
// transform to the old format for backward-compatibility
function x(o) {
if (!o) return;
if (Array.isArray(o)) return o.map(x(o));
var ret = Object.assign({}, o);
ret.currency = {
code: ret.currency,
symbol: ret.currency_symbol,
decimal: ret.currency_decimal
};
ret.code = {iso2: ret.iso2, iso3: ret.iso3}
for (var k of 'iso2|iso3|currency_symbol|currency_decimal'.split('|'))
delete ret[k];
return ret;
}
function find(prop, val) {
if (!(prop in self.cache)) self.cache[prop] = {};
if (self.cache[prop][val])
return self.cache[prop][val];
return self.cache[prop][val] = Object.keys(self.all)
.filter(k => self.all[k][prop] == val)
.map(k => x(self.all[k]))
.unpack(undefined);
}
var continents = require('./data/continents.json');
var continent = require('./data/continent.json');
var iso_alpha_3 = require('./data/iso_alpha_3.json');
var capital = require('./data/capital.json');
var currency = require('./data/currency.json');
var currency_info = require('./data/currency_info.json');
var names = require('./data/names.json');
var phone = require('./data/phone.json');
var regions = require('./data/regions.json');
var provinces = require('./data/provinces.json');
// compact it into a single object
Object.keys(iso_alpha_3).forEach(function (k) {
self.all[k] = {
iso2: k,
iso3: iso_alpha_3[k],
name: names[k],
continent: continents[continent[k]],
region: regions[k],
capital: capital[k],
currency: currency[k],
currency_symbol: currency_info[currency[k]].symbol,
currency_decimal: currency_info[currency[k]].decimal,
dialing_code: phone[k],
provinces: provinces[k]
};
});
// release memory (except for phone)
continents = continent = iso_alpha_3
= capital = currency = currency_info
= names = regions = provinces
= null;
// phone gets turned into a sorted array
var phones = Object.keys(phone).map(function (k) {
return {code: k, nbr: phone[k].replace(/\D/g, '')}
})
// we need to match the phone number against the longest
// prefix we can find. that avoids matching against 1 (US)
// when we should be matching against 1246 (Barbados)
phones.sort((a,b) => a.nbr.length < b.nbr.length ? 1 : -1);
phone = null;
Array.prototype.unpack = function() {
var l = this.length;
return l == 1 ? this[0]
: l == 0 && arguments.length > 0
? undefined
: this;
}
Array.prototype.unique = function() {
return this.filter((e, pos) => this.indexOf(e) == pos);
}