Utilities for converting between country calling codes and ISO region codes, plus discovery of supported regions. These functions provide essential mapping functionality for international phone number handling and region validation.
Converts an ISO region code to its corresponding numeric country calling code.
/**
* Get the country calling code for a given region code
* @param regionCode 2-character ISO region code (e.g., "SE", "US", "GB")
* @returns Numeric country calling code (e.g., 46 for Sweden)
*/
function getCountryCodeForRegionCode(regionCode: string): number;Usage Examples:
import { getCountryCodeForRegionCode } from "awesome-phonenumber";
console.log(getCountryCodeForRegionCode("SE")); // 46 (Sweden)
console.log(getCountryCodeForRegionCode("US")); // 1 (United States)
console.log(getCountryCodeForRegionCode("GB")); // 44 (United Kingdom)
console.log(getCountryCodeForRegionCode("DE")); // 49 (Germany)
console.log(getCountryCodeForRegionCode("JP")); // 81 (Japan)Converts a numeric country calling code to its corresponding ISO region code.
/**
* Get the region code for a given country calling code
* @param countryCode Numeric country calling code (e.g., 46, 1, 44)
* @returns 2-character ISO region code (e.g., "SE", "US", "GB")
*/
function getRegionCodeForCountryCode(countryCode: number): string;Usage Examples:
import { getRegionCodeForCountryCode } from "awesome-phonenumber";
console.log(getRegionCodeForCountryCode(46)); // "SE" (Sweden)
console.log(getRegionCodeForCountryCode(1)); // "US" (United States)
console.log(getRegionCodeForCountryCode(44)); // "GB" (United Kingdom)
console.log(getRegionCodeForCountryCode(49)); // "DE" (Germany)
console.log(getRegionCodeForCountryCode(81)); // "JP" (Japan)Returns an array of all country calling codes supported by the library.
/**
* Get all supported country calling codes
* @returns Array of calling code strings (e.g., ["1", "7", "20", "27", ...])
*/
function getSupportedCallingCodes(): string[];Usage Examples:
import { getSupportedCallingCodes } from "awesome-phonenumber";
const callingCodes = getSupportedCallingCodes();
console.log(callingCodes.length); // ~250+ supported codes
console.log(callingCodes.slice(0, 10)); // ["1", "7", "20", "27", "30", ...]
// Check if a calling code is supported
const isSupported = callingCodes.includes("46");
console.log(isSupported); // true
// Find codes starting with "1"
const nanpCodes = callingCodes.filter(code => code.startsWith("1"));
console.log(nanpCodes); // ["1"] - NANP regionReturns an array of all ISO region codes supported by the library.
/**
* Get all supported ISO region codes
* @returns Array of 2-character region code strings (e.g., ["AD", "AE", "AF", ...])
*/
function getSupportedRegionCodes(): string[];Usage Examples:
import { getSupportedRegionCodes } from "awesome-phonenumber";
const regionCodes = getSupportedRegionCodes();
console.log(regionCodes.length); // ~250+ supported regions
console.log(regionCodes.slice(0, 10)); // ["AD", "AE", "AF", "AG", "AI", ...]
// Check if a region is supported
const isSupported = regionCodes.includes("SE");
console.log(isSupported); // true
// Find European regions (simplified example)
const europeanCodes = regionCodes.filter(code =>
["AD", "AT", "BE", "CH", "DE", "DK", "ES", "FI", "FR", "GB", "IE", "IT", "NL", "NO", "SE"].includes(code)
);
console.log(europeanCodes);import { getSupportedRegionCodes, getCountryCodeForRegionCode } from "awesome-phonenumber";
function validateRegionCode(regionCode: string): boolean {
const supportedRegions = getSupportedRegionCodes();
return supportedRegions.includes(regionCode.toUpperCase());
}
function getRegionInfo(regionCode: string) {
if (!validateRegionCode(regionCode)) {
throw new Error(`Unsupported region code: ${regionCode}`);
}
return {
regionCode: regionCode.toUpperCase(),
countryCode: getCountryCodeForRegionCode(regionCode),
isSupported: true
};
}
console.log(getRegionInfo("se")); // { regionCode: "SE", countryCode: 46, isSupported: true }import { getSupportedRegionCodes, getCountryCodeForRegionCode } from "awesome-phonenumber";
// Create a mapping for UI components
function buildRegionMapping() {
const regions = getSupportedRegionCodes();
return regions.map(regionCode => ({
regionCode,
countryCode: getCountryCodeForRegionCode(regionCode),
displayName: `${regionCode} (+${getCountryCodeForRegionCode(regionCode)})`
}))
.sort((a, b) => a.regionCode.localeCompare(b.regionCode));
}
const regionOptions = buildRegionMapping();
console.log(regionOptions.slice(0, 5));
// [
// { regionCode: "AD", countryCode: 376, displayName: "AD (+376)" },
// { regionCode: "AE", countryCode: 971, displayName: "AE (+971)" },
// ...
// ]import { getSupportedCallingCodes, getRegionCodeForCountryCode } from "awesome-phonenumber";
// Build reverse lookup for country codes to regions
function buildCountryCodeLookup() {
const callingCodes = getSupportedCallingCodes();
const lookup = new Map<number, string>();
callingCodes.forEach(codeStr => {
const code = parseInt(codeStr);
const region = getRegionCodeForCountryCode(code);
lookup.set(code, region);
});
return lookup;
}
const countryCodeMap = buildCountryCodeLookup();
console.log(countryCodeMap.get(46)); // "SE"
console.log(countryCodeMap.get(1)); // "US"import {
getSupportedRegionCodes,
getCountryCodeForRegionCode,
parsePhoneNumber
} from "awesome-phonenumber";
class RegionManager {
private supportedRegions: string[];
constructor() {
this.supportedRegions = getSupportedRegionCodes();
}
isRegionSupported(regionCode: string): boolean {
return this.supportedRegions.includes(regionCode.toUpperCase());
}
getCountryCode(regionCode: string): number {
if (!this.isRegionSupported(regionCode)) {
throw new Error(`Region ${regionCode} not supported`);
}
return getCountryCodeForRegionCode(regionCode);
}
parseWithFallback(phoneNumber: string, preferredRegions: string[]) {
// Try parsing with each preferred region
for (const region of preferredRegions) {
if (this.isRegionSupported(region)) {
const result = parsePhoneNumber(phoneNumber, { regionCode: region });
if (result.valid) {
return result;
}
}
}
// Fallback to international parsing
return parsePhoneNumber(phoneNumber);
}
}
const regionManager = new RegionManager();
const result = regionManager.parseWithFallback("0707123456", ["SE", "NO", "DK"]);