beforeJourneys -> earlierThan, afterJourneys -> laterThan

This commit is contained in:
Jannis R 2018-03-04 20:41:26 +01:00 committed by Jannis Redmann
parent 88c511da27
commit c835467d85
4 changed files with 54 additions and 18 deletions

View file

@ -52,27 +52,27 @@ 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)) { if (('earlierThan' in opt) && ('laterThan' in opt)) {
throw new Error('opt.afterJourneys and opt.afterJourneys are mutually exclusive.') throw new Error('opt.laterThan and opt.laterThan are mutually exclusive.')
} }
let journeysRef = null let journeysRef = null
if ('beforeJourneys' in opt) { if ('earlierThan' in opt) {
if (!isNonEmptyString(opt.beforeJourneys)) { if (!isNonEmptyString(opt.earlierThan)) {
throw new Error('opt.beforeJourneys must be a non-empty string.') throw new Error('opt.earlierThan must be a non-empty string.')
} }
if ('when' in opt) { if ('when' in opt) {
throw new Error('opt.beforeJourneys and opt.when are mutually exclusive.') throw new Error('opt.earlierThan and opt.when are mutually exclusive.')
} }
journeysRef = opt.beforeJourneys journeysRef = opt.earlierThan
} }
if ('afterJourneys' in opt) { if ('laterThan' in opt) {
if (!isNonEmptyString(opt.afterJourneys)) { if (!isNonEmptyString(opt.laterThan)) {
throw new Error('opt.afterJourneys must be a non-empty string.') throw new Error('opt.laterThan must be a non-empty string.')
} }
if ('when' in opt) { if ('when' in opt) {
throw new Error('opt.afterJourneys and opt.when are mutually exclusive.') throw new Error('opt.laterThan and opt.when are mutually exclusive.')
} }
journeysRef = opt.afterJourneys journeysRef = opt.laterThan
} }
opt = Object.assign({ opt = Object.assign({

View file

@ -243,6 +243,18 @@ test('earlier/later journeys, Jungfernheide -> München Hbf', co(function* (t) {
t.equal(typeof model.laterRef, 'string') t.equal(typeof model.laterRef, 'string')
t.ok(model.laterRef) t.ok(model.laterRef)
// when and earlierThan/laterThan should be mutually exclusive
t.throws(() => {
client.journeys(jungfernh, münchenHbf, {
when, earlierThan: model.earlierRef
})
})
t.throws(() => {
client.journeys(jungfernh, münchenHbf, {
when, laterThan: model.laterRef
})
})
let earliestDep = Infinity, latestDep = -Infinity let earliestDep = Infinity, latestDep = -Infinity
for (let j of model) { for (let j of model) {
const dep = +new Date(j.departure) const dep = +new Date(j.departure)
@ -253,7 +265,7 @@ test('earlier/later journeys, Jungfernheide -> München Hbf', co(function* (t) {
const earlier = yield client.journeys(jungfernh, münchenHbf, { const earlier = yield client.journeys(jungfernh, münchenHbf, {
results: 3, results: 3,
// todo: single journey ref? // todo: single journey ref?
beforeJourneys: model.earlierRef earlierThan: model.earlierRef
}) })
for (let j of earlier) { for (let j of earlier) {
t.ok(new Date(j.departure) < earliestDep) t.ok(new Date(j.departure) < earliestDep)
@ -262,7 +274,7 @@ test('earlier/later journeys, Jungfernheide -> München Hbf', co(function* (t) {
const later = yield client.journeys(jungfernh, münchenHbf, { const later = yield client.journeys(jungfernh, münchenHbf, {
results: 3, results: 3,
// todo: single journey ref? // todo: single journey ref?
afterJourneys: model.laterRef laterThan: model.laterRef
}) })
for (let j of later) { for (let j of later) {
t.ok(new Date(j.departure) > latestDep) t.ok(new Date(j.departure) > latestDep)

View file

@ -284,6 +284,18 @@ test('earlier/later journeys, Salzburg Hbf -> Wien Westbahnhof', co(function* (t
t.equal(typeof model.laterRef, 'string') t.equal(typeof model.laterRef, 'string')
t.ok(model.laterRef) t.ok(model.laterRef)
// when and earlierThan/laterThan should be mutually exclusive
t.throws(() => {
client.journeys(salzburgHbf, wienWestbahnhof, {
when, earlierThan: model.earlierRef
})
})
t.throws(() => {
client.journeys(salzburgHbf, wienWestbahnhof, {
when, laterThan: model.laterRef
})
})
let earliestDep = Infinity, latestDep = -Infinity let earliestDep = Infinity, latestDep = -Infinity
for (let j of model) { for (let j of model) {
const dep = +new Date(j.departure) const dep = +new Date(j.departure)
@ -294,7 +306,7 @@ test('earlier/later journeys, Salzburg Hbf -> Wien Westbahnhof', co(function* (t
const earlier = yield client.journeys(salzburgHbf, wienWestbahnhof, { const earlier = yield client.journeys(salzburgHbf, wienWestbahnhof, {
results: 3, results: 3,
// todo: single journey ref? // todo: single journey ref?
beforeJourneys: model.earlierRef earlierThan: model.earlierRef
}) })
for (let j of earlier) { for (let j of earlier) {
t.ok(new Date(j.departure) < earliestDep) t.ok(new Date(j.departure) < earliestDep)
@ -303,7 +315,7 @@ test('earlier/later journeys, Salzburg Hbf -> Wien Westbahnhof', co(function* (t
const later = yield client.journeys(salzburgHbf, wienWestbahnhof, { const later = yield client.journeys(salzburgHbf, wienWestbahnhof, {
results: 3, results: 3,
// todo: single journey ref? // todo: single journey ref?
afterJourneys: model.laterRef laterThan: model.laterRef
}) })
for (let j of later) { for (let j of later) {
t.ok(new Date(j.departure) > latestDep) t.ok(new Date(j.departure) > latestDep)

View file

@ -178,6 +178,18 @@ test('earlier/later journeys', co(function* (t) {
t.equal(typeof model.laterRef, 'string') t.equal(typeof model.laterRef, 'string')
t.ok(model.laterRef) t.ok(model.laterRef)
// when and earlierThan/laterThan should be mutually exclusive
t.throws(() => {
client.journeys(spichernstr, bismarckstr, {
when, earlierThan: model.earlierRef
})
})
t.throws(() => {
client.journeys(spichernstr, bismarckstr, {
when, laterThan: model.laterRef
})
})
let earliestDep = Infinity, latestDep = -Infinity let earliestDep = Infinity, latestDep = -Infinity
for (let j of model) { for (let j of model) {
const dep = +new Date(j.departure) const dep = +new Date(j.departure)
@ -188,7 +200,7 @@ test('earlier/later journeys', co(function* (t) {
const earlier = yield client.journeys(spichernstr, bismarckstr, { const earlier = yield client.journeys(spichernstr, bismarckstr, {
results: 3, results: 3,
// todo: single journey ref? // todo: single journey ref?
beforeJourneys: model.earlierRef earlierThan: model.earlierRef
}) })
for (let j of earlier) { for (let j of earlier) {
t.ok(new Date(j.departure) < earliestDep) t.ok(new Date(j.departure) < earliestDep)
@ -197,7 +209,7 @@ test('earlier/later journeys', co(function* (t) {
const later = yield client.journeys(spichernstr, bismarckstr, { const later = yield client.journeys(spichernstr, bismarckstr, {
results: 3, results: 3,
// todo: single journey ref? // todo: single journey ref?
afterJourneys: model.laterRef laterThan: model.laterRef
}) })
for (let j of later) { for (let j of later) {
t.ok(new Date(j.departure) > latestDep) t.ok(new Date(j.departure) > latestDep)