Skip to content

Commit

Permalink
front: throw error if no track range found in findTrackSectionOffset …
Browse files Browse the repository at this point in the history
…utils

Signed-off-by: Clara Ni <[email protected]>
  • Loading branch information
clarani committed Feb 3, 2025
1 parent f54e511 commit 0a33b13
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('findTrackSectionOffset', () => {
expect(result).toEqual({ track: 'track_2', offset: 1050 });
});

it('should not find the track offset if past cumulative sums', () => {
it('should throw an error if the given position on path is beyond the last position of the path', () => {
const trackRangesLengthCumulativeSums = [1000, 2000, 3000];
const trackRanges = [
{ track_section: 'track_0' },
Expand All @@ -74,13 +74,10 @@ describe('findTrackSectionOffset', () => {
] as TrackRange[];

const offsetOnPath = 3001;
const result = findTrackSectionOffset(
offsetOnPath,
trackRangesLengthCumulativeSums,
trackRanges
);

expect(result).toEqual(null);
expect(() =>
findTrackSectionOffset(offsetOnPath, trackRangesLengthCumulativeSums, trackRanges)
).toThrow('No track range found for the given position on path');
});

it('should correctly find the track offset if it is located on the first track range', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ const findTrackSectionOffset = (
(cumulativeSum) => positionOnPath <= cumulativeSum
);
const trackRange = trackRangesOnPath[index];
if (!trackRange) return null;

if (!trackRange) {
throw new Error('No track range found for the given position on path');
}

// compute offset
const inferiorSum = index > 0 ? tracksLengthCumulativeSums[index - 1] : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ const getPointOnPathCoordinates = (
trackRanges
);

const track = tracks[trackOffset!.track];
const track = tracks[trackOffset.track];

return getPointOnTrackCoordinates(track.geo, mToMm(track.length), trackOffset!.offset);
return getPointOnTrackCoordinates(track.geo, mToMm(track.length), trackOffset.offset);
};

export default getPointOnPathCoordinates;
3 changes: 0 additions & 3 deletions front/src/modules/powerRestriction/helpers/createPathStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const createPathStep = (
tracksLengthCumulativeSums,
pathProperties.trackSectionRanges
);
if (!trackOffset) return undefined;

const coordinates = getPointOnPathCoordinates(
tracksById,
Expand Down Expand Up @@ -85,8 +84,6 @@ export const createCutAtPathStep = (
pathProperties.trackSectionRanges
);

if (!trackOffset) return null;

const coordinatesAtCut = getPointOnPathCoordinates(
tracksById,
pathProperties.trackSectionRanges,
Expand Down

0 comments on commit 0a33b13

Please sign in to comment.