parseCommon: resolve hint & warning references

This commit is contained in:
Jannis R 2019-08-23 16:21:44 +02:00
parent 62cc53ffcf
commit 6af8f6d5ec
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
6 changed files with 33 additions and 40 deletions

View file

@ -1,6 +1,6 @@
'use strict' 'use strict'
const findRemark = require('./find-remark') const findRemarks = require('./find-remarks')
const ARRIVAL = 'a' const ARRIVAL = 'a'
const DEPARTURE = 'd' const DEPARTURE = 'd'
@ -9,7 +9,6 @@ const DEPARTURE = 'd'
// todo: d.stbStop.dProgType/d.stbStop.aProgType // todo: d.stbStop.dProgType/d.stbStop.aProgType
const createParseArrOrDep = (profile, opt, data, prefix) => { const createParseArrOrDep = (profile, opt, data, prefix) => {
const {hints, warnings} = data
if (prefix !== ARRIVAL && prefix !== DEPARTURE) throw new Error('invalid prefix') if (prefix !== ARRIVAL && prefix !== DEPARTURE) throw new Error('invalid prefix')
const parseArrOrDep = (d) => { const parseArrOrDep = (d) => {
@ -53,10 +52,10 @@ const createParseArrOrDep = (profile, opt, data, prefix) => {
} }
if (opt.remarks) { if (opt.remarks) {
res.remarks = [] res.remarks = findRemarks([
.concat(d.remL || [], d.msgL || []) ...(d.remL || []),
.map(ref => findRemark(hints, warnings, ref)) ...(d.msgL || [])
.filter(rem => !!rem) // filter unparsable ]).map(([remark]) => remark)
} }
if (opt.stopovers && Array.isArray(d.stopL)) { if (opt.stopovers && Array.isArray(d.stopL)) {

View file

@ -72,10 +72,12 @@ const parseCommonData = (profile, opt, res) => {
res.hints = [] res.hints = []
if (opt.remarks && Array.isArray(c.remL)) { if (opt.remarks && Array.isArray(c.remL)) {
res.hints = c.remL.map(hint => profile.parseHint(profile, hint, {...c, ...res})) res.hints = c.remL.map(hint => profile.parseHint(profile, hint, {...c, ...res}))
resolveIdxRefs(res, '**.remX', res.hints, 'hint')
} }
res.warnings = [] res.warnings = []
if (opt.remarks && Array.isArray(c.himL)) { if (opt.remarks && Array.isArray(c.himL)) {
res.warnings = c.himL.map(w => profile.parseWarning(profile, w, {...c, ...res})) res.warnings = c.himL.map(w => profile.parseWarning(profile, w, {...c, ...res}))
resolveIdxRefs(res, '**.himX', res.warnings, 'warning')
} }
res.polylines = [] res.polylines = []

View file

@ -1,5 +1,7 @@
'use strict' 'use strict'
const flatMap = require('lodash/flatMap')
// There are two kinds of notes: "remarks" (in `remL`) and HAFAS // There are two kinds of notes: "remarks" (in `remL`) and HAFAS
// Information Manager (HIM) notes (in `himL`). The former describe // Information Manager (HIM) notes (in `himL`). The former describe
// the regular operating situation, e.g. "bicycles allows", whereas // the regular operating situation, e.g. "bicycles allows", whereas
@ -10,8 +12,12 @@
// - warnings: notes from `himL` for cancellations, construction, etc // - warnings: notes from `himL` for cancellations, construction, etc
// - remarks: both "notes" and "warnings" // - remarks: both "notes" and "warnings"
const findRemark = (hints, warnings, ref) => { const findRemarks = (refs) => {
return warnings[ref.himX] || hints[ref.remX] || null return flatMap(refs, (ref) => {
return [ref.warning, ref.hint]
.filter(rem => !!rem)
.map(rem => [rem, ref])
})
} }
module.exports = findRemark module.exports = findRemarks

View file

@ -1,14 +1,17 @@
'use strict' 'use strict'
const parseDateTime = require('./date-time') const parseDateTime = require('./date-time')
const findRemark = require('./find-remark') const findRemarks = require('./find-remarks')
const clone = obj => Object.assign({}, obj) const clone = obj => Object.assign({}, obj)
const applyRemarks = (leg, hints, warnings, refs) => { const addRemark = (stopoverOrLeg, remark) => {
for (let ref of refs) { if (!Array.isArray(stopoverOrLeg.remarks)) stopoverOrLeg.remarks = []
const remark = findRemark(hints, warnings, ref) stopoverOrLeg.remarks.push(remark)
if (!remark) continue }
const applyRemarks = (leg, refs) => {
for (let [remark, ref] of findRemarks(refs)) {
const {fromLocation, toLocation} = ref const {fromLocation, toLocation} = ref
if (ref.fromLocation && ref.toLocation) { if (ref.fromLocation && ref.toLocation) {
@ -21,25 +24,20 @@ const applyRemarks = (leg, hints, warnings, refs) => {
for (let i = fromI; i <= toI; i++) { for (let i = fromI; i <= toI; i++) {
const stopover = leg.stopovers[i] const stopover = leg.stopovers[i]
if (!stopover) continue if (!stopover) continue
if (Array.isArray(stopover.remarks)) { addRemark(stopover, remark)
stopover.remarks.push(remark)
} else {
stopover.remarks = [remark]
}
} }
continue continue
} }
} }
if (Array.isArray(leg.remarks)) leg.remarks.push(remark) addRemark(leg, remark)
else leg.remarks = [remark]
// todo: `ref.tagL` // todo: `ref.tagL`
} }
} }
const createParseJourneyLeg = (profile, opt, data) => { const createParseJourneyLeg = (profile, opt, data) => {
const {hints, warnings, polylines} = data const {polylines} = data
// todo: pt.status, pt.isPartCncl // todo: pt.status, pt.isPartCncl
// todo: pt.isRchbl, pt.chRatingRT, pt.chgDurR, pt.minChg // todo: pt.isRchbl, pt.chRatingRT, pt.chgDurR, pt.minChg
// todo: pt.dep.dProgType, pt.arr.dProgType // 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 // https://gist.github.com/derhuerst/426d4b95aeae701843b1e9c23105b8d4#file-tripsearch-2018-12-05-http-L4207-L4229
if (opt.remarks && Array.isArray(pt.gis.msgL)) { 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') { } else if (pt.type === 'JNY') {
// todo: pull `public` value from `profile.products` // todo: pull `public` value from `profile.products`
@ -119,7 +117,7 @@ const createParseJourneyLeg = (profile, opt, data) => {
if (opt.remarks && Array.isArray(pt.jny.msgL)) { if (opt.remarks && Array.isArray(pt.jny.msgL)) {
// todo: apply leg-wide remarks if `parseStopovers` is false // 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) // filter stations the train passes without stopping, as this doesn't comply with fptf (yet)

View file

@ -1,7 +1,7 @@
'use strict' 'use strict'
const {DateTime} = require('luxon') const {DateTime} = require('luxon')
const findRemark = require('./find-remark') const findRemarks = require('./find-remarks')
const parseScheduledDays = (sDaysB, profile) => { const parseScheduledDays = (sDaysB, profile) => {
sDaysB = Buffer.from(sDaysB, 'hex') sDaysB = Buffer.from(sDaysB, 'hex')
@ -24,8 +24,6 @@ const parseScheduledDays = (sDaysB, profile) => {
const createParseJourney = (profile, opt, data) => { const createParseJourney = (profile, opt, data) => {
const parseLeg = profile.parseJourneyLeg(profile, opt, data) const parseLeg = profile.parseJourneyLeg(profile, opt, data)
const {hints, warnings} = data
// todo: c.conSubscr // todo: c.conSubscr
// todo: c.trfRes x vbb-parse-ticket // todo: c.trfRes x vbb-parse-ticket
// todo: c.sotRating, c.isSotCon, c.sotCtxt // todo: c.sotRating, c.isSotCon, c.sotCtxt
@ -53,11 +51,7 @@ const createParseJourney = (profile, opt, data) => {
} }
if (opt.remarks && Array.isArray(j.msgL)) { if (opt.remarks && Array.isArray(j.msgL)) {
res.remarks = [] res.remarks = findRemarks(j.msgL).map(([remark]) => remark)
for (let ref of j.msgL) {
const remark = findRemark(hints, warnings, ref)
if (remark) res.remarks.push(remark)
}
} }
if (opt.scheduledDays) { if (opt.scheduledDays) {

View file

@ -1,10 +1,8 @@
'use strict' 'use strict'
const findRemark = require('./find-remark') const findRemarks = require('./find-remarks')
const createParseStopover = (profile, opt, data, date) => { const createParseStopover = (profile, opt, data, date) => {
const {hints, warnings} = data
const parseStopover = (st) => { const parseStopover = (st) => {
const res = { const res = {
stop: st.location || null, stop: st.location || null,
@ -72,11 +70,7 @@ const createParseStopover = (profile, opt, data, date) => {
} }
if (opt.remarks && Array.isArray(st.msgL)) { if (opt.remarks && Array.isArray(st.msgL)) {
res.remarks = [] res.remarks = findRemarks(st.msgL).map(([remark]) => remark)
for (let ref of st.msgL) {
const remark = findRemark(hints, warnings, ref)
if (remark) res.remarks.push(remark)
}
} }
return res return res