@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
31 lines (30 loc) • 1.1 kB
TypeScript
/**
* Detect the country dialing code from a phone number string that has
* no separator between code and subscriber number.
*
* Uses a longest-prefix-match against every known E.164 country code
* in the Eufemia countries list.
*
* @param value A phone number string beginning with "+" or "00" (e.g. "+4712345678" or "004712345678").
* @returns An object with `countryCode` (e.g. "+47" or "+1-684" for
* dashed codes) and `phoneNumber` (e.g. "12345678"), or
* `undefined` when no known code could be detected.
*
* @example
* detectCountryCode('+4712345678')
* // => { countryCode: '+47', phoneNumber: '12345678' }
*
* detectCountryCode('004712345678')
* // => { countryCode: '+47', phoneNumber: '12345678' }
*
* detectCountryCode('+16841234567')
* // => { countryCode: '+1-684', phoneNumber: '1234567' }
*
* detectCountryCode('+hello')
* // => undefined
*/
export type DetectedCountryCode = {
countryCode: string;
phoneNumber: string;
};
export default function detectCountryCode(value: string): DetectedCountryCode | undefined;