-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
22 lines (18 loc) · 958 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { CountriesInfoInterface, EuCountries, EeaCountries } from "./data";
const isEqual = (str1: string, str2: string) =>
str1.toLowerCase() === str2.toLowerCase();
const getValidator = (countryInput: string) => {
if (!!+countryInput)
return (item: CountriesInfoInterface) => countryInput + "" === item.numeric;
if (countryInput.length === 2)
return (item: CountriesInfoInterface) => isEqual(countryInput, item.alpha2);
if (countryInput.length === 3)
return (item: CountriesInfoInterface) => isEqual(countryInput, item.alpha3);
return (item: CountriesInfoInterface) => isEqual(countryInput, item.name);
};
const commonProvider =
(validList: CountriesInfoInterface[]) =>
(countryInput: string): false | undefined | CountriesInfoInterface =>
!countryInput ? false : validList.find(getValidator(countryInput));
export const isEUCountry = commonProvider(EuCountries);
export const isEEACountry = commonProvider(EeaCountries);