-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.ts
139 lines (126 loc) · 4.45 KB
/
index.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
import { isString } from 'lodash';
import countriesRaw from './countries';
import { Country, Maybe, Province } from './types';
export const getCountries = (): Country[] =>
(Object.keys(countriesRaw) as Array<keyof typeof countriesRaw>).reduce((acc, current) => {
acc.push(countriesRaw[current]);
return acc;
}, [] as typeof countriesRaw[keyof typeof countriesRaw][]);
/**
* Find the country object of the given country name
*
* @param {String} name country name
* @param {Boolean} [useAlias] use alias flag, default `false`
* @return {Object} country country object
*/
export function getCountryByName(name?: Maybe<string>, useAlias?: Maybe<boolean>): Maybe<Country> {
if (!isString(name)) return null;
return (
(getCountries() || []).find(function (country: Country) {
if (useAlias) {
return (
country.name.toUpperCase() === name.toUpperCase() ||
(country.alias || []).find(function (alias) {
return alias.toUpperCase() === name.toUpperCase();
})
);
}
return country.name.toUpperCase() === name.toUpperCase();
}) ?? null
);
}
/**
* Find the country object of the given country name or short name
*
* @param {String} name country name or short name (alpha2, alpha3)
* @param {Boolean} [useAlias] use alias flag, default `false`
* @return {Object} country country object
*/
export function getCountryByNameOrShortName(
name?: Maybe<string>,
useAlias?: Maybe<boolean>,
): Maybe<Country> {
if (!isString(name)) return null;
return (
(getCountries() || []).find(function (country: Country) {
if (useAlias) {
return (
country.name.toUpperCase() === name.toUpperCase() ||
country.alpha2.toUpperCase() === name.toUpperCase() ||
country.alpha3.toUpperCase() === name.toUpperCase() ||
(country.alias || []).find(function (alias) {
return alias.toUpperCase() === name.toUpperCase();
})
);
}
return (
country.name.toUpperCase() === name.toUpperCase() ||
country.alpha2.toUpperCase() === name.toUpperCase() ||
country.alpha3.toUpperCase() === name.toUpperCase()
);
}) ?? null
);
}
export const findCountryByName = getCountryByName;
export const findCountryByNameOrShortName = getCountryByNameOrShortName;
/**
* Find the province object of the given province name
*
* @param {String} name english province name
* @param {Boolean} [useAlias] use alias flag, default `false`
* @return {Object} province province object
*/
export function getProvinceByName(
country: Country,
name?: Maybe<string>,
useAlias?: Maybe<boolean>,
): Maybe<Province> {
if (!isString(name) || !Array.isArray(country.provinces)) return null;
return (
(country.provinces || []).find(function (province: Province) {
if (useAlias) {
return (
province.name.toUpperCase() === name.toUpperCase() ||
(province.alias || []).find(function (alias) {
return alias.toUpperCase() === name.toUpperCase();
})
);
}
return province.name.toUpperCase() === name.toUpperCase();
}) ?? null
);
}
/**
* Find the province object of the given province name or short name
*
* @param {String} name english province name or short name
* @param {Boolean} [useAlias] use alias flag, default `false`
* @return {Object} province province object
*/
export function getProvinceByNameOrShortName(
country: Country,
name?: Maybe<string>,
useAlias?: Maybe<boolean>,
): Maybe<Province> {
if (!isString(name) || !Array.isArray(country.provinces)) return null;
return (
(country.provinces || []).find(function (province: Province) {
if (useAlias) {
return (
province.name.toUpperCase() === name.toUpperCase() ||
(province.short && province.short.toUpperCase() === name.toUpperCase()) ||
(province.alias || []).find(function (alias) {
return alias.toUpperCase() === name.toUpperCase();
})
);
}
return (
province.name.toUpperCase() === name.toUpperCase() ||
(province.short && province.short.toUpperCase() === name.toUpperCase())
);
}) ?? null
);
}
export const findProvinceByName = getProvinceByName;
export const findProvinceByNameOrShortName = getProvinceByNameOrShortName;
export default countriesRaw;