mapping-geojson-osm/mappings/formatters.ts

87 lines
2.4 KiB
TypeScript
Raw Normal View History

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