country-codes-kit
Version:
simple package to retrieve country calling codes, iso codes and currency symbols
112 lines • 4.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPhoneCode = getPhoneCode;
exports.getPhoneCodeByCountryName = getPhoneCodeByCountryName;
exports.getCurrency = getCurrency;
exports.getCountryISOCode = getCountryISOCode;
exports.getCountryDetails = getCountryDetails;
const index_js_1 = require("../codes/index.js");
/**
* Given an ISO2 country code, returns an array of phone codes associated with
* that country. If the country code does not exist in the mapping, returns an
* empty array.
*
* @param {ISO2_CODE} country_code - The ISO2 country code
* @returns {string[]} - Array of phone codes for the given country code
* @example
* getPhoneCodes("US") // ["1"]
* getPhoneCodes("NG") // ["234"]
*/
function getPhoneCode(country_code) {
if (index_js_1.ISO2_PHONE_CODE[country_code]) {
return index_js_1.ISO2_PHONE_CODE[country_code];
}
return [];
}
/**
* Given a country name, returns an array of phone codes associated with that
* country. If the country code does not exist in the mapping, returns an empty
* array.
*
* @param {string} country - The country name
* @returns {string[]} - Array of phone codes associated with the given country
* @example
* getPhoneCodesByCountryName("United States") // ["1"]
* getPhoneCodesByCountryName("Nigeria") // ["234"]
*/
function getPhoneCodeByCountryName(country) {
if (index_js_1.COUNTRY_ISO_CODE_2[country]) {
const countryCode = index_js_1.COUNTRY_ISO_CODE_2[country];
return getPhoneCode(countryCode);
}
return [];
}
/**
* Given an ISO2 code or a string representing a country code, returns the
* currency symbol associated with that country. If the country code is not
* found, it returns a default symbol "#".
*
* @param {ISO2_CODE | string} country_code - ISO2 code or a string representing
* the country code
* @returns {string} - The currency symbol or "#" if the country code is not found
*/
function getCurrency(country_code) {
if (index_js_1.CURRENCY_CODE[country_code]) {
return index_js_1.CURRENCY_CODE[country_code];
}
return "#";
}
/**
* Given a country name, returns the ISO2 or ISO3 code associated with that
* country. If given an Options object with an "iso_2" property set to true,
* returns the ISO2 code. If given an Options object with an "iso_3" property
* set to true, returns the ISO3 code. Otherwise, defaults to returning the ISO2
* code.
*
* @param {string} country - The country name
* @param {Options} [options] - Options object with properties "iso_2" and/or
* "iso_3" set to true
* @returns {string} - The ISO2 or ISO3 code associated with the given country
*/
function getCountryISOCode(country, options) {
country = country.toUpperCase();
if (options === null || options === void 0 ? void 0 : options.iso_2) {
return index_js_1.COUNTRY_ISO_CODE_2[country];
}
if (options === null || options === void 0 ? void 0 : options.iso_3) {
return index_js_1.COUNTRY_ISO_CODE_3[country];
}
return index_js_1.COUNTRY_ISO_CODE_2[country];
}
/**
* Given a string representing a country name, returns an object containing the
* country name, its ISO2 and ISO3 codes, and its associated phone codes and
* currency symbol. If the country code does not exist in the mapping, returns
* undefined.
*
* @param {string} country - The country name
* @returns {ICountryDetails | undefined} - The country details object, or undefined
* if the country code is not found
* @example
* getCountryDetails("United States")
* // {
* // country: "United States",
* // phone_codes: ["1"],
* // isoCode2: "US",
* // isoCode3: "USA",
* // currency: "$"
* // }
*/
function getCountryDetails(country) {
country = country.toUpperCase();
if (index_js_1.COUNTRY_ISO_CODE_2[country]) {
return {
country,
phone_codes: getPhoneCodeByCountryName(country),
isoCode2: index_js_1.COUNTRY_ISO_CODE_2[country],
isoCode3: index_js_1.COUNTRY_ISO_CODE_3[country],
currency: getCurrency(index_js_1.COUNTRY_ISO_CODE_2[country]),
};
}
}
//# sourceMappingURL=index.js.map