From 8ae3fe22369db8e8567980798360858cc352db91 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Mon, 5 Mar 2018 00:51:11 +0100 Subject: [PATCH] fix journeys collection :bug: --- index.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 34c3308a..07098f4e 100644 --- a/index.js +++ b/index.js @@ -110,6 +110,9 @@ const createClient = (profile, request = _request) => { const journeys = [] const more = (when, journeysRef) => { const query = { + outDate: profile.formatDate(profile, when), + outTime: profile.formatTime(profile, when), + ctxScr: journeysRef, // numF: opt.results, getPasslist: !!opt.passedStations, maxChg: opt.transfers, @@ -126,12 +129,6 @@ const createClient = (profile, request = _request) => { getIV: false, // todo: walk & bike as alternatives? getPolyline: false // todo: shape for displaying on a map? } - if (when) { - query.outDate = profile.formatDate(profile, when) - query.outTime = profile.formatTime(profile, when) - } else if (journeysRef) { - query.ctxScr = journeysRef - } else throw new Error('when or ref required') return request(profile, { cfg: {polyEnc: 'GPA'}, @@ -143,14 +140,21 @@ const createClient = (profile, request = _request) => { const parse = profile.parseJourney(profile, d.locations, d.lines, d.remarks) if (!journeys.earlierRef) journeys.earlierRef = d.outCtxScrB + let latestDep = -Infinity for (let j of d.outConL) { - journeys.push(parse(j)) + j = parse(j) + journeys.push(j) + if (journeys.length === opt.results) { // collected enough journeys.laterRef = d.outCtxScrF return journeys } + const dep = +new Date(j.departure) + if (dep > latestDep) latestDep = dep } - return more(null, d.outCtxScrF) // otherwise continue + + const when = new Date(latestDep) + return more(when, d.outCtxScrF) // otherwise continue }) }