forked from thegamenicorus/react-native-phone-input
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountry.js
More file actions
63 lines (51 loc) · 1.31 KB
/
country.js
File metadata and controls
63 lines (51 loc) · 1.31 KB
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
import { find, orderBy } from 'lodash';
let instance = null;
class Country {
static getInstance() {
if (!instance) {
instance = new Country();
}
return instance;
}
constructor() {
this.countryCodes = [];
this.countriesData = null;
}
setCustomCountriesData(json) {
this.countriesData = json;
}
addCountryCode(iso2, dialCode, priority) {
if (!(dialCode in this.countryCodes)) {
this.countryCodes[dialCode] = [];
}
const index = priority || 0;
this.countryCodes[dialCode][index] = iso2;
}
getAll() {
if (!this.countries) {
this.countries = orderBy(
this.countriesData || require('./resources/countries.json'),
['name'],
['asc'],
);
}
return this.countries;
}
getCountryCodes() {
if (!this.countryCodes.length) {
this.getAll().map((country) => {
this.addCountryCode(country.iso2, country.dialCode, country.priority);
if (country.areaCodes) {
country.areaCodes.map((areaCode) => {
this.addCountryCode(country.iso2, country.dialCode + areaCode);
});
}
});
}
return this.countryCodes;
}
getCountryDataByCode(iso2) {
return find(this.getAll(), country => country.iso2 === iso2);
}
}
export default Country.getInstance();