Allow value -1 for transfers option in journeys() (#6)

* allow value -1 for transfers option in journeys() for db and dbnav profiles

* add unit test for implicitly unconstrained transfers

* implement `formatTransfers()`, use it `formatJourneysReq()` functions and re-add corresponding unit test
This commit is contained in:
dabund24 2025-01-09 00:22:43 +01:00 committed by GitHub
parent 8026689ee8
commit 632a29d2aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 35 additions and 2 deletions

10
format/transfers.js Normal file
View file

@ -0,0 +1,10 @@
const formatTransfers = (transfers) => {
if (transfers === -1) { // profiles may not accept -1: https://github.com/public-transport/db-vendo-client/issues/5
return undefined;
}
return transfers;
};
export {
formatTransfers,
};

View file

@ -36,6 +36,7 @@ import {formatTime, formatTimeOfDay} from '../format/time.js';
import {formatLocation} from '../format/location.js';
import {formatTravellers} from '../format/travellers.js';
import {formatLoyaltyCard} from '../format/loyalty-cards.js';
import {formatTransfers} from '../format/transfers.js';
const DEBUG = (/(^|,)hafas-client(,|$)/).test(process.env.DEBUG || '');
const logRequest = DEBUG
@ -103,6 +104,7 @@ const defaultProfile = {
formatStation,
formatTime,
formatTimeOfDay,
formatTransfers,
formatTravellers,
formatRectangle: id,

View file

@ -4,10 +4,11 @@ const formatJourneysReq = (ctx, from, to, when, outFrwd, journeysRef) => {
from = profile.formatLocation(profile, from, 'from');
to = profile.formatLocation(profile, to, 'to');
const filters = profile.formatProductsFilter({profile}, opt.products || {});
const transfers = profile.formatTransfers(opt.transfers);
// TODO opt.accessibility
// TODO routingMode
let query = {
maxUmstiege: opt.transfers,
maxUmstiege: transfers,
minUmstiegszeit: opt.transferTime,
deutschlandTicketVorhanden: false,
nurDeutschlandTicketVerbindungen: false,

View file

@ -31,6 +31,7 @@ const formatJourneysReq = (ctx, from, to, when, outFrwd, journeysRef) => {
from = profile.formatLocation(profile, from, 'from');
to = profile.formatLocation(profile, to, 'to');
const filters = profile.formatProductsFilter({profile}, opt.products || {}, 'dbnav');
const transfers = profile.formatTransfers(opt.transfers) ?? undefined; // `dbnav` does not allow `undefined` here
// TODO opt.accessibility
// TODO routingMode
let query = formatBaseJourneysReq(ctx);
@ -46,7 +47,7 @@ const formatJourneysReq = (ctx, from, to, when, outFrwd, journeysRef) => {
? [{locationId: profile.formatLocation(profile, opt.via, 'opt.via').lid}]
: undefined,
zielLocationId: to.lid,
maxUmstiege: opt.transfers || undefined,
maxUmstiege: transfers,
minUmstiegsdauer: opt.transferTime || undefined,
fahrradmitnahme: opt.bike,
},

View file

@ -114,3 +114,22 @@ tap.test('formats a journeys() request with BC correctly (DB)', (t) => {
});
t.end();
});
tap.test('formats a journeys() request with unlimited transfers (DB)', (t) => {
const _opt = {...opt};
const ctx = {profile, opt: _opt};
ctx.opt.transfers = 0; // no transfers
const reqZeroTransfers = profile.formatJourneysReq(ctx, '8098160', '8000284', new Date('2024-12-07T23:50:12+01:00'), true, null);
t.equal(reqZeroTransfers.body.maxUmstiege, 0);
ctx.opt.transfers = undefined; // unconstrained transfers implicit
const reqUnlimitedTransfersImplicit = profile.formatJourneysReq(ctx, '8098160', '8000284', new Date('2024-12-07T23:50:12+01:00'), true, null);
t.equal(reqUnlimitedTransfersImplicit.body.maxUmstiege, undefined);
ctx.opt.transfers = -1; // unconstrained transfers explicit
const reqUnlimitedTransfersExplicit = profile.formatJourneysReq(ctx, '8098160', '8000284', new Date('2024-12-07T23:50:12+01:00'), true, null);
t.equal(reqUnlimitedTransfersExplicit.body.maxUmstiege, undefined);
t.end();
});