diff --git a/docs/journeys.md b/docs/journeys.md index 016f052c..b566edb3 100644 --- a/docs/journeys.md +++ b/docs/journeys.md @@ -40,7 +40,10 @@ With `opt`, you can override the default options, which look like this: ```js { - when: new Date(), + // Use either `departure` or `arrival` to specify a date/time. + departure: new Date(), + arrival: null, + 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 69f831ea..1db64568 100644 --- a/index.js +++ b/index.js @@ -62,15 +62,18 @@ const createClient = (profile, request = _request) => { to = profile.formatLocation(profile, to, 'to') if (('earlierThan' in opt) && ('laterThan' in opt)) { - throw new Error('opt.laterThan and opt.laterThan are mutually exclusive.') + throw new Error('opt.earlierThan and opt.laterThan are mutually exclusive.') + } + if (('departure' in opt) && ('arrival' in opt)) { + throw new Error('opt.departure and opt.arrival are mutually exclusive.') } let journeysRef = null if ('earlierThan' in opt) { if (!isNonEmptyString(opt.earlierThan)) { throw new Error('opt.earlierThan must be a non-empty string.') } - if ('when' in opt) { - throw new Error('opt.earlierThan and opt.when are mutually exclusive.') + if (('departure' in opt) || ('arrival' in opt)) { + throw new Error('opt.earlierThan and opt.departure/opt.arrival are mutually exclusive.') } journeysRef = opt.earlierThan } @@ -78,8 +81,8 @@ const createClient = (profile, request = _request) => { if (!isNonEmptyString(opt.laterThan)) { throw new Error('opt.laterThan must be a non-empty string.') } - if ('when' in opt) { - throw new Error('opt.laterThan and opt.when are mutually exclusive.') + if (('departure' in opt) || ('arrival' in opt)) { + throw new Error('opt.laterThan and opt.departure/opt.arrival are mutually exclusive.') } journeysRef = opt.laterThan } @@ -97,8 +100,19 @@ const createClient = (profile, request = _request) => { polylines: false // return leg shapes? }, opt) if (opt.via) opt.via = profile.formatLocation(profile, opt.via, 'opt.via') - opt.when = new Date(opt.when || Date.now()) - if (Number.isNaN(+opt.when)) throw new Error('opt.when is invalid') + + if (opt.when !== undefined) { + throw new Error('opt.when is not supported anymore. Use opt.departure/opt.arrival.') + } + let when = new Date(), outFrwd = true + if (opt.departure !== undefined && opt.departure !== null) { + when = new Date(opt.departure) + if (Number.isNaN(+when)) throw new Error('opt.departure is invalid') + } else if (opt.arrival !== undefined && opt.arrival !== null) { + when = new Date(opt.arrival) + if (Number.isNaN(+when)) throw new Error('opt.arrival is invalid') + outFrwd = false + } const filters = [ profile.formatProductsFilter(opt.products || {}) @@ -132,10 +146,10 @@ const createClient = (profile, request = _request) => { arrLocL: [to], jnyFltrL: filters, getTariff: !!opt.tickets, + outFrwd, // 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 } @@ -172,7 +186,7 @@ const createClient = (profile, request = _request) => { }) } - return more(opt.when, journeysRef) + return more(when, journeysRef) } const locations = (query, opt = {}) => { diff --git a/test/db.js b/test/db.js index cf605f6b..328171f9 100644 --- a/test/db.js +++ b/test/db.js @@ -76,7 +76,7 @@ const regensburgHbf = '8000309' test('journeys – Berlin Schwedter Str. to München Hbf', co(function* (t) { const journeys = yield client.journeys(blnSchwedterStr, münchenHbf, { - results: 3, when, passedStations: true + results: 3, departure: when, passedStations: true }) yield testJourneysStationToStation({ @@ -116,7 +116,8 @@ test('Berlin Schwedter Str. to Torfstraße 17', co(function* (t) { longitude: 13.3491223 } const journeys = yield client.journeys(blnSchwedterStr, torfstr, { - results: 3, when + results: 3, + departure: when }) yield testJourneysStationToAddress({ @@ -138,7 +139,8 @@ test('Berlin Schwedter Str. to ATZE Musiktheater', co(function* (t) { longitude: 13.350437 } const journeys = yield client.journeys(blnSchwedterStr, atze, { - results: 3, when + results: 3, + departure: when }) yield testJourneysStationToPoi({ @@ -157,7 +159,7 @@ test('journeys: via works – with detour', co(function* (t) { const journeys = yield client.journeys(westhafen, wedding, { via: württembergallee, results: 1, - when, + departure: when, passedStations: true }) @@ -186,7 +188,7 @@ test('earlier/later journeys, Jungfernheide -> München Hbf', co(function* (t) { test('journey leg details', co(function* (t) { const journeys = yield client.journeys(berlinHbf, münchenHbf, { - results: 1, when + results: 1, departure: when }) const p = journeys[0].legs[0] diff --git a/test/insa.js b/test/insa.js index af3ca98e..9b3facce 100644 --- a/test/insa.js +++ b/test/insa.js @@ -42,7 +42,9 @@ const dessau = '008010077' test('journeys – Magdeburg Hbf to Magdeburg-Buckau', co(function* (t) { const journeys = yield client.journeys(magdeburgHbf, magdeburgBuckau, { - results: 3, when, passedStations: true + results: 3, + departure: when, + passedStations: true }) yield testJourneysStationToStation({ @@ -78,7 +80,8 @@ test('Magdeburg Hbf to 39104 Magdeburg, Sternstr. 10', co(function*(t) { } const journeys = yield client.journeys(magdeburgHbf, sternStr, { - results: 3, when + results: 3, + departure: when }) yield testJourneysStationToAddress({ @@ -100,7 +103,8 @@ test('Magdeburg Hbf to Kloster Unser Lieben Frauen', co(function*(t) { longitude: 11.636437 } const journeys = yield client.journeys(magdeburgHbf, kloster, { - results: 3, when + results: 3, + departure: when }) yield testJourneysStationToPoi({ @@ -120,7 +124,7 @@ test('journeys: via works – with detour', co(function* (t) { const journeys = yield client.journeys(hasselbachplatzSternstrasse, stendal, { via: dessau, results: 1, - when, + departure: when, passedStations: true }) @@ -149,7 +153,7 @@ test('earlier/later journeys', co(function* (t) { test('journey leg details', co(function* (t) { const journeys = yield client.journeys(magdeburgHbf, magdeburgBuckau, { - results: 1, when + results: 1, departure: when }) const p = journeys[0].legs[0] diff --git a/test/lib/earlier-later-journeys.js b/test/lib/earlier-later-journeys.js index bdf4eb1d..aa6da9d8 100644 --- a/test/lib/earlier-later-journeys.js +++ b/test/lib/earlier-later-journeys.js @@ -13,7 +13,7 @@ const testEarlierLaterJourneys = co(function* (cfg) { } = cfg const model = yield fetchJourneys(fromId, toId, { - results: 3, when + results: 3, departure: when }) // todo: move to journeys validator? @@ -22,17 +22,31 @@ const testEarlierLaterJourneys = co(function* (cfg) { t.equal(typeof model.laterRef, 'string') t.ok(model.laterRef) - // when and earlierThan/laterThan should be mutually exclusive + // departure/arrival and earlierThan/laterThan should be mutually exclusive t.throws(() => { fetchJourneys(fromId, toId, { - when, earlierThan: model.earlierRef + departure: when, earlierThan: model.earlierRef }) // silence rejections, we're only interested in exceptions .catch(() => {}) }) t.throws(() => { fetchJourneys(fromId, toId, { - when, laterThan: model.laterRef + departure: when, laterThan: model.laterRef + }) + // silence rejections, we're only interested in exceptions + .catch(() => {}) + }) + t.throws(() => { + fetchJourneys(fromId, toId, { + arrival: when, earlierThan: model.earlierRef + }) + // silence rejections, we're only interested in exceptions + .catch(() => {}) + }) + t.throws(() => { + fetchJourneys(fromId, toId, { + arrival: when, laterThan: model.laterRef }) // silence rejections, we're only interested in exceptions .catch(() => {}) diff --git a/test/lib/journeys-fails-with-no-product.js b/test/lib/journeys-fails-with-no-product.js index b4ae96f4..f3ee4de8 100644 --- a/test/lib/journeys-fails-with-no-product.js +++ b/test/lib/journeys-fails-with-no-product.js @@ -14,7 +14,7 @@ const journeysFailsWithNoProduct = (cfg) => { for (let p of products) productsObj[p.id] = false t.throws(() => { - client.journeys(fromId, toId, {when, products}) + client.journeys(fromId, toId, {departure: when, products}) // silence rejections, we're only interested in exceptions .catch(() => {}) }) diff --git a/test/nahsh.js b/test/nahsh.js index 89a99a2c..5648f972 100644 --- a/test/nahsh.js +++ b/test/nahsh.js @@ -67,7 +67,9 @@ const schleswig = '8005362' test('journeys – Kiel Hbf to Flensburg', co(function* (t) { const journeys = yield client.journeys(kielHbf, flensburg, { - results: 3, when, passedStations: true + results: 3, + departure: when, + passedStations: true }) yield testJourneysStationToStation({ @@ -109,7 +111,8 @@ test('Kiel Hbf to Husum, Zingel 10', co(function* (t) { longitude: 9.050798 } const journeys = yield client.journeys(kielHbf, zingel, { - results: 3, when + results: 3, + departure: when }) yield testJourneysStationToAddress({ @@ -131,7 +134,8 @@ test('Kiel Hbf to Holstentor', co(function* (t) { longitude: 10.679976 } const journeys = yield client.journeys(kielHbf, holstentor, { - results: 3, when + results: 3, + departure: when }) yield testJourneysStationToPoi({ @@ -147,7 +151,9 @@ test('Kiel Hbf to Holstentor', co(function* (t) { test('Husum to Lübeck Hbf with stopover at Kiel Hbf', co(function* (t) { const journeys = yield client.journeys(husum, luebeckHbf, { via: kielHbf, - results: 1, when, passedStations: true + results: 1, + departure: when, + passedStations: true }) validate(t, journeys, 'journeys', 'journeys') @@ -179,7 +185,7 @@ test('earlier/later journeys, Kiel Hbf -> Flensburg', co(function* (t) { test('journey leg details for Flensburg to Husum', co(function* (t) { const journeys = yield client.journeys(flensburg, husum, { - results: 1, when + results: 1, departure: when }) const p = journeys[0].legs[0] diff --git a/test/oebb.js b/test/oebb.js index d2bf9632..253ac9c1 100644 --- a/test/oebb.js +++ b/test/oebb.js @@ -60,7 +60,9 @@ const wienRenngasse = '1390186' test.skip('journeys – Salzburg Hbf to Wien Westbahnhof', co(function* (t) { const journeys = yield client.journeys(salzburgHbf, wienFickeystr, { - results: 3, when, passedStations: true + results: 3, + departure: when, + passedStations: true }) yield testJourneysStationToStation({ @@ -101,7 +103,8 @@ test('Salzburg Hbf to 1220 Wien, Wagramer Straße 5', co(function* (t) { longitude: 16.425863 } const journeys = yield client.journeys(salzburgHbf, wagramerStr, { - results: 3, when + results: 3, + departure: when }) yield testJourneysStationToAddress({ @@ -123,7 +126,7 @@ test('Salzburg Hbf to Albertina', co(function* (t) { longitude: 16.368404 } const journeys = yield client.journeys(salzburgHbf, albertina, { - results: 3, when + results: 3, departure: when }) yield testJourneysStationToPoi({ @@ -146,7 +149,7 @@ test('journeys: via works – with detour', co(function* (t) { const journeys = yield client.journeys(stephansplatz, schottenring, { via: donauinsel, results: 1, - when, + departure: when, passedStations: true }) @@ -170,7 +173,7 @@ test('journeys: via works – without detour', co(function* (t) { const journeys = yield client.journeys(karlsplatz, praterstern, { via: museumsquartier, results: 1, - when, + departure: when, passedStations: true }) @@ -208,7 +211,7 @@ test('earlier/later journeys, Salzburg Hbf -> Wien Westbahnhof', co(function* (t test('leg details for Wien Westbahnhof to München Hbf', co(function* (t) { const journeys = yield client.journeys(wienWestbahnhof, muenchenHbf, { - results: 1, when + results: 1, departure: when }) const p = journeys[0].legs[0] diff --git a/test/vbb.js b/test/vbb.js index db7530ac..fcbf68fa 100644 --- a/test/vbb.js +++ b/test/vbb.js @@ -111,7 +111,9 @@ const württembergallee = '900000026153' test('journeys – Spichernstr. to Bismarckstr.', co(function* (t) { const journeys = yield client.journeys(spichernstr, bismarckstr, { - results: 3, when, passedStations: true + results: 3, + departure: when, + passedStations: true }) yield testJourneysStationToStation({ @@ -128,7 +130,8 @@ test('journeys – Spichernstr. to Bismarckstr.', co(function* (t) { test('journeys – only subway', co(function* (t) { const journeys = yield client.journeys(spichernstr, bismarckstr, { - results: 20, when, + results: 20, + departure: when, products: { suburban: false, subway: true, @@ -185,7 +188,7 @@ test('earlier/later journeys', co(function* (t) { test('journey leg details', co(function* (t) { const journeys = yield client.journeys(spichernstr, amrumerStr, { - results: 1, when + results: 1, departure: when }) const p = journeys[0].legs[0] @@ -205,7 +208,8 @@ test('journeys – station to address', co(function* (t) { longitude: 13.350042 } const journeys = yield client.journeys(spichernstr, torfstr, { - results: 3, when + results: 3, + departure: when }) yield testJourneysStationToAddress({ @@ -227,7 +231,8 @@ test('journeys – station to POI', co(function* (t) { longitude: 13.351686 } const journeys = yield client.journeys(spichernstr, atze, { - results: 3, when + results: 3, + departure: when }) yield testJourneysStationToPoi({ @@ -246,7 +251,7 @@ test('journeys: via works – with detour', co(function* (t) { const journeys = yield client.journeys(westhafen, wedding, { via: württembergallee, results: 1, - when, + departure: when, passedStations: true })