From 31973431ff1a0289e58fb8f4ab308bb1d36a0b92 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Thu, 28 Jun 2018 13:45:56 +0200 Subject: [PATCH] option not to parse remarks --- docs/departures.md | 3 ++- docs/journey-leg.md | 3 ++- docs/journeys.md | 1 + index.js | 7 +++++-- lib/request.js | 5 +++-- parse/arrival-or-departure.js | 13 ++++++++----- parse/journey-leg.js | 3 ++- parse/journey.js | 2 +- parse/stopover.js | 2 +- 9 files changed, 25 insertions(+), 14 deletions(-) diff --git a/docs/departures.md b/docs/departures.md index d331da07..344958b4 100644 --- a/docs/departures.md +++ b/docs/departures.md @@ -27,7 +27,8 @@ With `opt`, you can override the default options, which look like this: when: new Date(), direction: null, // only show departures heading to this station duration: 10, // show departures for the next n minutes - stationLines: false // parse & expose lines of the station? + stationLines: false, // parse & expose lines of the station? + remarks: true // parse & expose hints & warnings? } ``` diff --git a/docs/journey-leg.md b/docs/journey-leg.md index 6060b36a..de7fddc8 100644 --- a/docs/journey-leg.md +++ b/docs/journey-leg.md @@ -26,7 +26,8 @@ With `opt`, you can override the default options, which look like this: { when: new Date(), stopovers: true, // return stations on the way? - polyline: false // return a shape for the leg? + polyline: false, // return a shape for the leg? + remarks: true // parse & expose hints & warnings? } ``` diff --git a/docs/journeys.md b/docs/journeys.md index 0e8a5bc8..ebbf55ce 100644 --- a/docs/journeys.md +++ b/docs/journeys.md @@ -65,6 +65,7 @@ With `opt`, you can override the default options, which look like this: }, tickets: false, // return tickets? only available with some profiles polylines: false, // return a shape for each leg? + remarks: true, // parse & expose hints & warnings? // Consider walking to nearby stations at the beginning of a journey? startWithWalking: true } diff --git a/index.js b/index.js index dfe795e2..6dee9941 100644 --- a/index.js +++ b/index.js @@ -34,7 +34,8 @@ const createClient = (profile, request = _request) => { opt = Object.assign({ direction: null, // only show departures heading to this station duration: 10, // show departures for the next n minutes - stationLines: false // parse & expose lines of the station? + stationLines: false, // parse & expose lines of the station? + remarks: true // parse & expose hints & warnings? }, opt) opt.when = new Date(opt.when || Date.now()) if (Number.isNaN(+opt.when)) throw new Error('opt.when is invalid') @@ -115,6 +116,7 @@ const createClient = (profile, request = _request) => { bike: false, // only bike-friendly journeys tickets: false, // return tickets? polylines: false, // return leg shapes? + remarks: true, // parse & expose hints & warnings? // Consider walking to nearby stations at the beginning of a journey? startWithWalking: true }, opt) @@ -322,7 +324,8 @@ const createClient = (profile, request = _request) => { } opt = Object.assign({ stopovers: true, // return stations on the way? - polyline: false + polyline: false, + remarks: true // parse & expose hints & warnings? }, opt) opt.when = new Date(opt.when || Date.now()) if (Number.isNaN(+opt.when)) throw new Error('opt.when is invalid') diff --git a/lib/request.js b/lib/request.js index 8102b7fe..4022bed4 100644 --- a/lib/request.js +++ b/lib/request.js @@ -81,11 +81,12 @@ const request = (profile, opt, data) => { const d = b.svcResL[0].res const c = d.common || {} - if (Array.isArray(c.remL)) { + d.hints = [] + if (opt.remarks && Array.isArray(c.remL)) { d.hints = c.remL.map(hint => profile.parseHint(profile, hint)) } d.warnings = [] - if (Array.isArray(c.himL)) { + if (opt.remarks && Array.isArray(c.himL)) { d.warnings = c.himL.map(w => profile.parseWarning(profile, w)) } if (Array.isArray(c.opL)) { diff --git a/parse/arrival-or-departure.js b/parse/arrival-or-departure.js index f6f7874a..6aaa5e4e 100644 --- a/parse/arrival-or-departure.js +++ b/parse/arrival-or-departure.js @@ -19,11 +19,7 @@ const createParseArrOrDep = (profile, opt, data, prefix) => { when: when.toISO(), direction: profile.parseStationName(d.dirTxt), line: lines[parseInt(d.prodX)] || null, - remarks: ([] - .concat(d.remL || [], d.msgL || []) - .map(ref => findRemark(hints, warnings, ref)) - .filter(rem => !!rem) // filter unparsable - ), + remarks: [], // todo: res.trip from rawLine.prodCtx.num? trip: +d.jid.split('|')[1] // todo: this seems brittle } @@ -56,6 +52,13 @@ const createParseArrOrDep = (profile, opt, data, prefix) => { res.formerScheduledWhen = when.toISO() } + if (opt.remarks) { + res.remarks = [] + .concat(d.remL || [], d.msgL || []) + .map(ref => findRemark(hints, warnings, ref)) + .filter(rem => !!rem) // filter unparsable + } + return res } diff --git a/parse/journey-leg.js b/parse/journey-leg.js index c92d8204..df56120b 100644 --- a/parse/journey-leg.js +++ b/parse/journey-leg.js @@ -95,12 +95,13 @@ const createParseJourneyLeg = (profile, opt, data) => { res.stopovers = stopL.map(parse) // todo: is there a `pt.jny.remL`? - if (Array.isArray(pt.jny.msgL)) { + if (opt.remarks && Array.isArray(pt.jny.msgL)) { for (let i = 0; i < stopL.length; i++) { Object.defineProperty(res.stopovers[i], locX, { value: stopL[i].locX }) } + // todo: apply leg-wide remarks if `parseStopovers` is false applyRemarks(res, hints, warnings, pt.jny.msgL) } diff --git a/parse/journey.js b/parse/journey.js index dfbbd20f..a852469f 100644 --- a/parse/journey.js +++ b/parse/journey.js @@ -23,7 +23,7 @@ const createParseJourney = (profile, opt, data) => { legs } - if (Array.isArray(j.msgL)) { + if (opt.remarks && Array.isArray(j.msgL)) { res.remarks = [] for (let ref of j.msgL) { const remark = findRemark(hints, warnings, ref) diff --git a/parse/stopover.js b/parse/stopover.js index b2e5b620..6b55944c 100644 --- a/parse/stopover.js +++ b/parse/stopover.js @@ -59,7 +59,7 @@ const createParseStopover = (profile, opt, data, date) => { } } - if (Array.isArray(st.msgL)) { + if (opt.remarks && Array.isArray(st.msgL)) { res.remarks = [] for (let ref of st.msgL) { const remark = findRemark(hints, warnings, ref)