country-codes-library
Version:
The Country Code Library provides a collection of two-letter and three-letter country codes according to the ISO 3166-1 standard, as well as it provides USA, China and Canada Province codes (State codes / adminstrative division codes). In addition, it inc
242 lines (241 loc) • 7.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCountryByCode2 = getCountryByCode2;
exports.getCountryByCode3 = getCountryByCode3;
exports.getCountryByName = getCountryByName;
exports.getCountriesByCurrency = getCountriesByCurrency;
exports.getCountriesByCallingCode = getCountriesByCallingCode;
exports.searchCountries = searchCountries;
exports.convertCountryCode = convertCountryCode;
exports.getCallingCode = getCallingCode;
exports.getCurrencyCode = getCurrencyCode;
exports.getCurrencySymbol = getCurrencySymbol;
exports.getAllCurrencies = getAllCurrencies;
exports.getAllCallingCodes = getAllCallingCodes;
const countryData_1 = require("./countryData");
/**
* Get country information by 2-letter ISO code
* @param code - ISO 3166-1 alpha-2 code (e.g., "US", "GB", "CN")
* @returns Country object or null if not found
* @example
* ```ts
* const country = getCountryByCode2("US");
* // Returns: { name: "UnitedStates", code2: "US", code3: "USA", ... }
* ```
*/
function getCountryByCode2(code) {
const upperCode = code.toUpperCase();
for (const country of (0, countryData_1.getAllCountries)()) {
if (country.code2.toUpperCase() === upperCode) {
return country;
}
}
return null;
}
/**
* Get country information by 3-letter ISO code
* @param code - ISO 3166-1 alpha-3 code (e.g., "USA", "GBR", "CHN")
* @returns Country object or null if not found
* @example
* ```ts
* const country = getCountryByCode3("USA");
* // Returns: { name: "UnitedStates", code2: "US", code3: "USA", ... }
* ```
*/
function getCountryByCode3(code) {
const upperCode = code.toUpperCase();
for (const country of (0, countryData_1.getAllCountries)()) {
if (country.code3.toUpperCase() === upperCode) {
return country;
}
}
return null;
}
/**
* Get country information by name
* @param name - Country name (e.g., "UnitedStates", "United States")
* @returns Country object or null if not found
* @example
* ```ts
* const country = getCountryByName("UnitedStates");
* // Returns: { name: "UnitedStates", code2: "US", code3: "USA", ... }
* ```
*/
function getCountryByName(name) {
// Try exact match first
if (countryData_1.CountryData[name]) {
return countryData_1.CountryData[name];
}
// Try case-insensitive match
const lowerName = name.toLowerCase().replace(/\s+/g, '');
for (const countryName in countryData_1.CountryData) {
if (countryName.toLowerCase().replace(/\s+/g, '') === lowerName) {
return countryData_1.CountryData[countryName];
}
}
return null;
}
/**
* Get all countries that use a specific currency
* @param currencyCode - ISO 4217 currency code (e.g., "USD", "EUR")
* @returns Array of countries using the currency
* @example
* ```ts
* const countries = getCountriesByCurrency("EUR");
* // Returns array of European countries using Euro
* ```
*/
function getCountriesByCurrency(currencyCode) {
const upperCurrency = currencyCode.toUpperCase();
return (0, countryData_1.getAllCountries)().filter(country => country.currencyCode.toUpperCase() === upperCurrency);
}
/**
* Get all countries with a specific calling code
* @param callingCode - International calling code (e.g., "+1", "+44")
* @returns Array of countries with the calling code
* @example
* ```ts
* const countries = getCountriesByCallingCode("+1");
* // Returns: [USA, Canada, and other +1 countries]
* ```
*/
function getCountriesByCallingCode(callingCode) {
// Normalize calling code (add + if missing)
const normalizedCode = callingCode.startsWith('+') ? callingCode : `+${callingCode}`;
return (0, countryData_1.getAllCountries)().filter(country => country.callingCode === normalizedCode);
}
/**
* Search countries by name (supports partial matching)
* @param query - Search query
* @param options - Search options
* @returns Array of matching countries
* @example
* ```ts
* const results = searchCountries("united");
* // Returns: [UnitedStates, UnitedKingdom, UnitedArabEmirates]
* ```
*/
function searchCountries(countryName, options = {}) {
const { caseSensitive = false, exactMatch = false } = options;
const searchQuery = caseSensitive ? countryName : countryName.toLowerCase();
return (0, countryData_1.getAllCountries)().filter(country => {
const countryName = caseSensitive ? country.name : country.name.toLowerCase();
if (exactMatch) {
return countryName === searchQuery;
}
return countryName.includes(searchQuery);
});
}
/**
* Convert between 2-letter and 3-letter country codes
* @param code - Country code to convert
* @param targetFormat - Target format ('code2' or 'code3')
* @returns Converted code or null if not found
* @example
* ```ts
* convertCountryCode("US", "code3"); // Returns: "USA"
* convertCountryCode("GBR", "code2"); // Returns: "GB"
* ```
*/
function convertCountryCode(code, targetFormat) {
// Try as 2-letter code first
let country = getCountryByCode2(code);
// If not found, try as 3-letter code
if (!country) {
country = getCountryByCode3(code);
}
if (!country) {
return null;
}
return targetFormat === 'code2' ? country.code2 : country.code3;
}
/**
* Get calling code for a country
* @param countryIdentifier - Country name, code2, or code3
* @returns Calling code or null if not found
* @example
* ```ts
* getCallingCode("US"); // Returns: "+1"
* getCallingCode("UnitedStates"); // Returns: "+1"
* ```
*/
function getCallingCode(countryIdentifier) {
let country = getCountryByName(countryIdentifier);
if (!country) {
country = getCountryByCode2(countryIdentifier);
}
if (!country) {
country = getCountryByCode3(countryIdentifier);
}
return country ? country.callingCode : null;
}
/**
* Get currency code for a country
* @param countryIdentifier - Country name, code2, or code3
* @returns Currency code or null if not found
* @example
* ```ts
* getCurrencyCode("US"); // Returns: "USD"
* getCurrencyCode("Germany"); // Returns: "EUR"
* ```
*/
function getCurrencyCode(countryIdentifier) {
let country = getCountryByName(countryIdentifier);
if (!country) {
country = getCountryByCode2(countryIdentifier);
}
if (!country) {
country = getCountryByCode3(countryIdentifier);
}
return country ? country.currencyCode : null;
}
/**
* Get currency symbol for a country
* @param countryIdentifier - Country name, code2, or code3
* @returns Currency symbol or null if not found
* @example
* ```ts
* getCurrencySymbol("US"); // Returns: "$"
* getCurrencySymbol("Japan"); // Returns: "¥"
* ```
*/
function getCurrencySymbol(countryIdentifier) {
let country = getCountryByName(countryIdentifier);
if (!country) {
country = getCountryByCode2(countryIdentifier);
}
if (!country) {
country = getCountryByCode3(countryIdentifier);
}
return country ? country.currencySymbol : null;
}
/**
* Get all unique currencies used worldwide
* @returns Array of unique currency codes
*/
function getAllCurrencies() {
const currencies = new Set();
(0, countryData_1.getAllCountries)().forEach(country => {
if (country.currencyCode) {
currencies.add(country.currencyCode);
}
});
return Array.from(currencies).sort();
}
/**
* Get all unique calling codes
* @returns Array of unique calling codes
*/
function getAllCallingCodes() {
const codes = new Set();
(0, countryData_1.getAllCountries)().forEach(country => {
if (country.callingCode && country.callingCode !== '+0') {
codes.add(country.callingCode);
}
});
return Array.from(codes).sort((a, b) => {
const numA = parseInt(a.replace('+', ''));
const numB = parseInt(b.replace('+', ''));
return numA - numB;
});
}