diff --git a/parse/arrival-or-departure.js b/parse/arrival-or-departure.js index 2b3631ea..107e4fee 100644 --- a/parse/arrival-or-departure.js +++ b/parse/arrival-or-departure.js @@ -1,6 +1,6 @@ 'use strict' -const findRemark = require('./find-remark') +const findRemarks = require('./find-remarks') const ARRIVAL = 'a' const DEPARTURE = 'd' @@ -9,7 +9,6 @@ const DEPARTURE = 'd' // todo: d.stbStop.dProgType/d.stbStop.aProgType const createParseArrOrDep = (profile, opt, data, prefix) => { - const {hints, warnings} = data if (prefix !== ARRIVAL && prefix !== DEPARTURE) throw new Error('invalid prefix') const parseArrOrDep = (d) => { @@ -53,10 +52,10 @@ const createParseArrOrDep = (profile, opt, data, prefix) => { } if (opt.remarks) { - res.remarks = [] - .concat(d.remL || [], d.msgL || []) - .map(ref => findRemark(hints, warnings, ref)) - .filter(rem => !!rem) // filter unparsable + res.remarks = findRemarks([ + ...(d.remL || []), + ...(d.msgL || []) + ]).map(([remark]) => remark) } if (opt.stopovers && Array.isArray(d.stopL)) { diff --git a/parse/common.js b/parse/common.js index f5061ee3..eefc0a58 100644 --- a/parse/common.js +++ b/parse/common.js @@ -72,10 +72,12 @@ const parseCommonData = (profile, opt, res) => { res.hints = [] if (opt.remarks && Array.isArray(c.remL)) { res.hints = c.remL.map(hint => profile.parseHint(profile, hint, {...c, ...res})) + resolveIdxRefs(res, '**.remX', res.hints, 'hint') } res.warnings = [] if (opt.remarks && Array.isArray(c.himL)) { res.warnings = c.himL.map(w => profile.parseWarning(profile, w, {...c, ...res})) + resolveIdxRefs(res, '**.himX', res.warnings, 'warning') } res.polylines = [] diff --git a/parse/find-remark.js b/parse/find-remarks.js similarity index 68% rename from parse/find-remark.js rename to parse/find-remarks.js index 9bf89884..26b64c32 100644 --- a/parse/find-remark.js +++ b/parse/find-remarks.js @@ -1,5 +1,7 @@ 'use strict' +const flatMap = require('lodash/flatMap') + // There are two kinds of notes: "remarks" (in `remL`) and HAFAS // Information Manager (HIM) notes (in `himL`). The former describe // the regular operating situation, e.g. "bicycles allows", whereas @@ -10,8 +12,12 @@ // - warnings: notes from `himL` for cancellations, construction, etc // - remarks: both "notes" and "warnings" -const findRemark = (hints, warnings, ref) => { - return warnings[ref.himX] || hints[ref.remX] || null +const findRemarks = (refs) => { + return flatMap(refs, (ref) => { + return [ref.warning, ref.hint] + .filter(rem => !!rem) + .map(rem => [rem, ref]) + }) } -module.exports = findRemark +module.exports = findRemarks diff --git a/parse/journey-leg.js b/parse/journey-leg.js index 3e7fa076..819f664c 100644 --- a/parse/journey-leg.js +++ b/parse/journey-leg.js @@ -1,14 +1,17 @@ 'use strict' const parseDateTime = require('./date-time') -const findRemark = require('./find-remark') +const findRemarks = require('./find-remarks') const clone = obj => Object.assign({}, obj) -const applyRemarks = (leg, hints, warnings, refs) => { - for (let ref of refs) { - const remark = findRemark(hints, warnings, ref) - if (!remark) continue +const addRemark = (stopoverOrLeg, remark) => { + if (!Array.isArray(stopoverOrLeg.remarks)) stopoverOrLeg.remarks = [] + stopoverOrLeg.remarks.push(remark) +} + +const applyRemarks = (leg, refs) => { + for (let [remark, ref] of findRemarks(refs)) { const {fromLocation, toLocation} = ref if (ref.fromLocation && ref.toLocation) { @@ -21,25 +24,20 @@ const applyRemarks = (leg, hints, warnings, refs) => { for (let i = fromI; i <= toI; i++) { const stopover = leg.stopovers[i] if (!stopover) continue - if (Array.isArray(stopover.remarks)) { - stopover.remarks.push(remark) - } else { - stopover.remarks = [remark] - } + addRemark(stopover, remark) } continue } } - if (Array.isArray(leg.remarks)) leg.remarks.push(remark) - else leg.remarks = [remark] + addRemark(leg, remark) // todo: `ref.tagL` } } const createParseJourneyLeg = (profile, opt, data) => { - const {hints, warnings, polylines} = data + const {polylines} = data // todo: pt.status, pt.isPartCncl // todo: pt.isRchbl, pt.chRatingRT, pt.chgDurR, pt.minChg // todo: pt.dep.dProgType, pt.arr.dProgType @@ -94,7 +92,7 @@ const createParseJourneyLeg = (profile, opt, data) => { // https://gist.github.com/derhuerst/426d4b95aeae701843b1e9c23105b8d4#file-tripsearch-2018-12-05-http-L4207-L4229 if (opt.remarks && Array.isArray(pt.gis.msgL)) { - applyRemarks(res, hints, warnings, pt.gis.msgL) + applyRemarks(res, pt.gis.msgL) } } else if (pt.type === 'JNY') { // todo: pull `public` value from `profile.products` @@ -119,7 +117,7 @@ const createParseJourneyLeg = (profile, opt, data) => { if (opt.remarks && Array.isArray(pt.jny.msgL)) { // todo: apply leg-wide remarks if `parseStopovers` is false - applyRemarks(res, hints, warnings, pt.jny.msgL) + applyRemarks(res, pt.jny.msgL) } // filter stations the train passes without stopping, as this doesn't comply with fptf (yet) diff --git a/parse/journey.js b/parse/journey.js index 8c633bae..2a0d492f 100644 --- a/parse/journey.js +++ b/parse/journey.js @@ -1,7 +1,7 @@ 'use strict' const {DateTime} = require('luxon') -const findRemark = require('./find-remark') +const findRemarks = require('./find-remarks') const parseScheduledDays = (sDaysB, profile) => { sDaysB = Buffer.from(sDaysB, 'hex') @@ -24,8 +24,6 @@ const parseScheduledDays = (sDaysB, profile) => { const createParseJourney = (profile, opt, data) => { const parseLeg = profile.parseJourneyLeg(profile, opt, data) - const {hints, warnings} = data - // todo: c.conSubscr // todo: c.trfRes x vbb-parse-ticket // todo: c.sotRating, c.isSotCon, c.sotCtxt @@ -53,11 +51,7 @@ const createParseJourney = (profile, opt, data) => { } if (opt.remarks && Array.isArray(j.msgL)) { - res.remarks = [] - for (let ref of j.msgL) { - const remark = findRemark(hints, warnings, ref) - if (remark) res.remarks.push(remark) - } + res.remarks = findRemarks(j.msgL).map(([remark]) => remark) } if (opt.scheduledDays) { diff --git a/parse/stopover.js b/parse/stopover.js index c0943c90..c14d84cf 100644 --- a/parse/stopover.js +++ b/parse/stopover.js @@ -1,10 +1,8 @@ 'use strict' -const findRemark = require('./find-remark') +const findRemarks = require('./find-remarks') const createParseStopover = (profile, opt, data, date) => { - const {hints, warnings} = data - const parseStopover = (st) => { const res = { stop: st.location || null, @@ -72,11 +70,7 @@ const createParseStopover = (profile, opt, data, date) => { } if (opt.remarks && Array.isArray(st.msgL)) { - res.remarks = [] - for (let ref of st.msgL) { - const remark = findRemark(hints, warnings, ref) - if (remark) res.remarks.push(remark) - } + res.remarks = findRemarks(st.msgL).map(([remark]) => remark) } return res