@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
25 lines • 871 B
JavaScript
import countryCodes from "./constants/countryCodes.js";
const strippedCodes = Array.from(new Set(countryCodes.map(c => c.replace(/-/g, '')))).sort((a, b) => b.length - a.length || a.localeCompare(b));
const cdcFormatMap = Object.fromEntries(countryCodes.map(c => [c.replace(/-/g, ''), c]));
export default 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