db-vendo-client/parse/hint.js

90 lines
2 KiB
JavaScript
Raw Normal View History

2017-11-11 22:35:41 +01:00
'use strict'
const codesByIcon = Object.assign(Object.create(null), {
cancel: 'cancelled',
himWarn: 'disturbance',
})
const linkTypesByCode = Object.assign(Object.create(null), {
BB: 'stop-website',
// IFOPT-based DHID
// https://wiki.openstreetmap.org/wiki/DE:Key:ref:IFOPT
// https://trid.trb.org/view/1435440
IF: 'stop-dhid',
OZ: 'transit-authority',
// todo: `{type: 'I',code: 'TD',icoX: 1,txtN: '8010224'}`
// todo: `{type: 'I',code: 'TE',icoX: 1,txtN: '8024001'}`
})
2019-06-21 18:43:10 +02:00
// todo: pass in tag list from hint reference, e.g.:
// "tagL": [
// "RES_JNY_H3" // H3 = level 3 heading? shown on overview
// ]
// "tagL": [
// "RES_JNY_DTL" // only shown in journey detail
// ]
// todo: https://github.com/public-transport/hafas-client/issues/5
2020-03-18 21:35:43 +01:00
// todo: expose h.type somehow
const parseHint = (ctx, h) => {
2018-06-01 11:12:54 +02:00
// todo: C
2018-06-11 18:41:53 +02:00
if (h.type === 'I' && h.code && h.txtN) {
if (h.code in linkTypesByCode) {
const text = h.txtN === 'NULL' ? null : h.txtN
return {type: linkTypesByCode[h.code], text}
}
if (h.code === 'TW' && h.txtN[0] === '$') {
return {type: 'local-fare-zone', text: h.txtN.slice(1)}
}
if (h.code === 'TW' && h.txtN[0] === '#') {
return {type: 'foreign-id', text: h.txtN.slice(1)}
}
}
2018-06-11 18:41:53 +02:00
const text = h.txtN && h.txtN.trim() || ''
2019-08-23 16:02:34 +02:00
const icon = h.icon || null
2018-10-27 14:50:45 +02:00
const code = h.code || (icon && icon.type && codesByIcon[icon.type]) || null
2018-06-07 15:41:00 +02:00
2018-06-11 19:50:44 +02:00
if (h.type === 'M') {
return {
type: 'status',
summary: h.txtS && h.txtS.trim() || '',
code,
2018-06-11 19:50:44 +02:00
text
}
}
if (h.type === 'L') {
2018-06-01 11:12:54 +02:00
return {
type: 'status',
code: 'alternative-trip',
2018-06-11 18:41:53 +02:00
text,
tripId: h.jid
2018-06-01 11:12:54 +02:00
}
}
2018-10-28 16:37:19 +01:00
if (h.type === 'A' || h.type === 'I') {
return {
type: 'hint',
code,
text
}
2018-06-07 15:41:00 +02:00
}
2019-11-24 16:59:50 +01:00
if (
h.type === 'D' || h.type === 'U' || h.type === 'R' || h.type === 'N' ||
h.type === 'Y' || h.type === 'Q' || h.type === 'P'
2019-11-24 16:59:50 +01:00
) {
2018-06-01 11:12:54 +02:00
// todo: how can we identify the individual types?
2018-07-02 18:48:23 +02:00
// todo: does `D` mean "disturbance"?
2018-06-01 11:12:54 +02:00
return {
type: 'status',
code,
2018-06-11 18:41:53 +02:00
text
2018-06-01 11:12:54 +02:00
}
}
2018-07-02 18:48:23 +02:00
2018-06-01 11:12:54 +02:00
return null
2017-11-11 22:35:41 +01:00
}
module.exports = parseHint