UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

32 lines (31 loc) 1.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = detectCountryCode; var _countryCodes = _interopRequireDefault(require("./constants/countryCodes.js")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const strippedCodes = Array.from(new Set(_countryCodes.default.map(c => c.replace(/-/g, '')))).sort((a, b) => b.length - a.length || a.localeCompare(b)); const cdcFormatMap = Object.fromEntries(_countryCodes.default.map(c => [c.replace(/-/g, ''), c])); function detectCountryCode(value) { if (typeof value !== 'string') { return undefined; } if (value.startsWith('00')) { value = `+${value.slice(2)}`; } if (!value.startsWith('+')) { return undefined; } const digits = value.slice(1); for (const code of strippedCodes) { if (digits.startsWith(code) && digits.length > code.length) { return { countryCode: `+${cdcFormatMap[code] || code}`, phoneNumber: digits.slice(code.length) }; } } return undefined; } //# sourceMappingURL=detectCountryCode.js.map