forked from OSSPhilippines/covid19-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertCountry.ts
More file actions
27 lines (24 loc) · 871 Bytes
/
convertCountry.ts
File metadata and controls
27 lines (24 loc) · 871 Bytes
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
import axios from "axios";
let countryCodes: { [key: string]: string } = {};
(async () => {
countryCodes = (await axios.get("http://country.io/names.json")).data;
})();
/**
*
* @param country A string that is either an 2 digit ISO country code or a full length countryname
* @returns Full version of the country name
*/
export const convertCountryCode: (country: string) => Promise<string> = async (
country
) => {
// Wait 1 second for countryCodes to initialize, needed for CLI
if (Object.keys(countryCodes).length === 0) {
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
}
if (country.length < 3) country = countryCodes[country.toUpperCase()];
if (country === undefined || typeof country === "undefined")
throw new Error(`Cannot find provided country`);
else return country;
};