mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 15:19:35 +02:00
beforeJourneys/afterJourneys options
This commit is contained in:
parent
d8c9fd7a33
commit
56e660d08e
2 changed files with 67 additions and 1 deletions
30
index.js
30
index.js
|
@ -52,6 +52,29 @@ const createClient = (profile, request = _request) => {
|
||||||
from = profile.formatLocation(profile, from)
|
from = profile.formatLocation(profile, from)
|
||||||
to = profile.formatLocation(profile, to)
|
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({
|
opt = Object.assign({
|
||||||
results: 5, // how many journeys?
|
results: 5, // how many journeys?
|
||||||
via: null, // let journeys pass this station?
|
via: null, // let journeys pass this station?
|
||||||
|
@ -81,6 +104,7 @@ const createClient = (profile, request = _request) => {
|
||||||
const query = profile.transformJourneysQuery({
|
const query = profile.transformJourneysQuery({
|
||||||
outDate: profile.formatDate(profile, opt.when),
|
outDate: profile.formatDate(profile, opt.when),
|
||||||
outTime: profile.formatTime(profile, opt.when),
|
outTime: profile.formatTime(profile, opt.when),
|
||||||
|
ctxScr: journeysRef,
|
||||||
numF: opt.results,
|
numF: opt.results,
|
||||||
getPasslist: !!opt.passedStations,
|
getPasslist: !!opt.passedStations,
|
||||||
maxChg: opt.transfers,
|
maxChg: opt.transfers,
|
||||||
|
@ -106,7 +130,11 @@ const createClient = (profile, request = _request) => {
|
||||||
.then((d) => {
|
.then((d) => {
|
||||||
if (!Array.isArray(d.outConL)) return []
|
if (!Array.isArray(d.outConL)) return []
|
||||||
const parse = profile.parseJourney(profile, d.locations, d.lines, d.remarks)
|
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
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
38
test/vbb.js
38
test/vbb.js
|
@ -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) {
|
test('journey leg details', co(function* (t) {
|
||||||
const journeys = yield client.journeys(spichernstr, amrumerStr, {
|
const journeys = yield client.journeys(spichernstr, amrumerStr, {
|
||||||
results: 1, when
|
results: 1, when
|
||||||
|
|
Loading…
Add table
Reference in a new issue