2025-01-15 22:20:14 +01:00
|
|
|
|
import * as fs from 'node:fs'
|
|
|
|
|
|
|
|
|
|
let show_debug = 0
|
|
|
|
|
// show_debug = 1
|
|
|
|
|
let output_folder = 'output';
|
|
|
|
|
const prefix_phone_fr_only = true
|
|
|
|
|
|
|
|
|
|
// console.log('----------------------show_debug', show_debug)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* wrapper de log qui se montre uniquemnt si show_debug a été activé
|
|
|
|
|
* @param args
|
|
|
|
|
*/
|
|
|
|
|
function debugLog(...args: any[]) {
|
|
|
|
|
if (show_debug) {
|
|
|
|
|
console.log('### debug: ',)
|
2025-04-17 17:34:39 +02:00
|
|
|
|
args.map((elem: any) => console.log(' - ', elem))
|
2025-01-15 22:20:14 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-11 15:53:23 +02:00
|
|
|
|
const truthyValues = [true, 'true', 'True', 'TRUE', '1', 'yes', 1]
|
|
|
|
|
const falsyValues = [false, 'false', 'False', 'FALSE', '0', 'no', 0]
|
2025-01-15 22:20:14 +01:00
|
|
|
|
|
|
|
|
|
let listOfBooleanKeys = [
|
|
|
|
|
"prise_type_ef",
|
|
|
|
|
"prise_type_2",
|
|
|
|
|
"prise_type_combo_ccs",
|
|
|
|
|
"prise_type_chademo",
|
|
|
|
|
"gratuit",
|
|
|
|
|
"paiement_acte",
|
|
|
|
|
"paiement_cb",
|
|
|
|
|
"cable_t2_attache"
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2025-04-11 15:53:23 +02:00
|
|
|
|
function boolToAddable(someBooleanValue: boolean) {
|
|
|
|
|
return someBooleanValue ? 1 : 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isTruthyValue(someValue: string) {
|
|
|
|
|
let convertedValue;
|
|
|
|
|
if (truthyValues.indexOf(someValue) !== -1) {
|
|
|
|
|
convertedValue = true
|
|
|
|
|
}
|
|
|
|
|
if (falsyValues.indexOf(someValue) !== -1) {
|
|
|
|
|
convertedValue = false
|
|
|
|
|
}
|
|
|
|
|
return convertedValue
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-15 22:20:14 +01:00
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param pointKeyName
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
|
|
|
|
function isBooleanKey(pointKeyName: string): boolean {
|
|
|
|
|
|
|
|
|
|
return listOfBooleanKeys.indexOf(pointKeyName) !== -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* crée un fichier dans le dossier par défaut, output
|
|
|
|
|
* @param fileName
|
|
|
|
|
* @param fileContent
|
|
|
|
|
*/
|
|
|
|
|
function writeFile(fileName: string, fileContent: any, outputPathOverride: string = '') {
|
|
|
|
|
|
|
|
|
|
if (outputPathOverride) {
|
|
|
|
|
output_folder = outputPathOverride
|
|
|
|
|
} else {
|
|
|
|
|
console.log('pas de output', outputPathOverride
|
|
|
|
|
)
|
|
|
|
|
}
|
2025-04-10 12:53:03 +02:00
|
|
|
|
let destination = `./${output_folder}/${fileName}`.replaceAll('//', '/');
|
2025-01-15 22:20:14 +01:00
|
|
|
|
console.log('write file ', destination)
|
|
|
|
|
return fs.writeFile(
|
|
|
|
|
destination,
|
|
|
|
|
fileContent,
|
|
|
|
|
'utf8',
|
|
|
|
|
(err) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.log(`Error writing file: ${err}`)
|
|
|
|
|
} else {
|
|
|
|
|
console.log(`File ${fileName} is written successfully!`)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-10 12:53:03 +02:00
|
|
|
|
/**
|
|
|
|
|
* trouve le max dans une string séparée par des points-virgules
|
|
|
|
|
* @param str
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
function find_max_in_string(str: string, separator: string = ';') {
|
|
|
|
|
let max = 0;
|
|
|
|
|
if (str.indexOf(separator) !== -1) {
|
|
|
|
|
let explode = str.split(separator);
|
|
|
|
|
explode.forEach((item: string) => {
|
|
|
|
|
// keep only the number
|
|
|
|
|
let value = parseInt(item.replaceAll(/[^0-9]/g, ''));
|
|
|
|
|
if (value && value > max) {
|
|
|
|
|
max = value;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return max;
|
|
|
|
|
}
|
|
|
|
|
return parseInt(str.replaceAll(/[^0-9]/g, ''));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* tronque une string à une longueur donnée
|
|
|
|
|
* @param str
|
|
|
|
|
* @param limit
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
function truncate_enums_to_limit(str: string, limit: number = 255) {
|
|
|
|
|
if (str.indexOf(';') !== -1) {
|
|
|
|
|
let elements = str.split(';');
|
|
|
|
|
let result: string[] = [];
|
|
|
|
|
let currentLength = 0;
|
|
|
|
|
|
|
|
|
|
for (let element of elements) {
|
|
|
|
|
// +1 pour le point-virgule qui sera ajouté
|
|
|
|
|
if (currentLength + element.length + 1 <= limit) {
|
|
|
|
|
result.push(element);
|
|
|
|
|
currentLength += element.length + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
let joined = result.join(';');
|
|
|
|
|
return joined;
|
|
|
|
|
}
|
|
|
|
|
if (str.length > limit) {
|
|
|
|
|
return str.substring(0, limit)
|
|
|
|
|
}
|
|
|
|
|
return str
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-28 22:24:41 +02:00
|
|
|
|
function convertToBoolean(originalValue: any): boolean {
|
|
|
|
|
if (truthyValues.indexOf(originalValue) !== -1) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (falsyValues.indexOf(originalValue) !== -1) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return false; // valeur par défaut
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function convertToYesOrNo(originalValue: any) {
|
|
|
|
|
let intermediateValue = '' + originalValue
|
|
|
|
|
let isEnumeration = false;
|
|
|
|
|
let convertedValue = '';
|
|
|
|
|
|
|
|
|
|
// handle lists with ; as separator
|
|
|
|
|
if (intermediateValue.includes(';')) {
|
|
|
|
|
isEnumeration = true;
|
|
|
|
|
intermediateValue = intermediateValue.split(';')[0]
|
|
|
|
|
}
|
|
|
|
|
// on ne peut pas conclure ce que l'on doit garder avec une liste
|
|
|
|
|
if (isEnumeration) {
|
|
|
|
|
this.stats.enumeration_detected++
|
|
|
|
|
return ''
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debugLog('convertProperty: ==========> original value', originalValue, intermediateValue)
|
|
|
|
|
if (truthyValues.indexOf(originalValue) !== -1) {
|
|
|
|
|
convertedValue = 'yes'
|
|
|
|
|
} else {
|
|
|
|
|
debugLog('convertProperty: ==========> !!! NOT in truthy values', originalValue)
|
|
|
|
|
}
|
|
|
|
|
if (falsyValues.indexOf(originalValue) !== -1) {
|
|
|
|
|
convertedValue = 'no'
|
|
|
|
|
} else {
|
|
|
|
|
debugLog('convertProperty: ==========> !!! NOT in falsy values', originalValue)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return convertedValue;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-15 22:20:14 +01:00
|
|
|
|
export default {
|
2025-04-11 15:53:23 +02:00
|
|
|
|
// debug tools
|
2025-01-15 22:20:14 +01:00
|
|
|
|
debugLog,
|
2025-04-11 15:53:23 +02:00
|
|
|
|
// typing
|
|
|
|
|
boolToAddable,
|
2025-01-15 22:20:14 +01:00
|
|
|
|
isBooleanKey,
|
2025-04-11 15:53:23 +02:00
|
|
|
|
isTruthyValue,
|
|
|
|
|
truthyValues,
|
|
|
|
|
falsyValues,
|
2025-04-28 22:24:41 +02:00
|
|
|
|
// booleans
|
|
|
|
|
convertToBoolean,
|
|
|
|
|
convertToYesOrNo,
|
2025-04-11 15:53:23 +02:00
|
|
|
|
// research
|
2025-04-10 12:53:03 +02:00
|
|
|
|
find_max_in_string,
|
2025-04-11 15:53:23 +02:00
|
|
|
|
// formatting
|
2025-04-10 12:53:03 +02:00
|
|
|
|
truncate_enums_to_limit,
|
2025-04-11 15:53:23 +02:00
|
|
|
|
|
2025-04-10 12:53:03 +02:00
|
|
|
|
prefix_phone_fr_only,
|
2025-04-11 15:53:23 +02:00
|
|
|
|
// file tools
|
|
|
|
|
writeFile,
|
2025-01-15 22:20:14 +01:00
|
|
|
|
}
|