forked from xcarpentier/react-native-country-picker-modal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountryService.ts
244 lines (220 loc) · 6.84 KB
/
CountryService.ts
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import {
CountryCode,
Country,
TranslationLanguageCode,
TranslationLanguageCodeMap,
FlagType,
CountryCodeList,
Region,
Subregion,
} from './types'
import Fuse from 'fuse.js'
const imageJsonUrl =
'https://xcarpentier.github.io/react-native-country-picker-modal/countries/'
type CountryMap = { [key in CountryCode]: Country }
interface DataCountry {
emojiCountries?: CountryMap
imageCountries?: CountryMap
}
const localData: DataCountry = {
emojiCountries: undefined,
imageCountries: undefined,
}
export const loadDataAsync = ((data: DataCountry) => (
dataType: FlagType = FlagType.EMOJI,
): Promise<CountryMap> => {
return new Promise((resolve, reject) => {
switch (dataType) {
case FlagType.FLAT:
if (!data.imageCountries) {
fetch(imageJsonUrl)
.then((response: Response) => response.json())
.then((remoteData: any) => {
data.imageCountries = remoteData
resolve(data.imageCountries)
})
.catch(reject)
} else {
resolve(data.imageCountries)
}
break
default:
if (!data.emojiCountries) {
data.emojiCountries = require('./assets/data/countries-emoji.json')
resolve(data.emojiCountries)
} else {
resolve(data.emojiCountries)
}
break
}
})
})(localData)
export const getEmojiFlagAsync = async (countryCode: CountryCode = 'FR') => {
const countries = await loadDataAsync()
if (!countries) {
throw new Error('Unable to find emoji because emojiCountries is undefined')
}
return countries[countryCode].flag
}
export const getImageFlagAsync = async (countryCode: CountryCode = 'FR') => {
const countries = await loadDataAsync(FlagType.FLAT)
if (!countries) {
throw new Error('Unable to find image because imageCountries is undefined')
}
return countries[countryCode].flag
}
export const getCountryNameAsync = async (
countryCode: CountryCode = 'FR',
translation: TranslationLanguageCode = 'common',
) => {
const countries = await loadDataAsync()
if (!countries) {
throw new Error('Unable to find image because imageCountries is undefined')
}
return countries[countryCode].name
? (countries[countryCode].name as TranslationLanguageCodeMap)[translation]
: (countries[countryCode].name as TranslationLanguageCodeMap)['common']
}
export const getCountryCallingCodeAsync = async (countryCode: CountryCode) => {
const countries = await loadDataAsync()
if (!countries) {
throw new Error('Unable to find image because imageCountries is undefined')
}
return countries[countryCode].callingCode[0]
}
export const getCountryCurrencyAsync = async (countryCode: CountryCode) => {
const countries = await loadDataAsync()
if (!countries) {
throw new Error('Unable to find image because imageCountries is undefined')
}
return countries[countryCode].currency[0]
}
const isCountryPresent = (countries: { [key in CountryCode]: Country }) => (
countryCode: CountryCode,
) => !!countries[countryCode]
const isRegion = (region?: Region) => (country: Country) =>
region ? country.region === region : true
const isSubregion = (subregion?: Subregion) => (country: Country) =>
subregion ? country.subregion === subregion : true
const isIncluded = (countryCodes?: CountryCode[]) => (country: Country) =>
countryCodes && countryCodes.length > 0
? countryCodes.includes(country.cca2)
: true
const isExcluded = (excludeCountries?: CountryCode[]) => (country: Country) =>
excludeCountries && excludeCountries.length > 0
? !excludeCountries.includes(country.cca2)
: true
export const getCountriesAsync = async (
flagType: FlagType,
translation: TranslationLanguageCode = 'common',
region?: Region,
subregion?: Subregion,
countryCodes?: CountryCode[],
excludeCountries?: CountryCode[],
preferredCountries?: CountryCode[],
withAlphaFilter?: boolean
): Promise<Country[]> => {
const countriesRaw = await loadDataAsync(flagType)
if (!countriesRaw) {
return []
}
if (preferredCountries && !withAlphaFilter) {
const newCountryCodeList = [...preferredCountries, ...CountryCodeList.filter(code => !preferredCountries.includes(code))]
const countries = newCountryCodeList.filter(isCountryPresent(countriesRaw))
.map((cca2: CountryCode) => ({
cca2,
...{
...countriesRaw[cca2],
name:
(countriesRaw[cca2].name as TranslationLanguageCodeMap)[
translation
] ||
(countriesRaw[cca2].name as TranslationLanguageCodeMap)['common'],
},
}))
.filter(isRegion(region))
.filter(isSubregion(subregion))
.filter(isIncluded(countryCodes))
.filter(isExcluded(excludeCountries))
return countries
} else {
const countries = CountryCodeList.filter(isCountryPresent(countriesRaw))
.map((cca2: CountryCode) => ({
cca2,
...{
...countriesRaw[cca2],
name:
(countriesRaw[cca2].name as TranslationLanguageCodeMap)[
translation
] ||
(countriesRaw[cca2].name as TranslationLanguageCodeMap)['common'],
},
}))
.filter(isRegion(region))
.filter(isSubregion(subregion))
.filter(isIncluded(countryCodes))
.filter(isExcluded(excludeCountries))
.sort((country1: Country, country2: Country) =>
(country1.name as string).localeCompare(country2.name as string),
)
return countries
}
}
const DEFAULT_FUSE_OPTION = {
shouldSort: true,
threshold: 0.3,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
keys: ['name', 'cca2', 'callingCode'],
}
let fuse: Fuse<Country>
export const search = (
filter: string = '',
data: Country[] = [],
options: Fuse.FuseOptions<any> = DEFAULT_FUSE_OPTION,
) => {
if (data.length === 0) {
return []
}
if (!fuse) {
fuse = new Fuse<Country>(data, options)
}
if (filter && filter !== '') {
const result = fuse.search(filter)
return result
} else {
return data
}
}
const uniq = (arr: any[]) => Array.from(new Set(arr))
export const getLetters = (countries: Country[]) => {
return uniq(
countries
.map((country: Country) =>
(country.name as string).substr(0, 1).toLocaleUpperCase(),
)
.sort((l1: string, l2: string) => l1.localeCompare(l2)),
)
}
export interface CountryInfo {
countryName: string
currency: string
callingCode: string
}
export const getCountryInfoAsync = async ({
countryCode,
translation,
}: {
countryCode: CountryCode
translation?: TranslationLanguageCode
}): Promise<CountryInfo> => {
const countryName = await getCountryNameAsync(
countryCode,
translation || 'common',
)
const currency = await getCountryCurrencyAsync(countryCode)
const callingCode = await getCountryCallingCodeAsync(countryCode)
return { countryName, currency, callingCode }
}