add tests and utils : limit values, get max in enums

This commit is contained in:
Tykayn 2025-04-10 12:53:03 +02:00 committed by tykayn
parent aa35803a0b
commit b4c28335b2
10 changed files with 153 additions and 43 deletions

View file

@ -54,7 +54,7 @@ function writeFile(fileName: string, fileContent: any, outputPathOverride: strin
console.log('pas de output', outputPathOverride
)
}
let destination = `./${output_folder}/${fileName}`.replace('//', '/');
let destination = `./${output_folder}/${fileName}`.replaceAll('//', '/');
console.log('write file ', destination)
return fs.writeFile(
destination,
@ -70,9 +70,61 @@ function writeFile(fileName: string, fileContent: any, outputPathOverride: strin
)
}
/**
* 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,
prefix_phone_fr_only
find_max_in_string,
truncate_enums_to_limit,
prefix_phone_fr_only,
}