From d04ed9323fe8758ce4eca183bf387dd5eb2c88f6 Mon Sep 17 00:00:00 2001 From: Clara Ni Date: Wed, 10 Jul 2024 10:38:50 +0200 Subject: [PATCH] fix Mathieu's comment --- .../pathfinding/helpers/getPathVoltages.ts | 83 ++++++------------- 1 file changed, 27 insertions(+), 56 deletions(-) diff --git a/front/src/modules/pathfinding/helpers/getPathVoltages.ts b/front/src/modules/pathfinding/helpers/getPathVoltages.ts index ff91b3ec131..1a20b97b226 100644 --- a/front/src/modules/pathfinding/helpers/getPathVoltages.ts +++ b/front/src/modules/pathfinding/helpers/getPathVoltages.ts @@ -27,81 +27,52 @@ const getPathVoltages = ( if (!electrifications || !pathLength) return []; const boundaries = [...electrifications.boundaries, pathLength]; - const ranges: RangedValue[] = []; let start = 0; let currentVoltage: string = ''; electrifications.values.forEach((electrification, index) => { if (isNonElectrified(electrification)) { - // add the previous range if (currentVoltage) { - ranges.push({ - begin: start, - end: boundaries[index - 1], - value: currentVoltage, - }); + // non electrified range, we add the previous range which has a voltage + ranges.push({ begin: start, end: boundaries[index - 1], value: currentVoltage }); currentVoltage = ''; - start = index === 0 ? 0 : boundaries[index - 1]; + start = boundaries[index - 1] || 0; } - } - - if (!currentVoltage && isElectrification(electrification)) { - // add a non electrified range - if (index > 0) { - ranges.push({ - begin: start, - end: boundaries[index - 1], - value: '', - }); + } else if (isElectrification(electrification)) { + if (!currentVoltage) { + // electrified range, we add the previous range without electrification + if (index > 0) ranges.push({ begin: start, end: boundaries[index - 1], value: '' }); + start = boundaries[index - 1] || 0; + } else if (electrification.voltage !== currentVoltage) { + // electrified range, we add the previous range which has a different voltage + ranges.push({ begin: start, end: boundaries[index], value: currentVoltage }); + start = boundaries[index]; } currentVoltage = electrification.voltage; - start = index === 0 ? 0 : boundaries[index - 1]; - } - - // add the last range - if (index === electrifications.values.length - 1) { - ranges.push({ - begin: start, - end: pathLength, - value: currentVoltage, - }); - return; - } - - if (!currentVoltage) return; - - // if 2 electrifications not separated by a neutral section - if (isElectrification(electrification) && electrification.voltage !== currentVoltage) { - // add the previous range - ranges.push({ - begin: start, - end: boundaries[index], - value: currentVoltage, - }); - start = boundaries[index]; - currentVoltage = electrification.voltage; - return; - } - - // if electrification is a neutral section - if (isNeutralSection(electrification)) { - // add the range if the following range is not an electrification or has not the same voltage than currentVoltage + } else if (isNeutralSection(electrification)) { const nextElectrification = electrifications.values[index + 1]; if ( - !isElectrification(nextElectrification) || - (isElectrification(nextElectrification) && nextElectrification.voltage !== currentVoltage) + currentVoltage && + (!nextElectrification || + !isElectrification(nextElectrification) || + nextElectrification.voltage !== currentVoltage) ) { - ranges.push({ - begin: start, - end: boundaries[index - 1], - value: currentVoltage, - }); + // neutral range, we add the previous range with electrification if: + // - there is no next range + // - the next range is an electrification and the voltage is not the same than on the previous one + ranges.push({ begin: start, end: boundaries[index - 1], value: currentVoltage }); currentVoltage = ''; start = boundaries[index - 1]; } } + + if (index === electrifications.values.length - 1) { + // we add the last range + ranges.push({ begin: start, end: pathLength, value: currentVoltage }); + } }); + return ranges; };