mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 15:19:35 +02:00
parseIcon, parseHint, parseLocation: handle more edge cases 🐛
This commit is contained in:
parent
36a8b388f2
commit
2cb6a0c32b
5 changed files with 28 additions and 3 deletions
|
@ -1,7 +1,8 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const codesByIcon = Object.assign(Object.create(null), {
|
const codesByIcon = Object.assign(Object.create(null), {
|
||||||
cancel: 'cancelled'
|
cancel: 'cancelled',
|
||||||
|
himWarn: 'disturbance',
|
||||||
})
|
})
|
||||||
|
|
||||||
const linkTypesByCode = Object.assign(Object.create(null), {
|
const linkTypesByCode = Object.assign(Object.create(null), {
|
||||||
|
@ -29,7 +30,8 @@ const parseHint = (ctx, h) => {
|
||||||
|
|
||||||
if (h.type === 'I' && h.code && h.txtN) {
|
if (h.type === 'I' && h.code && h.txtN) {
|
||||||
if (h.code in linkTypesByCode) {
|
if (h.code in linkTypesByCode) {
|
||||||
return {type: linkTypesByCode[h.code], text: h.txtN}
|
const text = h.txtN === 'NULL' ? null : h.txtN
|
||||||
|
return {type: linkTypesByCode[h.code], text}
|
||||||
}
|
}
|
||||||
if (h.code === 'TW' && h.txtN[0] === '$') {
|
if (h.code === 'TW' && h.txtN[0] === '$') {
|
||||||
return {type: 'local-fare-zone', text: h.txtN.slice(1)}
|
return {type: 'local-fare-zone', text: h.txtN.slice(1)}
|
||||||
|
@ -70,7 +72,7 @@ const parseHint = (ctx, h) => {
|
||||||
|
|
||||||
if (
|
if (
|
||||||
h.type === 'D' || h.type === 'U' || h.type === 'R' || h.type === 'N' ||
|
h.type === 'D' || h.type === 'U' || h.type === 'R' || h.type === 'N' ||
|
||||||
h.type === 'Y' || h.type === 'Q'
|
h.type === 'Y' || h.type === 'Q' || h.type === 'P'
|
||||||
) {
|
) {
|
||||||
// todo: how can we identify the individual types?
|
// todo: how can we identify the individual types?
|
||||||
// todo: does `D` mean "disturbance"?
|
// todo: does `D` mean "disturbance"?
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const parseIcon = (ctx, i) => {
|
const parseIcon = (ctx, i) => {
|
||||||
|
if (i.res === 'Empty') return null
|
||||||
const res = {
|
const res = {
|
||||||
type: i.res || null,
|
type: i.res || null,
|
||||||
title: i.text || i.txt || i.txtS || null
|
title: i.text || i.txt || i.txtS || null
|
||||||
|
|
|
@ -24,6 +24,9 @@ const parseLocation = (ctx, l) => {
|
||||||
if (l.crd) {
|
if (l.crd) {
|
||||||
res.latitude = l.crd.y / 1000000
|
res.latitude = l.crd.y / 1000000
|
||||||
res.longitude = l.crd.x / 1000000
|
res.longitude = l.crd.x / 1000000
|
||||||
|
} else if (('X' in lid) && ('Y' in lid)) {
|
||||||
|
res.latitude = lid.Y / 1000000
|
||||||
|
res.longitude = lid.X / 1000000
|
||||||
}
|
}
|
||||||
|
|
||||||
if (l.type === STATION) {
|
if (l.type === STATION) {
|
||||||
|
@ -70,6 +73,7 @@ const parseLocation = (ctx, l) => {
|
||||||
const i = text.indexOf(':')
|
const i = text.indexOf(':')
|
||||||
return [text.slice(0, i), text.slice(i + 1)]
|
return [text.slice(0, i), text.slice(i + 1)]
|
||||||
})
|
})
|
||||||
|
.filter(([src]) => src !== 'NULL')
|
||||||
if (otherIds.length > 0) {
|
if (otherIds.length > 0) {
|
||||||
if (!stop.ids) stop.ids = {}
|
if (!stop.ids) stop.ids = {}
|
||||||
for (const [src, id] of otherIds) stop.ids[src] = id
|
for (const [src, id] of otherIds) stop.ids[src] = id
|
||||||
|
|
|
@ -66,5 +66,11 @@ test('parses icons correctly', (t) => {
|
||||||
fgColor: {r: 255, g: 255, b: 255, a: 255},
|
fgColor: {r: 255, g: 255, b: 255, a: 255},
|
||||||
bgColor: {r: 0, g: 51, b: 153, a: 255}
|
bgColor: {r: 0, g: 51, b: 153, a: 255}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const empty = {
|
||||||
|
"res": "Empty"
|
||||||
|
}
|
||||||
|
t.equal(parse(ctx, empty), null)
|
||||||
|
|
||||||
t.end()
|
t.end()
|
||||||
})
|
})
|
||||||
|
|
|
@ -107,3 +107,15 @@ test('parses a stop correctly', (t) => {
|
||||||
|
|
||||||
t.end()
|
t.end()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('falls back to coordinates from `lid`', (t) => {
|
||||||
|
const {location} = parse(ctx, {
|
||||||
|
type: 'S',
|
||||||
|
name: 'foo',
|
||||||
|
lid: 'a=b@L=foo@X=12345678@Y=23456789'
|
||||||
|
})
|
||||||
|
t.ok(location)
|
||||||
|
t.equal(location.latitude, 23.456789)
|
||||||
|
t.equal(location.longitude, 12.345678)
|
||||||
|
t.end()
|
||||||
|
})
|
||||||
|
|
Loading…
Add table
Reference in a new issue