-
Notifications
You must be signed in to change notification settings - Fork 444
Expand file tree
/
Copy pathcountryCodeDataLoader.ts
More file actions
51 lines (43 loc) · 1.55 KB
/
countryCodeDataLoader.ts
File metadata and controls
51 lines (43 loc) · 1.55 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
import type {
CodeToCountryIsoMapType,
CountryEntry,
CountryIso,
IsoToCountryMapType,
} from './countryCodeData';
// Hardcoded US fallback for use before data loads
export const US_FALLBACK_ENTRY: CountryEntry = {
name: 'United States' as CountryEntry['name'],
iso: 'us' as CountryIso,
code: '1' as CountryEntry['code'],
pattern: '(...) ...-....' as CountryEntry['pattern'],
priority: 100,
};
// Module-level cache
let isoToCountryMap: IsoToCountryMapType | undefined;
let codeToCountriesMap: CodeToCountryIsoMapType | undefined;
let subAreaCodeSets: { us: ReadonlySet<string>; ca: ReadonlySet<string> } | undefined;
let loadPromise: Promise<void> | undefined;
export function loadCountryCodeData(): Promise<void> {
if (!loadPromise) {
loadPromise = import(/* webpackChunkName: "phone-country-data" */ './countryCodeData').then(mod => {
isoToCountryMap = mod.IsoToCountryMap;
codeToCountriesMap = mod.CodeToCountriesMap;
subAreaCodeSets = mod.SubAreaCodeSets;
});
}
return loadPromise;
}
export function isCountryCodeDataLoaded(): boolean {
return isoToCountryMap !== undefined;
}
export function getIsoToCountryMap(): IsoToCountryMapType | undefined {
return isoToCountryMap;
}
export function getCodeToCountriesMap(): CodeToCountryIsoMapType | undefined {
return codeToCountriesMap;
}
export function getSubAreaCodeSets() {
return subAreaCodeSets;
}
export type { CountryEntry, CountryIso, IsoToCountryMapType, CodeToCountryIsoMapType };
export type { CountryName, DialingCode, PhonePattern } from './countryCodeData';