From 6b1f22cc65a7fffd6f7bd3553933b3353e2c7037 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Mon, 13 Nov 2017 00:30:14 +0100 Subject: [PATCH] parse product bitmasks --- p/db/index.js | 18 ++++++++++-------- parse/location.js | 2 +- test/db.js | 16 ++++++++++++++++ test/util.js | 1 + 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/p/db/index.js b/p/db/index.js index 7c897f20..6cf75c8a 100644 --- a/p/db/index.js +++ b/p/db/index.js @@ -82,6 +82,14 @@ const defaultProducts = { regional: true, regionalExp: true } +const formatProducts = (products) => { + products = Object.assign(Object.create(null), defaultProducts, products) + return { + type: 'PROD', + mode: 'INC', + value: modes.stringifyBitmask(products) + '' + } +} // todo: find option for absolute number of results @@ -94,16 +102,10 @@ const dbProfile = { // todo: parseLocation parseLine, + parseProducts: modes.parseBitmask, formatStation, - formatProducts: (products) => { - products = Object.assign(Object.create(null), defaultProducts, products) - return { - type: 'PROD', - mode: 'INC', - value: modes.stringifyBitmask(products) + '' - } - } + formatProducts } module.exports = dbProfile diff --git a/parse/location.js b/parse/location.js index 70d59f81..f692af91 100644 --- a/parse/location.js +++ b/parse/location.js @@ -19,7 +19,7 @@ const parseLocation = (profile, l) => { } if (type === 'poi' || type === 'station') res.id = l.extId - if ('pCls' in l) res.products = l.pCls + if ('pCls' in l) res.products = profile.parseProducts(l.pCls) return res } diff --git a/test/db.js b/test/db.js index 0bb8fcf7..3f238959 100644 --- a/test/db.js +++ b/test/db.js @@ -6,6 +6,7 @@ const isRoughlyEqual = require('is-roughly-equal') const createClient = require('..') const dbProfile = require('../p/db') +const modes = require('../p/db/modes') const { findStation, assertValidStation, @@ -35,6 +36,12 @@ const assertIsJungfernheide = (t, s) => { t.ok(isRoughlyEqual(s.coordinates.longitude, 13.299424, .0005)) } +const assertValidProducts = (t, p) => { + for (let k of Object.keys(modes)) { + t.ok('boolean', typeof modes[k], 'mode ' + k + ' must be a boolean') + } +} + const test = tapePromise(tape) const client = createClient(dbProfile) @@ -50,12 +57,18 @@ test('Berlin Jungfernheide to München Hbf', async (t) => { if (!await findStation(journey.origin.id)) { console.error('unknown station', journey.origin.id, journey.origin.name) } + if (journey.origin.products) { + assertValidProducts(t, journey.origin.products) + } t.ok(isValidWhen(journey.departure)) assertValidStation(t, journey.destination) if (!await findStation(journey.origin.id)) { console.error('unknown station', journey.destination.id, journey.destination.name) } + if (journey.destination.products) { + assertValidProducts(t, journey.destination.products) + } t.ok(isValidWhen(journey.arrival)) t.ok(Array.isArray(journey.parts)) @@ -100,6 +113,7 @@ test('Berlin Jungfernheide to Torfstraße 17', async (t) => { if (!await findStation(part.origin.id)) { console.error('unknown station', part.origin.id, part.origin.name) } + if (part.origin.products) assertValidProducts(t, part.origin.products) t.ok(isValidWhen(part.departure)) t.ok(isValidWhen(part.arrival)) @@ -127,6 +141,7 @@ test('Berlin Jungfernheide to ATZE Musiktheater', async (t) => { if (!await findStation(part.origin.id)) { console.error('unknown station', part.origin.id, part.origin.name) } + if (part.origin.products) assertValidProducts(t, part.origin.products) t.ok(isValidWhen(part.departure)) t.ok(isValidWhen(part.arrival)) @@ -150,6 +165,7 @@ test('departures at Berlin Jungfernheide', async (t) => { if (!await findStation(dep.station.id)) { console.error('unknown station', dep.station.id, dep.station.name) } + if (dep.station.products) assertValidProducts(t, dep.station.products) t.ok(isValidWhen(dep.when)) } diff --git a/test/util.js b/test/util.js index e5d590fe..85a0871b 100644 --- a/test/util.js +++ b/test/util.js @@ -57,6 +57,7 @@ const isValidMode = (m) => { const assertValidLine = (t, l) => { t.equal(l.type, 'line') t.equal(typeof l.name, 'string') + if (!isValidMode(l.mode)) console.error(l) t.ok(isValidMode(l.mode), 'invalid mode ' + l.mode) t.equal(typeof l.product, 'string') }