From 2cb6a0c32b17cb25bf379ad9e633007112027453 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Wed, 18 Mar 2020 21:37:31 +0100 Subject: [PATCH] parseIcon, parseHint, parseLocation: handle more edge cases :bug: --- parse/hint.js | 8 +++++--- parse/icon.js | 1 + parse/location.js | 4 ++++ test/parse/icon.js | 6 ++++++ test/parse/location.js | 12 ++++++++++++ 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/parse/hint.js b/parse/hint.js index 0288d5e5..332bc033 100644 --- a/parse/hint.js +++ b/parse/hint.js @@ -1,7 +1,8 @@ 'use strict' const codesByIcon = Object.assign(Object.create(null), { - cancel: 'cancelled' + cancel: 'cancelled', + himWarn: 'disturbance', }) 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.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] === '$') { return {type: 'local-fare-zone', text: h.txtN.slice(1)} @@ -70,7 +72,7 @@ const parseHint = (ctx, h) => { if ( 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: does `D` mean "disturbance"? diff --git a/parse/icon.js b/parse/icon.js index 191ef8eb..14e0180d 100644 --- a/parse/icon.js +++ b/parse/icon.js @@ -1,6 +1,7 @@ 'use strict' const parseIcon = (ctx, i) => { + if (i.res === 'Empty') return null const res = { type: i.res || null, title: i.text || i.txt || i.txtS || null diff --git a/parse/location.js b/parse/location.js index 232ccb0d..b9622d9e 100644 --- a/parse/location.js +++ b/parse/location.js @@ -24,6 +24,9 @@ const parseLocation = (ctx, l) => { if (l.crd) { res.latitude = l.crd.y / 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) { @@ -70,6 +73,7 @@ const parseLocation = (ctx, l) => { const i = text.indexOf(':') return [text.slice(0, i), text.slice(i + 1)] }) + .filter(([src]) => src !== 'NULL') if (otherIds.length > 0) { if (!stop.ids) stop.ids = {} for (const [src, id] of otherIds) stop.ids[src] = id diff --git a/test/parse/icon.js b/test/parse/icon.js index 634f5570..9b052140 100644 --- a/test/parse/icon.js +++ b/test/parse/icon.js @@ -66,5 +66,11 @@ test('parses icons correctly', (t) => { fgColor: {r: 255, g: 255, b: 255, a: 255}, bgColor: {r: 0, g: 51, b: 153, a: 255} }) + + const empty = { + "res": "Empty" + } + t.equal(parse(ctx, empty), null) + t.end() }) diff --git a/test/parse/location.js b/test/parse/location.js index 1b452f0a..f861268f 100644 --- a/test/parse/location.js +++ b/test/parse/location.js @@ -107,3 +107,15 @@ test('parses a stop correctly', (t) => { 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() +})