appointments.js
1 month ago
attendees.js
1 month ago
colorManipulation.js
1 month ago
customer.js
1 month ago
date.js
3 weeks ago
defaultCustomize.js
3 weeks ago
employee.js
5 days ago
events.js
1 month ago
formFieldsTemplates.js
1 month ago
formatting.js
1 month ago
helper.js
5 days ago
image.js
1 month ago
integrationApple.js
1 month ago
integrationGoogle.js
1 month ago
integrationOutlook.js
1 month ago
integrationStripe.js
1 month ago
integrationZoom.js
1 month ago
licence.js
1 month ago
liveAnnouncer.js
3 weeks ago
phone.js
3 weeks ago
pricing.js
1 month ago
recurring.js
5 days ago
responsive.js
1 month ago
scrollElements.js
1 month ago
settings.js
1 month ago
translationsElementPlus.js
1 month ago
utilHeaderHeight.js
1 month ago
phone.js
54 lines
| 1 | import { parsePhoneNumberFromString } from 'libphonenumber-js' |
| 2 | |
| 3 | /** |
| 4 | * Returns phone value with country calling code (e.g. +381641234567). |
| 5 | * |
| 6 | * @param {string|number|null|undefined} value |
| 7 | * @param {import('maz-ui/components').MazInputPhoneNumberData|null|undefined} phoneData |
| 8 | * @param {string|undefined|null} countryCode |
| 9 | * @returns {string} |
| 10 | */ |
| 11 | export function getInternationalPhoneValue(value, phoneData, countryCode) { |
| 12 | const normalizedValue = value == null ? '' : String(value).trim() |
| 13 | |
| 14 | if (!normalizedValue && !phoneData?.nationalNumber) { |
| 15 | return '' |
| 16 | } |
| 17 | |
| 18 | if (phoneData?.e164) { |
| 19 | return phoneData.e164 |
| 20 | } |
| 21 | |
| 22 | if (phoneData?.formatInternational) { |
| 23 | return phoneData.formatInternational.replace(/\s+/g, '') |
| 24 | } |
| 25 | |
| 26 | if (normalizedValue.startsWith('+')) { |
| 27 | return normalizedValue.replace(/\s+/g, '') |
| 28 | } |
| 29 | |
| 30 | const country = (phoneData?.countryCode || phoneData?.parsedCountryCode || countryCode || '') |
| 31 | .toString() |
| 32 | .toUpperCase() |
| 33 | |
| 34 | if (country && normalizedValue) { |
| 35 | const parsed = parsePhoneNumberFromString(normalizedValue, country) |
| 36 | |
| 37 | if (parsed) { |
| 38 | return parsed.format('E.164') |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | if (phoneData?.countryCallingCode && phoneData?.nationalNumber) { |
| 43 | return `+${phoneData.countryCallingCode}${phoneData.nationalNumber}` |
| 44 | } |
| 45 | |
| 46 | if (phoneData?.countryCallingCode && normalizedValue) { |
| 47 | const nationalDigits = normalizedValue.replace(/\D/g, '') |
| 48 | |
| 49 | return nationalDigits ? `+${phoneData.countryCallingCode}${nationalDigits}` : '' |
| 50 | } |
| 51 | |
| 52 | return normalizedValue |
| 53 | } |
| 54 |