Skip to content

Commit fd7b6a4

Browse files
committed
front: remove regex and use util function instead
Signed-off-by: Louis Greiner <[email protected]>
1 parent 87e7484 commit fd7b6a4

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

front/src/applications/operationalStudies/components/MacroEditor/ngeToOsrd.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ import { Duration } from 'utils/duration';
1313
import { DEFAULT_TRAINRUN_FREQUENCIES, DEFAULT_TRAINRUN_FREQUENCY } from './consts';
1414
import type MacroEditorState from './MacroEditorState';
1515
import type { NodeIndexed } from './MacroEditorState';
16-
import { createMacroNode, deleteMacroNodeByNgeId, updateMacroNode } from './utils';
16+
import {
17+
createMacroNode,
18+
deleteMacroNodeByNgeId,
19+
trainrunFrequencyFromLabel,
20+
updateMacroNode,
21+
} from './utils';
1722
import type {
1823
NetzgrafikDto,
1924
NGEEvent,
@@ -209,7 +214,7 @@ const createTrainSchedulePayload = async ({
209214
}
210215

211216
const trainScheduleLabels =
212-
trainSchedule?.labels?.filter((label) => label.match(/^frequency::(?!30$|60$|120$)\d+$/)) || [];
217+
trainSchedule?.labels?.filter((label) => trainrunFrequencyFromLabel(label) !== null) || [];
213218

214219
trainrunLabels = uniq([...trainrunLabels, ...trainScheduleLabels]);
215220

front/src/applications/operationalStudies/components/MacroEditor/osrdToNge.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
DEFAULT_DTO,
1919
} from './consts';
2020
import MacroEditorState, { type NodeIndexed } from './MacroEditorState';
21-
import { deleteMacroNodeByDbId, getSavedMacroNodes } from './utils';
21+
import { deleteMacroNodeByDbId, getSavedMacroNodes, trainrunFrequencyFromLabel } from './utils';
2222
import {
2323
type PortDto,
2424
type TimeLockDto,
@@ -130,16 +130,6 @@ const castNodeToNge = (
130130
),
131131
});
132132

133-
/**
134-
* Match a frequency label to a NGE TrainrunFrequency, or `null` if not handled.
135-
*/
136-
const trainrunFrequencyFromLabel = (label: string) => {
137-
if (!label.startsWith('frequency::')) return null;
138-
const n = parseInt(label.split('::', 2)[1], 10);
139-
const frequency = DEFAULT_TRAINRUN_FREQUENCIES.find((freq) => freq.frequency === n);
140-
return frequency ?? null;
141-
};
142-
143133
/**
144134
* NGE trainrun frequency is stored as OSRD labels (`"frequency::30"` or `"frequency::120"`).
145135
* Update the current frequency if the new frequency is smaller.

front/src/applications/operationalStudies/components/MacroEditor/utils.ts

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from 'common/api/osrdEditoastApi';
99
import type { AppDispatch } from 'store';
1010

11+
import { DEFAULT_TRAINRUN_FREQUENCIES } from './consts';
1112
import type MacroEditorState from './MacroEditorState';
1213
import type { NodeIndexed } from './MacroEditorState';
1314

@@ -161,3 +162,13 @@ export const getSavedMacroNodes = async (
161162
}
162163
return result;
163164
};
165+
166+
/**
167+
* Match a frequency label to a NGE TrainrunFrequency, or `null` if not handled.
168+
*/
169+
export const trainrunFrequencyFromLabel = (label: string) => {
170+
if (!label.startsWith('frequency::')) return null;
171+
const n = parseInt(label.split('::', 2)[1], 10);
172+
const frequency = DEFAULT_TRAINRUN_FREQUENCIES.find((freq) => freq.frequency === n);
173+
return frequency ?? null;
174+
};

0 commit comments

Comments
 (0)