Skip to content

Commit

Permalink
front: remove regex and use util function instead
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Greiner <[email protected]>
  • Loading branch information
louisgreiner committed Jan 28, 2025
1 parent fcd03d3 commit c8d9bd3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import { Duration } from 'utils/duration';
import { DEFAULT_TRAINRUN_FREQUENCIES, DEFAULT_TRAINRUN_FREQUENCY } from './consts';
import type MacroEditorState from './MacroEditorState';
import type { NodeIndexed } from './MacroEditorState';
import { createMacroNode, deleteMacroNodeByNgeId, updateMacroNode } from './utils';
import {
createMacroNode,
deleteMacroNodeByNgeId,
trainrunFrequencyFromLabel,
updateMacroNode,
} from './utils';
import type {
NetzgrafikDto,
NGEEvent,
Expand Down Expand Up @@ -209,7 +214,7 @@ const createTrainSchedulePayload = async ({
}

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
DEFAULT_DTO,
} from './consts';
import MacroEditorState, { type NodeIndexed } from './MacroEditorState';
import { deleteMacroNodeByDbId, getSavedMacroNodes } from './utils';
import { deleteMacroNodeByDbId, getSavedMacroNodes, trainrunFrequencyFromLabel } from './utils';
import {
type PortDto,
type TimeLockDto,
Expand Down Expand Up @@ -130,16 +130,6 @@ const castNodeToNge = (
),
});

/**
* Match a frequency label to a NGE TrainrunFrequency, or `null` if not handled.
*/
const trainrunFrequencyFromLabel = (label: string) => {
if (!label.startsWith('frequency::')) return null;
const n = parseInt(label.split('::', 2)[1], 10);
const frequency = DEFAULT_TRAINRUN_FREQUENCIES.find((freq) => freq.frequency === n);
return frequency ?? null;
};

/**
* NGE trainrun frequency is stored as OSRD labels (`"frequency::30"` or `"frequency::120"`).
* Update the current frequency if the new frequency is smaller.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'common/api/osrdEditoastApi';
import type { AppDispatch } from 'store';

import { DEFAULT_TRAINRUN_FREQUENCIES } from './consts';
import type MacroEditorState from './MacroEditorState';
import type { NodeIndexed } from './MacroEditorState';

Expand Down Expand Up @@ -161,3 +162,13 @@ export const getSavedMacroNodes = async (
}
return result;
};

/**
* Match a frequency label to a NGE TrainrunFrequency, or `null` if not handled.
*/
export const trainrunFrequencyFromLabel = (label: string) => {
if (!label.startsWith('frequency::')) return null;
const n = parseInt(label.split('::', 2)[1], 10);
const frequency = DEFAULT_TRAINRUN_FREQUENCIES.find((freq) => freq.frequency === n);
return frequency ?? null;
};

0 comments on commit c8d9bd3

Please sign in to comment.