forked from fannarsh/country-list
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcountry-list.js
50 lines (40 loc) · 1.03 KB
/
country-list.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
'use strict'
var data = require('./data.json')
/**
* Precompute name and code lookups.
*/
var nameMap = {}
var codeMap = {}
data.forEach(function (country) {
nameMap[country.name.toLowerCase()] = country.code
codeMap[country.code.toLowerCase()] = country.name
})
module.exports = CountryList
function CountryList () {
if (!(this instanceof CountryList)) return new CountryList()
};
CountryList.prototype.getCode = function getCode (name) {
return nameMap[name.toLowerCase()]
}
CountryList.prototype.getName = function getName (code) {
return codeMap[code.toLowerCase()]
}
CountryList.prototype.getNames = function getNames () {
return data.map(function (country) {
return country.name
})
}
CountryList.prototype.getCodes = function getCodes () {
return data.map(function (country) {
return country.code
})
}
CountryList.prototype.getCodeList = function () {
return codeMap
}
CountryList.prototype.getNameList = function () {
return nameMap
}
CountryList.prototype.getData = function getData () {
return data
}