Skip to content

Commit

Permalink
add suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bergmannjg committed Mar 25, 2024
1 parent 92712c7 commit 6591078
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
22 changes: 11 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import isObj from 'lodash/isObject.js';
import sortBy from 'lodash/sortBy.js';
import omit from 'lodash/omit.js';
import {DateTime} from 'luxon';

import {defaultProfile} from './lib/default-profile.js';
import {validateProfile} from './lib/validate-profile.js';
Expand Down Expand Up @@ -276,6 +277,7 @@ const createClient = (profile, userAgent, opt = {}) => {
opt = Object.assign({
via: null, // let journeys pass this station?
transfers: -1, // maximum nr of transfers
transferTime: 0, // minimum time for a single transfer in minutes
bike: false, // only bike-friendly journeys
tickets: false, // return tickets?
polylines: false, // return leg shapes?
Expand All @@ -295,7 +297,13 @@ const createClient = (profile, userAgent, opt = {}) => {
throw new TypeError('opt.departure is invalid');
}
const now = new Date();
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
let today = DateTime.fromObject({
year: now.year, month: now.month, day: now.day,
hour: 0, minute: 0, second: 0, millisecond: 0,
}, {
zone: profile.timezone,
locale: profile.locale,
});
if (today > when) {
throw new TypeError('opt.departure date older than current date.');
}
Expand All @@ -304,14 +312,6 @@ const createClient = (profile, userAgent, opt = {}) => {
const filters = [
profile.formatProductsFilter({profile}, opt.products || {}),
];
if (
opt.accessibility
&& profile.filters
&& profile.filters.accessibility
&& profile.filters.accessibility[opt.accessibility]
) {
filters.push(profile.filters.accessibility[opt.accessibility]);
}

const query = {
maxChg: opt.transfers,
Expand All @@ -326,7 +326,7 @@ const createClient = (profile, userAgent, opt = {}) => {
query.outDate = profile.formatDate(profile, when);

if (profile.endpoint !== dbProfile.endpoint) {
throw new Error('db profile expected.');
throw new Error('bestPrices() only works with the DB profile.');
}

const {res, common} = await profile.request({profile, opt}, userAgent, {
Expand All @@ -335,7 +335,7 @@ const createClient = (profile, userAgent, opt = {}) => {
req: profile.transformJourneysQuery({profile, opt}, query),
});
if (!Array.isArray(res.outConL)) {
return {};
return null;
}
// todo: outConGrpL

Expand Down
8 changes: 4 additions & 4 deletions parse/bestprice.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const parseBestPrice = (ctx, outDaySeg, journeys) => {

const bpjourneys = outDaySeg.conRefL
? outDaySeg.conRefL
.map(i => journeys.find(j => j.refreshToken == res.outConL[i].ctxRecon))
.map(outConLIdx => journeys.find(j => j.refreshToken == res.outConL[outConLIdx].ctxRecon))
.filter(j => Boolean(j))
: [];

Expand All @@ -13,9 +13,9 @@ const parseBestPrice = (ctx, outDaySeg, journeys) => {

const result = {
journeys: bpjourneys,
fromDate: profile.parseDateTime(ctx, outDaySeg.fromDate, outDaySeg.fromTime),
toDate: profile.parseDateTime(ctx, outDaySeg.toDate, outDaySeg.toTime),
bestPrice: amount > 0 && currency ? {amount, currency} : undefined,
from: profile.parseDateTime(ctx, outDaySeg.fromDate, outDaySeg.fromTime),
to: profile.parseDateTime(ctx, outDaySeg.toDate, outDaySeg.toTime),
bestPrice: amount > 0 && currency ? {amount, currency} : null,
};

return result;
Expand Down
5 changes: 2 additions & 3 deletions test/db-bestprice.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ const opt = {
via: null,
transfers: -1,
transferTime: 0,
accessibility: 'none',
bike: false,
tickets: true,
polylines: true,
remarks: true,
walkingSpeed: 'normal',
startWithWalking: true,
departure: '2023-06-15',
products: {},
firstClass: false,
ageGroup: 'E',
};

tap.test('parses a bestprice with a DEVI leg correctly (DB)', (t) => {
Expand Down
24 changes: 12 additions & 12 deletions test/fixtures/db-bestprice.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ const json = `
]
}
],
"fromDate": "2023-06-15T10:00:00+02:00",
"toDate": "2023-06-15T13:00:00+02:00",
"from": "2023-06-15T10:00:00+02:00",
"to": "2023-06-15T13:00:00+02:00",
"bestPrice": {
"amount": 31,
"currency": "EUR"
Expand Down Expand Up @@ -806,8 +806,8 @@ const json = `
]
}
],
"fromDate": "2023-06-15T13:00:00+02:00",
"toDate": "2023-06-15T16:00:00+02:00",
"from": "2023-06-15T13:00:00+02:00",
"to": "2023-06-15T16:00:00+02:00",
"bestPrice": {
"amount": 29.9,
"currency": "EUR"
Expand Down Expand Up @@ -1506,8 +1506,8 @@ const json = `
]
}
],
"fromDate": "2023-06-15T16:00:00+02:00",
"toDate": "2023-06-15T19:00:00+02:00",
"from": "2023-06-15T16:00:00+02:00",
"to": "2023-06-15T19:00:00+02:00",
"bestPrice": {
"amount": 28.9,
"currency": "EUR"
Expand Down Expand Up @@ -2293,23 +2293,23 @@ const json = `
]
}
],
"fromDate": "2023-06-15T19:00:00+02:00",
"toDate": "2023-06-16T00:00:00+02:00",
"from": "2023-06-15T19:00:00+02:00",
"to": "2023-06-16T00:00:00+02:00",
"bestPrice": {
"amount": 9.9,
"currency": "EUR"
}
},
{
"journeys": [],
"fromDate": "2023-06-15T00:00:00+02:00",
"toDate": "2023-06-15T07:00:00+02:00",
"from": "2023-06-15T00:00:00+02:00",
"to": "2023-06-15T07:00:00+02:00",
"bestPrice": null
},
{
"journeys": [],
"fromDate": "2023-06-15T07:00:00+02:00",
"toDate": "2023-06-15T10:00:00+02:00",
"from": "2023-06-15T07:00:00+02:00",
"to": "2023-06-15T10:00:00+02:00",
"bestPrice": null
}
],
Expand Down

0 comments on commit 6591078

Please sign in to comment.