UNPKG

@fremtind/jkl-formatters-util

Version:
26 lines (25 loc) 1.05 kB
import { unicode } from "@fremtind/jkl-constants-util"; const TELEFONNUMMER_REGEX = { mobil: /^([8]\d{2})(\d{2})(\d{3})$/, fast: /^([2-9]\d)(\d{2})(\d{2})(\d{2})$/, mobilPartial: /^([8]\d{2})(\d{1,2})?(\d{1,3})?$/, fastPartial: /^([2-9]\d)(\d{1,2})?(\d{1,2})?(\d{1,2})?$/ }; function formatTelefonnummer(input, options) { const strippedInput = input.replace(/\W/g, ""); const mobilRegex = (options == null ? void 0 : options.partial) ? TELEFONNUMMER_REGEX.mobilPartial : TELEFONNUMMER_REGEX.mobil; const fastRegex = (options == null ? void 0 : options.partial) ? TELEFONNUMMER_REGEX.fastPartial : TELEFONNUMMER_REGEX.fast; const match = strippedInput.match(mobilRegex) || strippedInput.match(fastRegex); if (!match) { return input; } return [ (options == null ? void 0 : options.countryCode) ? "+".concat(options.countryCode) : void 0, ...match.slice(1) ].filter(Boolean).join(unicode.NON_BREAKING_SPACE); } export { TELEFONNUMMER_REGEX, formatTelefonnummer }; //# sourceMappingURL=formatTelefonnummer.js.map