Skip to content

Commit 6ff3e36

Browse files
committed
fix(timetables): fix null stoptime in trip sorter
The first stopTime for a trip was being used to sort the trips returned from the server. When that stopTime was null, the sorter was failing. This fix iterates over the stopTimes for each pair of trips until it finds one that exists for each pair and then it sorts on that index. fixes catalogueglobal/datatools-server#48
1 parent 6ae5163 commit 6ff3e36

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

lib/editor/util/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,22 @@ export function sortAndFilterTrips (
134134
? trips
135135
.filter(t => t.useFrequency === useFrequency) // filter out based on useFrequency
136136
.sort((a, b) => {
137-
if (!a.stopTimes[0].departureTime || !b.stopTimes[0].departureTime) {
137+
// There may be a case where a null value (skipped stop) appears first
138+
// in the list of stopTimes. In this case, we want to compare on the
139+
// first stopTime that exists for each pair of trips.
140+
let aStopTime, bStopTime
141+
let count = 0
142+
while (!aStopTime || !bStopTime) {
143+
aStopTime = a.stopTimes[count]
144+
bStopTime = b.stopTimes[count]
145+
count++
146+
}
147+
if (!aStopTime.departureTime || !bStopTime.departureTime) {
138148
return 0
139-
} else if (a.stopTimes[0].departureTime < b.stopTimes[0].departureTime) {
149+
} else if (aStopTime.departureTime < bStopTime.departureTime) {
140150
return -1
141151
} else if (
142-
a.stopTimes[0].departureTime > b.stopTimes[0].departureTime
152+
aStopTime.departureTime > bStopTime.departureTime
143153
) {
144154
return 1
145155
}

0 commit comments

Comments
 (0)