Skip to content

Commit

Permalink
front: use matchPathStepAndOp() in pathStepMatchesOp()
Browse files Browse the repository at this point in the history
The logic in matchPathStepAndOp() is more general.
pathStepMatchesOp() performs some special checks on kp/name/opId
so retain these.

Also need to adapt the argument type for matchPathStepAndOp() to
only pick the properties we really need.

Signed-off-by: Simon Ser <contact@emersion.fr>
  • Loading branch information
emersion committed Oct 18, 2024
1 parent b10f859 commit c53e268
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions front/src/modules/pathfinding/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export const formatSuggestedOperationalPoints = (
coordinates: getPointCoordinates(geometry, pathLength, op.position),
}));

export const matchPathStepAndOp = (step: PathStep, op: SuggestedOP) => {
export const matchPathStepAndOp = (
step: PathStep,
op: Pick<SuggestedOP, 'opId' | 'uic' | 'ch' | 'trigram' | 'track' | 'offsetOnTrack'>
) => {
if ('operational_point' in step) {
return step.operational_point === op.opId;
}
Expand Down Expand Up @@ -155,15 +158,22 @@ export const upsertPathStepsInOPs = (ops: SuggestedOP[], pathSteps: PathStep[]):

export const pathStepMatchesOp = (
pathStep: PathStep,
op: Pick<SuggestedOP, 'uic' | 'ch' | 'kp' | 'name' | 'opId'>,
op: Pick<
SuggestedOP,
'opId' | 'uic' | 'ch' | 'trigram' | 'track' | 'offsetOnTrack' | 'name' | 'kp'
>,
withKP = false
) =>
('uic' in pathStep &&
'ch' in pathStep &&
pathStep.uic === op.uic &&
pathStep.ch === op.ch &&
(withKP ? pathStep.kp === op.kp : pathStep.name === op.name)) ||
pathStep.id === op.opId;
) => {
if (!matchPathStepAndOp(pathStep, op)) {
// TODO: we abuse the PathStep.id field here, the backend also sets it to an
// ID which has nothing to do with OPs
return pathStep.id === op.opId;
}
if ('uic' in pathStep) {
return withKP ? pathStep.kp === op.kp : pathStep.name === op.name;
}
return true;
};

/**
* Check if a suggested operational point is a via.
Expand All @@ -175,7 +185,10 @@ export const pathStepMatchesOp = (
*/
export const isVia = (
vias: PathStep[],
op: Pick<SuggestedOP, 'uic' | 'ch' | 'kp' | 'name' | 'opId'>,
op: Pick<
SuggestedOP,
'opId' | 'uic' | 'ch' | 'trigram' | 'track' | 'offsetOnTrack' | 'name' | 'kp'
>,
{ withKP = false } = {}
) => vias.some((via) => pathStepMatchesOp(via, op, withKP));

Expand Down

0 comments on commit c53e268

Please sign in to comment.