-
Notifications
You must be signed in to change notification settings - Fork 35
/
country-list.js
78 lines (62 loc) · 1.48 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
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
class CountryList {
constructor() {
this.data = require('./data.json')
this.labelMap = {}
this.valueMap = {}
this.data.forEach(country => {
this.labelMap[country.label.toLowerCase()] = country.value
this.valueMap[country.value.toLowerCase()] = country.label
})
}
getValue(label) {
return this.labelMap[label.toLowerCase()]
}
getLabel(value) {
return this.valueMap[value.toLowerCase()]
}
getLabels() {
return this.data.map(country => country.label)
}
getValues() {
return this.data.map(country => country.value)
}
getLabelList() {
return this.labelMap
}
getValueList() {
return this.valueMap
}
getData() {
return this.data
}
setLabel(value, label) {
this.data.forEach(country => {
if (country.value === value) {
country.label = label
this.valueMap[country.value.toLowerCase()] = country.label
}
})
return this
}
setEmpty(label) {
this.data.unshift({
value: '',
label: label,
})
this.valueMap[''] = label
this.labelMap[label] = ''
return this
}
native() {
this.nativeData = require('./data-native.json')
this.nativeData.forEach(country => {
this.labelMap[country.label.toLowerCase()] = country.value
this.valueMap[country.value.toLowerCase()] = country.label
})
return this
}
}
const countryList = () => {
if (!(this instanceof CountryList)) return new CountryList()
}
module.exports = countryList