mirror of
https://forge.chapril.org/tykayn/wololo
synced 2025-06-20 01:34:42 +02:00
130 lines
3 KiB
TypeScript
130 lines
3 KiB
TypeScript
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: ',)
|
||
args.map((elem: any) => console.log(' ', elem))
|
||
}
|
||
}
|
||
|
||
|
||
let listOfBooleanKeys = [
|
||
"prise_type_ef",
|
||
"prise_type_2",
|
||
"prise_type_combo_ccs",
|
||
"prise_type_chademo",
|
||
"gratuit",
|
||
"paiement_acte",
|
||
"paiement_cb",
|
||
"cable_t2_attache"
|
||
]
|
||
|
||
|
||
/**
|
||
*
|
||
* @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
|
||
)
|
||
}
|
||
let destination = `./${output_folder}/${fileName}`.replaceAll('//', '/');
|
||
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!`)
|
||
}
|
||
}
|
||
)
|
||
}
|
||
|
||
/**
|
||
* 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
|
||
}
|
||
|
||
export default {
|
||
debugLog,
|
||
isBooleanKey,
|
||
writeFile,
|
||
find_max_in_string,
|
||
truncate_enums_to_limit,
|
||
prefix_phone_fr_only,
|
||
}
|