beforeJourneys/afterJourneys options

This commit is contained in:
Jannis R 2018-03-04 19:54:21 +01:00 committed by Jannis Redmann
parent d8c9fd7a33
commit 56e660d08e
2 changed files with 67 additions and 1 deletions

View file

@ -52,6 +52,29 @@ const createClient = (profile, request = _request) => {
from = profile.formatLocation(profile, from)
to = profile.formatLocation(profile, to)
if (('beforeJourneys' in opt) && ('afterJourneys' in opt)) {
throw new Error('opt.afterJourneys and opt.afterJourneys are mutually exclusive.')
}
let journeysRef = null
if ('beforeJourneys' in opt) {
if (!isNonEmptyString(opt.beforeJourneys)) {
throw new Error('opt.beforeJourneys must be a non-empty string.')
}
if ('when' in opt) {
throw new Error('opt.beforeJourneys and opt.when are mutually exclusive.')
}
journeysRef = opt.beforeJourneys
}
if ('afterJourneys' in opt) {
if (!isNonEmptyString(opt.afterJourneys)) {
throw new Error('opt.afterJourneys must be a non-empty string.')
}
if ('when' in opt) {
throw new Error('opt.afterJourneys and opt.when are mutually exclusive.')
}
journeysRef = opt.afterJourneys
}
opt = Object.assign({
results: 5, // how many journeys?
via: null, // let journeys pass this station?
@ -81,6 +104,7 @@ const createClient = (profile, request = _request) => {
const query = profile.transformJourneysQuery({
outDate: profile.formatDate(profile, opt.when),
outTime: profile.formatTime(profile, opt.when),
ctxScr: journeysRef,
numF: opt.results,
getPasslist: !!opt.passedStations,
maxChg: opt.transfers,
@ -106,7 +130,11 @@ const createClient = (profile, request = _request) => {
.then((d) => {
if (!Array.isArray(d.outConL)) return []
const parse = profile.parseJourney(profile, d.locations, d.lines, d.remarks)
return d.outConL.map(parse)
const res = d.outConL.map(parse)
if (d.outCtxScrB) res.earlierJourneysRef = d.outCtxScrB
if (d.outCtxScrF) res.laterJourneysRef = d.outCtxScrF
return res
})
}

View file

@ -168,6 +168,44 @@ test('journeys  fails with no product', co(function* (t) {
}
}))
test('earlier/later journeys', co(function* (t) {
const model = yield client.journeys(spichernstr, bismarckstr, {
results: 3, when
})
t.equal(typeof model.earlierJourneysRef, 'string')
t.ok(model.earlierJourneysRef)
t.equal(typeof model.laterJourneysRef, 'string')
t.ok(model.laterJourneysRef)
let earliestDep = Infinity, latestDep = -Infinity
for (let j of model) {
const dep = +new Date(j.departure)
if (dep < earliestDep) earliestDep = dep
else if (dep > latestDep) latestDep = dep
}
const earlier = yield client.journeys(spichernstr, bismarckstr, {
results: 3,
// todo: single journey ref?
beforeJourneys: model.earlierJourneysRef
})
for (let j of earlier) {
t.ok(new Date(j.departure) < earliestDep)
}
const later = yield client.journeys(spichernstr, bismarckstr, {
results: 3,
// todo: single journey ref?
afterJourneys: model.laterJourneysRef
})
for (let j of later) {
t.ok(new Date(j.departure) > latestDep)
}
t.end()
}))
test('journey leg details', co(function* (t) {
const journeys = yield client.journeys(spichernstr, amrumerStr, {
results: 1, when