diff --git a/docs/journeys.md b/docs/journeys.md index 550a7c15..6036c8d5 100644 --- a/docs/journeys.md +++ b/docs/journeys.md @@ -41,6 +41,7 @@ With `opt`, you can override the default options, which look like this: ```js { when: new Date(), + whenRepresents: 'departure', // use 'arrival' for journeys arriving before `when` earlierThan: null, // ref to get journeys earlier than the last query laterThan: null, // ref to get journeys later than the last query results: 5, // how many journeys? diff --git a/index.js b/index.js index 7ed498ee..9c81ce8b 100644 --- a/index.js +++ b/index.js @@ -79,6 +79,7 @@ const createClient = (profile, request = _request) => { results: 5, // how many journeys? via: null, // let journeys pass this station? passedStations: false, // return stations on the way? + whenRepresents: 'departure', // use 'arrival' for journeys arriving before `when` transfers: 5, // maximum of 5 transfers transferTime: 0, // minimum time for a single transfer in minutes // todo: does this work with every endpoint? @@ -90,6 +91,10 @@ const createClient = (profile, request = _request) => { if (opt.via) opt.via = profile.formatLocation(profile, opt.via) opt.when = opt.when || new Date() + if (opt.whenRepresents !== 'departure' && opt.whenRepresents !== 'arrival') { + throw new Error('opt.whenRepresents must be `departure` or `arrival`.') + } + const filters = [ profile.formatProducts(opt.products || {}) ] @@ -122,10 +127,10 @@ const createClient = (profile, request = _request) => { arrLocL: [to], jnyFltrL: filters, getTariff: !!opt.tickets, + outFrwd: opt.whenRepresents !== 'arrival', // todo: what is req.gisFltrL? getPT: true, // todo: what is this? - outFrwd: true, // todo: what is this? getIV: false, // todo: walk & bike as alternatives? getPolyline: !!opt.polylines } diff --git a/test/vbb.js b/test/vbb.js index 778c432b..5ac5a7f8 100644 --- a/test/vbb.js +++ b/test/vbb.js @@ -168,6 +168,21 @@ test('journeys – fails with no product', co(function* (t) { } })) +test('journeys – with arrival time', co(function* (t) { + const journeys = yield client.journeys(spichernstr, bismarckstr, { + results: 3, + when, + whenRepresents: 'arrival' + }) + + for (let j of journeys) { + const arr = +new Date(j.arrival) + t.ok(arr <= when) + } + + t.end() +})) + test('earlier/later journeys', co(function* (t) { const model = yield client.journeys(spichernstr, bismarckstr, { results: 3, when