fn

findPhoneCountry

v0.0.5

Resolve a digit string to its country among a PhoneCountrylist. Matches the longest dialing code (codes are prefix-free, so this is unambiguous), then — for countries sharing a code (NANP +1, +7 RU/KZ) — the most specific area code, then the lowest priority (the primary country) when no area code matches. The default dataset is indexed for O(1) lookup; a custom list falls back to a linear scan.

Example

ts
findPhoneCountry('12025550123')?.iso2; // 'us'
findPhoneCountry('14165550123')?.iso2; // 'ca' (area code 416)
findPhoneCountry('12425550123')?.iso2; // 'bs' (area code 242)

Signature

ts
declare function findPhoneCountry(digits: string, countries?: readonly PhoneCountry[]): PhoneCountry | undefined;

Parameters

ParameterTypeDescription
digitsstringThe number's digits (no +/separators), e.g. '14165550123'
countries?readonly PhoneCountry[]The country list

Returns

PhoneCountry | undefinedThe matched country, or undefined