country-data-list
Version:
Data about countries - like their ISO codes and currencies
144 lines (122 loc) • 3.54 kB
JavaScript
/**
* Country-list: A utility library for retrieving country, currency, and language information
*
* This module provides access to standardized data about countries, currencies,
* and languages, including ISO codes, calling codes, and currency symbols.
*/
import {
currencySymbolMap,
getSymbolFromCurrency,
getNameFromCurrency,
getSafeSymbolFromCurrency,
getSafeNameFromCurrency,
} from './data/currency-symbol.js';
import continents from './data/continents.js';
import * as regions from './data/regions.js';
import countriesAll from './data/countries.js';
import currenciesAll from './data/currencies.js';
import languagesAll from './data/languages.js';
import { timezones } from './data/timezones.module.js';
import lookupFn from './lookup.js';
const countries = {
all: countriesAll,
};
countriesAll.forEach((country) => {
// prefer assigned country codes over inactive ones
const { status } = countries[country.alpha2] || {};
if (!status || status === 'deleted') {
countries[country.alpha2] = country;
}
const { status: statusAlpha3 } = countries[country.alpha3] || {};
if (!statusAlpha3 || statusAlpha3 === 'deleted') {
countries[country.alpha3] = country;
}
});
const currencies = {
all: currenciesAll,
};
currenciesAll.forEach((currency) => {
// If the symbol isn't available, default to the currency code
let symbolCode = getSymbolFromCurrency(currency.code);
if (symbolCode === '?') {
symbolCode = currency.code;
}
const newCurrency = Object.assign(currency, { symbol: symbolCode });
currencies[currency.code] = newCurrency;
});
const languages = {
all: languagesAll,
};
// Note that for the languages there are several entries with the same alpha3 -
// eg Dutch and Flemish. Not sure how to best deal with that - here whichever
// comes last wins.
languagesAll.forEach((language) => {
languages[language.alpha2] = language;
languages[language.bibliographic] = language;
languages[language.alpha3] = language;
});
const lookup = lookupFn({
countries: countriesAll,
currencies: currenciesAll,
languages: languagesAll,
});
const callingCountries = { all: [] };
const callingCodesAll = countriesAll.reduce((codes, country) => {
const { countryCallingCodes, alpha2, alpha3 } = country;
if (countryCallingCodes && countryCallingCodes.length) {
callingCountries.all.push(country);
callingCountries[alpha2] = country;
callingCountries[alpha3] = country;
countryCallingCodes.forEach((code) => {
if (codes.indexOf(code) === -1) {
codes.push(code);
}
});
}
return codes;
}, []);
delete callingCountries['']; // remove empty alpha3s
callingCodesAll.sort((a, b) => {
const parse = (str) => +str;
const splitA = a.split(' ').map(parse);
const splitB = b.split(' ').map(parse);
if (splitA[0] < splitB[0]) {
return -1;
}
if (splitA[0] > splitB[0]) {
return 1;
}
// Same - check split[1]
if (splitA[1] === undefined && splitB[1] !== undefined) {
return -1;
}
if (splitA[1] !== undefined && splitB[1] === undefined) {
return 1;
}
if (splitA[1] < splitB[1]) {
return -1;
}
if (splitA[1] > splitB[1]) {
return 1;
}
return 0;
});
const callingCodes = {
all: callingCodesAll,
};
export {
continents,
regions,
countries,
currencies,
languages,
timezones,
lookup,
callingCountries,
callingCodes,
currencySymbolMap,
getSymbolFromCurrency,
getNameFromCurrency,
getSafeSymbolFromCurrency,
getSafeNameFromCurrency,
};