minotor 12.0.0 → 12.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -85,7 +85,7 @@ export class Route {
85
85
  * ...
86
86
  * }
87
87
  */
88
- private readonly stopIndices: Map<StopId, StopRouteIndex[]>;
88
+ private readonly stopIndices: Map<StopId, StopRouteIndex | StopRouteIndex[]>;
89
89
  /**
90
90
  * The identifier of the route as a service shown to users.
91
91
  */
@@ -115,15 +115,17 @@ export class Route {
115
115
  this.serviceRouteId = serviceRouteId;
116
116
  this.nbStops = stops.length;
117
117
  this.nbTrips = this.stopTimes.length / (this.stops.length * 2);
118
- this.stopIndices = new Map<StopId, StopRouteIndex[]>();
118
+ this.stopIndices = new Map<StopId, StopRouteIndex | StopRouteIndex[]>();
119
119
  for (let i = 0; i < stops.length; i++) {
120
120
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
121
121
  const stopId = stops[i]!;
122
122
  const existingIndices = this.stopIndices.get(stopId);
123
- if (existingIndices) {
124
- existingIndices.push(i);
123
+ if (existingIndices === undefined) {
124
+ this.stopIndices.set(stopId, i);
125
+ } else if (typeof existingIndices === 'number') {
126
+ this.stopIndices.set(stopId, [existingIndices, i]);
125
127
  } else {
126
- this.stopIndices.set(stopId, [i]);
128
+ existingIndices.push(i);
127
129
  }
128
130
  }
129
131
  }
@@ -471,9 +473,21 @@ export class Route {
471
473
  if (stopIndex === undefined) {
472
474
  return [];
473
475
  }
476
+ if (typeof stopIndex === 'number') {
477
+ // Preserve stable, mutable array results for callers that request all indices.
478
+ const indices = [stopIndex];
479
+ this.stopIndices.set(stopId, indices);
480
+ return indices;
481
+ }
474
482
  return stopIndex;
475
483
  }
476
484
 
485
+ /** Returns the first occurrence without allocating an array. */
486
+ public firstStopRouteIndex(stopId: StopId): StopRouteIndex | undefined {
487
+ const indices = this.stopIndices.get(stopId);
488
+ return typeof indices === 'number' ? indices : indices?.[0];
489
+ }
490
+
477
491
  /**
478
492
  * Retrieves the id of a stop at a given index in a route.
479
493
  * @param stopRouteIndex The route index of the stop.
@@ -300,7 +300,7 @@ export class Timetable {
300
300
  if (!serviceRoute || !transportModes.has(serviceRoute.type)) continue;
301
301
  }
302
302
 
303
- const originStopIndex = route.stopRouteIndices(originStop)[0]!;
303
+ const originStopIndex = route.firstStopRouteIndex(originStop)!;
304
304
  const existingHopOnStopIndex = reachableRoutes.get(route);
305
305
  if (existingHopOnStopIndex !== undefined) {
306
306
  if (originStopIndex < existingHopOnStopIndex) {