wololo/mappings/utils.ts

224 lines
5.8 KiB
TypeScript
Raw Normal View History

2025-01-15 22:20:14 +01:00
import * as fs from 'node:fs'
import fetch from 'node-fetch';
2025-01-15 22:20:14 +01:00
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 é 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
}
}
const truthyValues = [true, 'true', '1', 'yes', 1]
const falsyValues = [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 + ''.toLowerCase()) !== -1) {
2025-04-11 15:53:23 +02:00
convertedValue = true
}
if (falsyValues.indexOf(someValue + ''.toLowerCase()) !== -1) {
2025-04-11 15:53:23 +02:00
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
)
}
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!`)
}
}
)
}
/**
* 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 + ''.toLowerCase()) !== -1) {
2025-04-28 22:24:41 +02:00
return true;
}
if (falsyValues.indexOf(originalValue + ''.toLowerCase()) !== -1) {
2025-04-28 22:24:41 +02:00
return false;
}
return false; // valeur par défaut
}
function convertToYesOrNo(originalValue: any): string {
2025-04-28 22:24:41 +02:00
let intermediateValue = '' + originalValue
let convertedValue = '';
// handle lists with ; as separator
// on ne peut pas conclure ce que l'on doit garder avec une liste
if (intermediateValue.indexOf(';') !== -1) {
2025-04-28 22:24:41 +02:00
return ''
}
intermediateValue = intermediateValue.split(';')[0]
2025-04-28 22:24:41 +02:00
debugLog('convertProperty: ==========> original value', originalValue, intermediateValue)
if (truthyValues.indexOf((originalValue + '').toLowerCase()) !== -1) {
return 'yes'
2025-04-28 22:24:41 +02:00
} else {
debugLog('convertProperty: ==========> !!! NOT in truthy values', originalValue)
}
if (falsyValues.indexOf((originalValue + '').toLowerCase()) !== -1) {
return 'no'
2025-04-28 22:24:41 +02:00
} else {
debugLog('convertProperty: ==========> !!! NOT in falsy values', originalValue)
}
return convertedValue;
}
async function replaceFile(sourceFilePathGeoJson: string, url: string) {
const response = await fetch(url)
if (!response.ok) {
throw new Error(`Erreur lors du téléchargement: ${response.status} ${response.statusText}`)
}
// afficher la taille du fichier téléchargé
const contentLength = response.headers.get('content-length')
const data = await response.text()
if (contentLength) {
const sizeInMB = (parseInt(contentLength) / (1024 * 1024)).toFixed(2)
console.log(`Taille du fichier téléchargé: ${sizeInMB} Mo`)
} else {
// mesurer la taille des données
const sizeInMB = (data.length / (1024 * 1024)).toFixed(2)
console.log(`Taille des données: ${sizeInMB} Mo`)
}
fs.writeFileSync(sourceFilePathGeoJson, data)
console.log('fichier téléchargé avec succès:', sourceFilePathGeoJson)
}
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
find_max_in_string,
2025-04-11 15:53:23 +02:00
// formatting
truncate_enums_to_limit,
2025-04-11 15:53:23 +02:00
prefix_phone_fr_only,
2025-04-11 15:53:23 +02:00
// file tools
writeFile,
replaceFile,
2025-01-15 22:20:14 +01:00
}