Skip to content

Commit

Permalink
3rd level of trips segmenting: by line name ✅
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Dec 17, 2021
1 parent 3edac47 commit 281782b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
20 changes: 19 additions & 1 deletion lib/trips-list-segments-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,26 @@ const tripsListSegmentsFilters = (hafas, prevLineNameOrFahrtNr, prevOpts, prevTr
]
}

if (prevLineNameOrFahrtNr === '*') {
// `prevTrips` may exclude some lines's trips because it is clipped at
// `TRIPS_BY_NAME_MAX_RESULTS`, so we need to need to add a "catch-all"
// segment as well.
const lineNames = uniq(
prevTrips
.map(t => t.line && t.line.name)
.filter(lineName => !!lineName)
)
return [
...lineNames.map((lineName) => ({
lineNameOrFahrtNr: lineName,
opts: prevOpts,
})),
// todo: catch-all segment, but AFAIK there's no "negative" operator filter
]
}

// todo: how do we segment here?
throw new Error('3rd level of segmenting is not supported')
throw new Error('4th level of segmenting is not supported')
}

module.exports = tripsListSegmentsFilters
32 changes: 26 additions & 6 deletions test/trips-list-segments-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ const mockHafas = {
},
}

const withOp = opName => ({line: {operator: {name: opName}}})
const withLine = (lineName, opName) => ({
line: {
name: lineName,
operator: {name: opName},
},
})

const lineNameOrFahrtNr0 = '*'
const opts0 = {}
Expand All @@ -40,10 +45,10 @@ a.deepStrictEqual(segments1, [{
const lineNameOrFahrtNr1a = segments1[0].lineNameOrFahrtNr
const opts1a = segments1[0].opts
const trips1a = [
withOp('OP 1'),
withOp('op2'),
withOp('OP 1'),
withOp('op_3'),
withLine('line A', 'OP 1'),
withLine('line B', 'op2'),
withLine('line C', 'OP 1'),
withLine('line B', 'op_3'),
]
const segments1a2 = tripsListSegmentsFilters(mockHafas, lineNameOrFahrtNr1a, opts1a, trips1a)
a.deepStrictEqual(segments1a2, [{
Expand All @@ -59,6 +64,21 @@ a.deepStrictEqual(segments1a2, [{

const lineNameOrFahrtNr1a2a = segments1a2[0].lineNameOrFahrtNr
const opts1a2a = segments1a2[0].opts
a.throws(() => tripsListSegmentsFilters(mockHafas, lineNameOrFahrtNr1a2a, opts1a2a, []))
const trips1a2a = [
withLine('line A', 'OP 1'),
withLine('line C', 'OP 1'),
]
const segments1a2a3 = tripsListSegmentsFilters(mockHafas, lineNameOrFahrtNr1a2a, opts1a2a, trips1a2a)
a.deepStrictEqual(segments1a2a3, [{
lineNameOrFahrtNr: 'line A',
opts: opts1a2a,
}, {
lineNameOrFahrtNr: 'line C',
opts: opts1a2a,
}])

const lineNameOrFahrtNr1a2a3a = segments1a2a3[0].lineNameOrFahrtNr
const opts1a2a3a = segments1a2a3[0].opts
a.throws(() => tripsListSegmentsFilters(mockHafas, lineNameOrFahrtNr1a2a3a, opts1a2a3a, []))

console.info('tripsListSegmentsFilters seems to be working ✔︎')

0 comments on commit 281782b

Please sign in to comment.