mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 07:09:35 +02:00
parse product bitmasks
This commit is contained in:
parent
4356032308
commit
6b1f22cc65
4 changed files with 28 additions and 9 deletions
|
@ -82,6 +82,14 @@ const defaultProducts = {
|
||||||
regional: true,
|
regional: true,
|
||||||
regionalExp: 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
|
// todo: find option for absolute number of results
|
||||||
|
|
||||||
|
@ -94,16 +102,10 @@ const dbProfile = {
|
||||||
|
|
||||||
// todo: parseLocation
|
// todo: parseLocation
|
||||||
parseLine,
|
parseLine,
|
||||||
|
parseProducts: modes.parseBitmask,
|
||||||
|
|
||||||
formatStation,
|
formatStation,
|
||||||
formatProducts: (products) => {
|
formatProducts
|
||||||
products = Object.assign(Object.create(null), defaultProducts, products)
|
|
||||||
return {
|
|
||||||
type: 'PROD',
|
|
||||||
mode: 'INC',
|
|
||||||
value: modes.stringifyBitmask(products) + ''
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = dbProfile
|
module.exports = dbProfile
|
||||||
|
|
|
@ -19,7 +19,7 @@ const parseLocation = (profile, l) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === 'poi' || type === 'station') res.id = l.extId
|
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
|
return res
|
||||||
}
|
}
|
||||||
|
|
16
test/db.js
16
test/db.js
|
@ -6,6 +6,7 @@ const isRoughlyEqual = require('is-roughly-equal')
|
||||||
|
|
||||||
const createClient = require('..')
|
const createClient = require('..')
|
||||||
const dbProfile = require('../p/db')
|
const dbProfile = require('../p/db')
|
||||||
|
const modes = require('../p/db/modes')
|
||||||
const {
|
const {
|
||||||
findStation,
|
findStation,
|
||||||
assertValidStation,
|
assertValidStation,
|
||||||
|
@ -35,6 +36,12 @@ const assertIsJungfernheide = (t, s) => {
|
||||||
t.ok(isRoughlyEqual(s.coordinates.longitude, 13.299424, .0005))
|
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 test = tapePromise(tape)
|
||||||
const client = createClient(dbProfile)
|
const client = createClient(dbProfile)
|
||||||
|
|
||||||
|
@ -50,12 +57,18 @@ test('Berlin Jungfernheide to München Hbf', async (t) => {
|
||||||
if (!await findStation(journey.origin.id)) {
|
if (!await findStation(journey.origin.id)) {
|
||||||
console.error('unknown station', journey.origin.id, journey.origin.name)
|
console.error('unknown station', journey.origin.id, journey.origin.name)
|
||||||
}
|
}
|
||||||
|
if (journey.origin.products) {
|
||||||
|
assertValidProducts(t, journey.origin.products)
|
||||||
|
}
|
||||||
t.ok(isValidWhen(journey.departure))
|
t.ok(isValidWhen(journey.departure))
|
||||||
|
|
||||||
assertValidStation(t, journey.destination)
|
assertValidStation(t, journey.destination)
|
||||||
if (!await findStation(journey.origin.id)) {
|
if (!await findStation(journey.origin.id)) {
|
||||||
console.error('unknown station', journey.destination.id, journey.destination.name)
|
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(isValidWhen(journey.arrival))
|
||||||
|
|
||||||
t.ok(Array.isArray(journey.parts))
|
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)) {
|
if (!await findStation(part.origin.id)) {
|
||||||
console.error('unknown station', part.origin.id, part.origin.name)
|
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.departure))
|
||||||
t.ok(isValidWhen(part.arrival))
|
t.ok(isValidWhen(part.arrival))
|
||||||
|
|
||||||
|
@ -127,6 +141,7 @@ test('Berlin Jungfernheide to ATZE Musiktheater', async (t) => {
|
||||||
if (!await findStation(part.origin.id)) {
|
if (!await findStation(part.origin.id)) {
|
||||||
console.error('unknown station', part.origin.id, part.origin.name)
|
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.departure))
|
||||||
t.ok(isValidWhen(part.arrival))
|
t.ok(isValidWhen(part.arrival))
|
||||||
|
|
||||||
|
@ -150,6 +165,7 @@ test('departures at Berlin Jungfernheide', async (t) => {
|
||||||
if (!await findStation(dep.station.id)) {
|
if (!await findStation(dep.station.id)) {
|
||||||
console.error('unknown station', dep.station.id, dep.station.name)
|
console.error('unknown station', dep.station.id, dep.station.name)
|
||||||
}
|
}
|
||||||
|
if (dep.station.products) assertValidProducts(t, dep.station.products)
|
||||||
t.ok(isValidWhen(dep.when))
|
t.ok(isValidWhen(dep.when))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,7 @@ const isValidMode = (m) => {
|
||||||
const assertValidLine = (t, l) => {
|
const assertValidLine = (t, l) => {
|
||||||
t.equal(l.type, 'line')
|
t.equal(l.type, 'line')
|
||||||
t.equal(typeof l.name, 'string')
|
t.equal(typeof l.name, 'string')
|
||||||
|
if (!isValidMode(l.mode)) console.error(l)
|
||||||
t.ok(isValidMode(l.mode), 'invalid mode ' + l.mode)
|
t.ok(isValidMode(l.mode), 'invalid mode ' + l.mode)
|
||||||
t.equal(typeof l.product, 'string')
|
t.equal(typeof l.product, 'string')
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue