mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-29 07:53:23 +00:00
fix: Correction de l'affichage des numéros de téléphone [#41]
This commit is contained in:
@ -1,36 +1,49 @@
|
||||
|
||||
const localePrefixes = {
|
||||
"fr-FR": "+33",
|
||||
// Ajoutez d'autres locales et leurs préfixes ici
|
||||
};
|
||||
|
||||
|
||||
export function formatPhoneNumber(phoneString, fromFormat = 'XX-XX-XX-XX-XX', toFormat = 'LX-XX-XX-XX-XX', locale = "fr-FR") {
|
||||
/**
|
||||
*
|
||||
* @param {*} phoneString au format E.164
|
||||
* @param {*} toFormat L=Country code, X=Number Format="LX XX XX XX XX"
|
||||
* @returns
|
||||
*/
|
||||
export function formatPhoneNumber(phoneString, toFormat = 'LX XX XX XX XX') {
|
||||
if (!phoneString) return;
|
||||
// Extraire les chiffres du numéro de téléphone
|
||||
const digits = phoneString.replace(/\D/g, '');
|
||||
|
||||
// Déterminer le préfixe international en fonction de la locale
|
||||
|
||||
|
||||
let prefix = localePrefixes[locale] || '';
|
||||
|
||||
// Si le format d'entrée commence par 'L', détecter la locale
|
||||
if (fromFormat.startsWith('L')) {
|
||||
const detectedPrefix = phoneString.match(/^\+\d+/);
|
||||
if (detectedPrefix) {
|
||||
prefix = detectedPrefix[0];
|
||||
phoneString = phoneString.replace(prefix, '');
|
||||
}
|
||||
// Vérifier si le numéro est au format international
|
||||
if(!validateE164PhoneNumber(phoneString)){
|
||||
return phoneString;
|
||||
}
|
||||
const matches = phoneString.match(/^\+(\d{1,3})(\d{9})$/);
|
||||
if (!matches) return phoneString;
|
||||
|
||||
// Remplacer 'L' par le préfixe et 'X' par les chiffres du numéro de téléphone
|
||||
const [_, countryCode, number] = matches;
|
||||
const prefix = `+${countryCode}`;
|
||||
const digits = number;
|
||||
|
||||
// Initialiser le numéro formaté avec le préfixe
|
||||
let formattedNumber = toFormat.replace('L', prefix);
|
||||
|
||||
let digitIndex = 0;
|
||||
|
||||
formattedNumber = formattedNumber.replace(/X/g, () => {
|
||||
|
||||
// Remplacer les X par les chiffres
|
||||
formattedNumber = formattedNumber.replace(/X/g, (match, offset) => {
|
||||
// Si c'est le dernier groupe de X, mettre tous les chiffres restants
|
||||
if (offset === formattedNumber.lastIndexOf('X') - match.length + 1) {
|
||||
const remainingDigits = digits.substring(digitIndex);
|
||||
digitIndex += remainingDigits.length;
|
||||
return remainingDigits;
|
||||
}
|
||||
// Sinon, mettre un seul chiffre
|
||||
return digits[digitIndex++] || '';
|
||||
});
|
||||
|
||||
return formattedNumber;
|
||||
}
|
||||
|
||||
// Fonction pour valider un numéro de téléphone au format E.164
|
||||
// Le format E.164 est un format international pour les numéros de téléphone
|
||||
// qui commence par un signe '+' suivi du code pays et du numéro de téléphone
|
||||
// Par exemple : +33123456789 pour un numéro français
|
||||
export function validateE164PhoneNumber(phoneNumber) {
|
||||
const regEx = /^\+[1-9]\d{9,14}$/;
|
||||
return regEx.test(phoneNumber);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user