db-vendo-client/parse/arrival-or-departure.js

60 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-06-26 15:49:50 +02:00
'use strict'
const parseWhen = require('./when')
const parsePlatform = require('./platform')
const findRemarks = require('./find-remarks')
2018-06-26 15:49:50 +02:00
const ARRIVAL = 'a'
const DEPARTURE = 'd'
2018-06-26 15:49:50 +02:00
// todo: what is d.jny.dirFlg?
// todo: d.stbStop.dProgType/d.stbStop.aProgType
const createParseArrOrDep = (profile, opt, data, prefix) => {
if (prefix !== ARRIVAL && prefix !== DEPARTURE) throw new Error('invalid prefix')
2018-06-26 15:49:50 +02:00
const parseArrOrDep = (d) => {
const tPlanned = d.stbStop[prefix + 'TimeS']
const tPrognosed = d.stbStop[prefix + 'TimeR']
const tzOffset = d.stbStop[prefix + 'TZOffset'] || null
const cancelled = !!d.stbStop[prefix + 'Cncl']
2018-06-26 15:49:50 +02:00
const res = {
tripId: d.jid,
stop: d.stbStop.location || null,
...parseWhen(profile, d.date, tPlanned, tPrognosed, tzOffset, cancelled),
...parsePlatform(profile, d.stbStop[prefix + 'PlatfS'], d.stbStop[prefix + 'PlatfR'], cancelled),
// todo: for arrivals, this is the *origin*, not the *direction*
2019-04-01 19:22:10 +02:00
direction: prefix === DEPARTURE && d.dirTxt && profile.parseStationName(d.dirTxt) || null,
2019-08-23 16:06:21 +02:00
line: d.line || null,
remarks: []
2018-06-26 15:49:50 +02:00
}
if (cancelled) {
2018-06-26 15:49:50 +02:00
res.cancelled = true
Object.defineProperty(res, 'canceled', {value: true})
}
2018-06-28 13:45:56 +02:00
if (opt.remarks) {
res.remarks = findRemarks([
...(d.remL || []),
...(d.msgL || [])
]).map(([remark]) => remark)
2018-06-28 13:45:56 +02:00
}
if (opt.stopovers && Array.isArray(d.stopL)) {
const parse = profile.parseStopover(profile, opt, data, d.date)
// Filter stations the train passes without stopping, as this doesn't comply with FPTF (yet).
const stopovers = d.stopL.map(parse).filter(st => !st.passBy)
if (prefix === ARRIVAL) res.previousStopovers = stopovers
else if (prefix === DEPARTURE) res.nextStopovers = stopovers
}
2018-06-26 15:49:50 +02:00
return res
}
return parseArrOrDep
}
module.exports = createParseArrOrDep