Skip to content

Commit

Permalink
fix Mathieu's comment
Browse files Browse the repository at this point in the history
  • Loading branch information
clarani committed Jul 10, 2024
1 parent 63a9b1f commit d04ed93
Showing 1 changed file with 27 additions and 56 deletions.
83 changes: 27 additions & 56 deletions front/src/modules/pathfinding/helpers/getPathVoltages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down

0 comments on commit d04ed93

Please sign in to comment.