2024-10-08 10:09:21 +02:00
|
|
|
import custom_utils from "./utils";
|
|
|
|
|
2024-10-15 18:43:52 +02:00
|
|
|
const {debugLog,prefix_phone_fr_only} = custom_utils
|
2024-10-08 10:09:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class that helps to convert values into predefined formats
|
|
|
|
*/
|
|
|
|
export default class Formatters {
|
|
|
|
|
2024-10-15 18:43:52 +02:00
|
|
|
static convertToPhone(originalValue: string) :string{
|
2024-10-08 10:09:21 +02:00
|
|
|
/**
|
|
|
|
* nettoyer les numéros de téléphone en ne gardant que les nombres et le préfixe de pays
|
|
|
|
*/
|
2024-10-15 18:43:52 +02:00
|
|
|
debugLog("convertToPhone:" , originalValue);
|
2024-10-08 10:09:21 +02:00
|
|
|
// debugLog('originalValue', originalValue.substring(1))
|
|
|
|
if (!originalValue) {
|
|
|
|
originalValue = ''
|
|
|
|
}
|
2024-10-15 18:43:52 +02:00
|
|
|
let original_without_spaces = originalValue.replace(' ', '')
|
2024-10-08 10:09:21 +02:00
|
|
|
let cleaned_value = `${original_without_spaces}`
|
|
|
|
cleaned_value = cleaned_value
|
|
|
|
.trim()
|
|
|
|
.replace('Stations-e', '')
|
|
|
|
.replace(/[a-zA-Zéèà]/ig, '')
|
|
|
|
.replace(/[\(\)\.\- ]/g, '')
|
|
|
|
let add_prefix = false;
|
|
|
|
if (
|
|
|
|
/^\d/.test(cleaned_value) &&
|
|
|
|
!/^\+33 /.test(original_without_spaces)
|
|
|
|
) {
|
|
|
|
add_prefix = true
|
|
|
|
}
|
|
|
|
cleaned_value = cleaned_value.replace('+33', '')
|
|
|
|
|
2024-10-15 18:43:52 +02:00
|
|
|
debugLog("convertToPhone: cleaned_value" , cleaned_value);
|
|
|
|
|
2024-10-08 10:09:21 +02:00
|
|
|
if (/^0/.test(cleaned_value)) {
|
|
|
|
cleaned_value = cleaned_value.substring(1)
|
|
|
|
}
|
|
|
|
let array_of_numbers = cleaned_value
|
|
|
|
.split('')
|
|
|
|
|
|
|
|
|
|
|
|
let ii = 0;
|
|
|
|
if (cleaned_value.length == 4) {
|
|
|
|
ii = 1
|
|
|
|
}
|
|
|
|
let convertedValue = ''
|
|
|
|
array_of_numbers.forEach((num: string) => {
|
|
|
|
if (ii % 2) {
|
|
|
|
convertedValue += ' ';
|
|
|
|
}
|
|
|
|
convertedValue += num;
|
|
|
|
ii++;
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
convertedValue = convertedValue.replace(' ', ' ').trim();
|
|
|
|
debugLog('convertedValue', convertedValue)
|
|
|
|
if (
|
|
|
|
/^\d/.test(convertedValue) &&
|
|
|
|
!/^\+33 /.test(convertedValue)
|
|
|
|
) {
|
|
|
|
add_prefix = true
|
|
|
|
}
|
2024-10-15 18:43:52 +02:00
|
|
|
if (add_prefix && prefix_phone_fr_only) {
|
2024-10-08 10:09:21 +02:00
|
|
|
convertedValue = `+33 ` + convertedValue
|
|
|
|
}
|
|
|
|
|
|
|
|
debugLog('phone: ', originalValue, '=>', convertedValue)
|
|
|
|
|
|
|
|
|
2024-10-15 18:43:52 +02:00
|
|
|
return ""+convertedValue;
|
2024-10-08 10:09:21 +02:00
|
|
|
}
|
2024-10-09 16:22:57 +02:00
|
|
|
|
|
|
|
static convertToName(originalValue: string) {
|
|
|
|
|
2024-10-09 16:34:52 +02:00
|
|
|
if (originalValue && originalValue.length) {
|
|
|
|
|
|
|
|
let tab = originalValue.toLowerCase().split('')
|
|
|
|
tab[0] = originalValue[0].toUpperCase()
|
|
|
|
return tab.join('');
|
|
|
|
}
|
|
|
|
return ''
|
2024-10-09 16:22:57 +02:00
|
|
|
}
|
2024-10-08 10:09:21 +02:00
|
|
|
}
|