All files / components/phone-input regions.ts

95.83% Statements 23/24
80% Branches 12/15
100% Functions 5/5
95.83% Lines 23/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 641x 1x               1x   1x 1x 1x 1x           1x     1x       1x   1x     3x 1x   3x   3x   750x         750x           750x       1x 2245x 528x   1717x 1717x        
import { getCallingCodeForCountry } from "./phone-input-validator";
import { getI18nCountries } from "../../utils/getI18nCountries";
 
export interface CountryData {
    country: string;
    label: string;
    prefix: string;
}
 
let defaultLang: string | null = null;
 
export function getDefaultLanguage(): string {
    Eif (defaultLang === null) {
        try {
            defaultLang = document
                .getElementsByTagName("html")[0]
                .getAttribute("lang")
                .replace(/[-_]/g, "-")
                .split("-")[0];
        } catch (e) {
            defaultLang = "en";
        }
    }
    return defaultLang;
}
 
/** List of countryCodes that should be displayed with the alias name instead of the official name */
const preferAliasCountries: string[] = [];
 
export async function getCountryDataProvider(
    lang: string
): Promise<CountryData[]> {
    if (lang === undefined || lang.length === 0) {
        lang = getDefaultLanguage();
    }
    const { countries } = await getI18nCountries(lang);
 
    return Object.keys(countries)
        .map((countryCode) => {
            const label: string = Array.isArray(countries[countryCode])
                ? (preferAliasCountries.includes(countryCode) &&
                      countries[countryCode][1]) ||
                  countries[countryCode][0]
                : countries[countryCode].toString();
            return {
                country: countryCode,
                label: label,
                prefix: getCallingCodeForCountry(countryCode),
            };
        })
        .filter(({ prefix }) => prefix !== "")
        .sort(sortCountries);
}
 
const sortCountries = (a: CountryData, b: CountryData): number => {
    if (a.label < b.label) {
        return -1;
    }
    Eif (a.label > b.label) {
        return 1;
    }
    return 0;
};