-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
170 lines (155 loc) · 6.33 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
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
/**
* Represents a country with its relevant codes and name.
* @typedef {Object} Country
* @property {string} cca3 - ISO3 code.
* @property {string} cca2 - ISO2 code.
* @property {string} ccn3 - Numeric code (as a string).
* @property {Object} name - Contains the country name.
* @property {string} name.common - Country name.
*/
type Country = {
cca3: string; // ISO3 code
cca2: string; // ISO2 code
ccn3: string; // Numeric code (as a string)
name: {
common: string; // Country name
};
};
let countriesCache: Country[] | null = null;
/**
* Fetches and caches country data from the REST Countries API.
* @returns {Promise<Country[]>} A promise that resolves to an array of Country objects.
*/
async function fetchCountries(): Promise<Country[]> {
if (countriesCache) return countriesCache; // Use cache if available
const response = await fetch("https://restcountries.com/v3.1/all");
const data = (await response.json()) as Country[];
countriesCache = data;
return countriesCache;
}
/**
* Converts an ISO3 code to an ISO2 code.
* @param {string} iso3 - The ISO3 code of the country.
* @returns {Promise<string | null>} A promise that resolves to the ISO2 code, or null if not found.
*/
export async function iso3ToIso2(iso3: string): Promise<string | null> {
const countries = await fetchCountries();
const country = countries.find((c) => c.cca3 === iso3.toUpperCase());
return country ? country.cca2 : null;
}
/**
* Converts an ISO2 code to an ISO3 code.
* @param {string} iso2 - The ISO2 code of the country.
* @returns {Promise<string | null>} A promise that resolves to the ISO3 code, or null if not found.
*/
export async function iso2ToIso3(iso2: string): Promise<string | null> {
const countries = await fetchCountries();
const country = countries.find((c) => c.cca2 === iso2.toUpperCase());
return country ? country.cca3 : null;
}
/**
* Converts an ISO3 code to its numeric representation.
* @param {string} iso3 - The ISO3 code of the country.
* @returns {Promise<string | null>} A promise that resolves to the numeric code, or null if not found.
*/
export async function iso3ToNumeric(iso3: string): Promise<string | null> {
const countries = await fetchCountries();
const country = countries.find((c) => c.cca3 === iso3.toUpperCase());
return country ? country.ccn3 : null;
}
/**
* Converts a numeric code to an ISO3 code.
* @param {string} numeric - The numeric code of the country.
* @returns {Promise<string | null>} A promise that resolves to the ISO3 code, or null if not found.
*/
export async function numericToIso3(numeric: string): Promise<string | null> {
const countries = await fetchCountries();
const country = countries.find((c) => c.ccn3 === numeric);
return country ? country.cca3 : null;
}
/**
* Converts an ISO3 code to the country's name.
* @param {string} iso3 - The ISO3 code of the country.
* @returns {Promise<string | null>} A promise that resolves to the country name, or null if not found.
*/
export async function iso3ToName(iso3: string): Promise<string | null> {
const countries = await fetchCountries();
const country = countries.find((c) => c.cca3 === iso3.toUpperCase());
return country ? country.name.common : null;
}
/**
* Converts a country name to its ISO3 code.
* @param {string} name - The name of the country.
* @returns {Promise<string | null>} A promise that resolves to the ISO3 code, or null if not found.
*/
export async function nameToIso3(name: string): Promise<string | null> {
const countries = await fetchCountries();
const country = countries.find(
(c) => c.name.common.toLowerCase() === name.toLowerCase()
);
return country ? country.cca3 : null;
}
/**
* Converts an ISO2 code to its numeric representation.
* @param {string} iso2 - The ISO2 code of the country.
* @returns {Promise<string | null>} A promise that resolves to the numeric code, or null if not found.
*/
export async function iso2ToNumeric(iso2: string): Promise<string | null> {
const countries = await fetchCountries();
const country = countries.find((c) => c.cca2 === iso2.toUpperCase());
return country ? country.ccn3 : null;
}
/**
* Converts a numeric code to an ISO2 code.
* @param {string} numeric - The numeric code of the country.
* @returns {Promise<string | null>} A promise that resolves to the ISO2 code, or null if not found.
*/
export async function numericToIso2(numeric: string): Promise<string | null> {
const countries = await fetchCountries();
const country = countries.find((c) => c.ccn3 === numeric);
return country ? country.cca2 : null;
}
/**
* Converts a country name to its ISO2 code.
* @param {string} name - The name of the country.
* @returns {Promise<string | null>} A promise that resolves to the ISO2 code, or null if not found.
*/
export async function nameToIso2(name: string): Promise<string | null> {
const countries = await fetchCountries();
const country = countries.find(
(c) => c.name.common.toLowerCase() === name.toLowerCase()
);
return country ? country.cca2 : null;
}
/**
* Converts an ISO2 code to the country's name.
* @param {string} iso2 - The ISO2 code of the country.
* @returns {Promise<string | null>} A promise that resolves to the country name, or null if not found.
*/
export async function iso2ToName(iso2: string): Promise<string | null> {
const countries = await fetchCountries();
const country = countries.find((c) => c.cca2 === iso2.toUpperCase());
return country ? country.name.common : null;
}
/**
* Converts a numeric code to the country's name.
* @param {string} numeric - The numeric code of the country.
* @returns {Promise<string | null>} A promise that resolves to the country name, or null if not found.
*/
export async function numericToName(numeric: string): Promise<string | null> {
const countries = await fetchCountries();
const country = countries.find((c) => c.ccn3 === numeric);
return country ? country.name.common : null;
}
/**
* Converts a country name to its numeric representation.
* @param {string} name - The name of the country.
* @returns {Promise<string | null>} A promise that resolves to the numeric code, or null if not found.
*/
export async function nameToNumeric(name: string): Promise<string | null> {
const countries = await fetchCountries();
const country = countries.find(
(c) => c.name.common.toLowerCase() === name.toLowerCase()
);
return country ? country.ccn3 : null;
}