84 lines
2.2 KiB
TypeScript
84 lines
2.2 KiB
TypeScript
import custom_utils from "./utils";
|
|
|
|
const {debugLog} = custom_utils
|
|
|
|
/**
|
|
* Class that helps to convert values into predefined formats
|
|
*/
|
|
export default class Formatters {
|
|
|
|
static convertToPhone(originalValue: string) {
|
|
/**
|
|
* nettoyer les numéros de téléphone en ne gardant que les nombres et le préfixe de pays
|
|
*/
|
|
// debugLog('originalValue', originalValue.substring(1))
|
|
if (!originalValue) {
|
|
originalValue = ''
|
|
}
|
|
let original_without_spaces = originalValue.replace(/ /g, '')
|
|
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', '')
|
|
|
|
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
|
|
}
|
|
if (add_prefix) {
|
|
convertedValue = `+33 ` + convertedValue
|
|
}
|
|
|
|
debugLog('phone: ', originalValue, '=>', convertedValue)
|
|
|
|
|
|
return "";
|
|
}
|
|
|
|
static convertToName(originalValue: string) {
|
|
|
|
if (originalValue && originalValue.length) {
|
|
|
|
let tab = originalValue.toLowerCase().split('')
|
|
tab[0] = originalValue[0].toUpperCase()
|
|
return tab.join('');
|
|
}
|
|
return ''
|
|
}
|
|
}
|