parseJourneyLeg applyRemarks: handle legs without stopovers 🐛

This commit is contained in:
Jannis R 2020-11-15 14:42:46 +01:00
parent 48ef7b5a5d
commit 66ff661767
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
5 changed files with 9026 additions and 11 deletions

View file

@ -13,7 +13,9 @@ const applyRemarks = (leg, refs) => {
for (let [remark, ref] of findRemarks(refs)) { for (let [remark, ref] of findRemarks(refs)) {
const {fromLocation, toLocation} = ref const {fromLocation, toLocation} = ref
let fromI = 0, toI = leg.stopovers.length - 1 let wholeLeg = true, fromI = 0, toI = 0
if (Array.isArray(leg.stopovers)) {
toI = leg.stopovers.length - 1
// this fails if `s.stop` is a new object (not reference-equal) // this fails if `s.stop` is a new object (not reference-equal)
// todo: do this index- or ID-based // todo: do this index- or ID-based
if (fromLocation) { if (fromLocation) {
@ -24,8 +26,9 @@ const applyRemarks = (leg, refs) => {
toI = leg.stopovers.findIndex(s => s.stop === toLocation) toI = leg.stopovers.findIndex(s => s.stop === toLocation)
if (toI < 0) continue if (toI < 0) continue
} }
wholeLeg = fromI === 0 && toI === (leg.stopovers.length - 1)
}
const wholeLeg = fromI === 0 && toI === (leg.stopovers.length - 1)
if (wholeLeg) addRemark(leg, remark) if (wholeLeg) addRemark(leg, remark)
else { else {
for (let i = fromI; i <= toI; i++) { for (let i = fromI; i <= toI; i++) {

38
test/db-journey-2.js Normal file
View file

@ -0,0 +1,38 @@
'use strict'
const test = require('tape')
const createClient = require('..')
const rawProfile = require('../p/db')
const res = require('./fixtures/db-journey-2.json')
const expected = require('./fixtures/db-journey-2.js')
const client = createClient(rawProfile, 'public-transport/hafas-client:test')
const {profile} = client
const opt = {
results: 4,
via: null,
stopovers: true,
transfers: -1,
transferTime: 0,
accessibility: 'none',
bike: false,
tickets: true,
polylines: true,
remarks: true,
walkingSpeed: 'normal',
startWithWalking: true,
scheduledDays: false,
departure: '2020-11-16T10:00:00+01:00',
products: {}
}
test('parses a journey remarks without failing', (t) => {
const common = profile.parseCommon({profile, opt, res})
const ctx = {profile, opt, common, res}
const journey = profile.parseJourney(ctx, res.outConL[2])
t.deepEqual(journey, expected)
t.end()
})

1126
test/fixtures/db-journey-2.js vendored Normal file

File diff suppressed because it is too large Load diff

7847
test/fixtures/db-journey-2.json vendored Normal file

File diff suppressed because it is too large Load diff

View file

@ -7,6 +7,7 @@ require('./db-stop')
require('./insa-stop') require('./insa-stop')
require('./bvg-journey') require('./bvg-journey')
require('./db-journey') require('./db-journey')
require('./db-journey-2')
require('./db-journey-polyline') require('./db-journey-polyline')
require('./db-arrivals') require('./db-arrivals')
require('./vbb-departures') require('./vbb-departures')