feat: standardize contact formatting
This commit is contained in:
@@ -11,8 +11,8 @@ const PHONE_CHAR_PATTERN = /[^+\d]/g
|
||||
* Normalises a phone number by trimming whitespace, removing spacing/separators and
|
||||
* converting international prefixes written with `00` to their `+` variant.
|
||||
*/
|
||||
export const normalizePhone = (rawValue: string): string => {
|
||||
const trimmed = (rawValue || '').trim()
|
||||
export const normalizePhone = (rawValue: string | null | undefined): string => {
|
||||
const trimmed = typeof rawValue === 'string' ? rawValue.trim() : ''
|
||||
if (!trimmed) {
|
||||
return ''
|
||||
}
|
||||
@@ -26,30 +26,57 @@ export const normalizePhone = (rawValue: string): string => {
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a phone number by grouping digits by two while keeping any international
|
||||
* prefix. The function remains tolerant to partially entered numbers.
|
||||
* Formats a phone number by grouping digits by two and joining them with dots while
|
||||
* keeping any international prefix. The function remains tolerant to partially
|
||||
* entered numbers and returns an empty string for nullish inputs.
|
||||
*/
|
||||
export const formatPhone = (rawValue: string): string => {
|
||||
export const formatPhone = (rawValue: string | null | undefined): string => {
|
||||
if (rawValue == null) {
|
||||
return ''
|
||||
}
|
||||
|
||||
const normalized = normalizePhone(rawValue)
|
||||
if (!normalized) {
|
||||
return ''
|
||||
}
|
||||
|
||||
if (normalized.startsWith('+33')) {
|
||||
let nationalNumber = normalized.slice(3)
|
||||
if (nationalNumber.startsWith('0')) {
|
||||
nationalNumber = nationalNumber.slice(1)
|
||||
}
|
||||
|
||||
if (nationalNumber.length % 2 !== 0) {
|
||||
nationalNumber = `0${nationalNumber}`
|
||||
}
|
||||
|
||||
const groups = nationalNumber.match(/\d{1,2}/g) ?? []
|
||||
if (groups.length === 0) {
|
||||
return '+33'
|
||||
}
|
||||
|
||||
return ['+33', ...groups].join('.')
|
||||
}
|
||||
|
||||
if (/^0\d{9}$/.test(normalized)) {
|
||||
return normalized
|
||||
}
|
||||
|
||||
const hasInternationalPrefix = normalized.startsWith('+')
|
||||
const prefix = hasInternationalPrefix ? normalized.slice(0, 1) : ''
|
||||
const digits = hasInternationalPrefix ? normalized.slice(1) : normalized
|
||||
|
||||
const groups = digits.match(/.{1,2}/g) ?? []
|
||||
const grouped = groups.join(' ')
|
||||
const groups = digits.match(/\d{1,2}/g) ?? []
|
||||
const grouped = groups.join('.')
|
||||
|
||||
return prefix ? `${prefix}${grouped ? ' ' : ''}${grouped}` : grouped
|
||||
return prefix ? `${prefix}${grouped}` : grouped
|
||||
}
|
||||
|
||||
/**
|
||||
* Masks a phone number for display purposes by replacing the middle digits with ·.
|
||||
* Useful for UI fragments where the full number should not be exposed.
|
||||
*/
|
||||
export const maskPhone = (rawValue: string): string => {
|
||||
export const maskPhone = (rawValue: string | null | undefined): string => {
|
||||
const normalized = normalizePhone(rawValue)
|
||||
if (!normalized) {
|
||||
return ''
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
uniqueConstructeurIds,
|
||||
resolveConstructeurs,
|
||||
formatConstructeurContact,
|
||||
} from '~/shared/constructeurUtils'
|
||||
|
||||
const formatSize = (size) => {
|
||||
@@ -202,10 +203,11 @@ const normalizeCustomFields = (values = []) => {
|
||||
|
||||
const normalizeConstructeur = (constructeur) => {
|
||||
if (!constructeur) { return null }
|
||||
const contact = formatConstructeurContact(constructeur)
|
||||
return {
|
||||
id: constructeur.id || null,
|
||||
name: constructeur.name || '—',
|
||||
contact: [constructeur.email, constructeur.phone].filter(Boolean).join(' • ') || '—'
|
||||
contact: contact || '—'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user