mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 07:09:35 +02:00
fix products parsing 🐛, fix tests for it ✅
This commit is contained in:
parent
f7b9cdfac6
commit
73c419fadb
8 changed files with 42 additions and 24 deletions
|
@ -140,7 +140,7 @@ const dbProfile = {
|
|||
|
||||
// todo: parseLocation
|
||||
parseLine: createParseLine,
|
||||
parseProducts: createParseBitmask(modes.bitmasks),
|
||||
parseProducts: createParseBitmask(modes.allProducts, defaultProducts),
|
||||
parseJourney: createParseJourney,
|
||||
|
||||
formatStation,
|
||||
|
|
|
@ -71,7 +71,7 @@ const insaProfile = {
|
|||
transformReqBody,
|
||||
|
||||
defaultProducts,
|
||||
parseProducts: createParseBitmask(products.bitmasks),
|
||||
parseProducts: createParseBitmask(products.allProducts, defaultProducts),
|
||||
formatProducts,
|
||||
|
||||
parseLine: createParseLine
|
||||
|
|
|
@ -113,7 +113,7 @@ const oebbProfile = {
|
|||
|
||||
products: products.allProducts,
|
||||
|
||||
parseProducts: createParseBitmask(products.bitmasks),
|
||||
parseProducts: createParseBitmask(products.allProducts, defaultProducts),
|
||||
parseLine: createParseLine,
|
||||
parseLocation,
|
||||
parseMovement: createParseMovement,
|
||||
|
|
|
@ -181,7 +181,7 @@ const vbbProfile = {
|
|||
parseStationName: shorten,
|
||||
parseLocation,
|
||||
parseLine: createParseLine,
|
||||
parseProducts: createParseBitmask(modes.bitmasks),
|
||||
parseProducts: createParseBitmask(modes.allProducts, defaultProducts),
|
||||
parseJourney: createParseJourney,
|
||||
parseDeparture: createParseDeparture,
|
||||
parseStopover: createParseStopover,
|
||||
|
|
|
@ -1,14 +1,29 @@
|
|||
'use strict'
|
||||
|
||||
const createParseBitmask = (bitmasks) => {
|
||||
const createParseBitmask = (allProducts, defaultProducts) => {
|
||||
allProducts = allProducts.sort((p1, p2) => p2.bitmask - p1.bitmask) // desc
|
||||
if (allProducts.length === 0) throw new Error('allProducts is empty.')
|
||||
for (let product of allProducts) {
|
||||
if ('string' !== typeof product.product) {
|
||||
throw new Error('allProducts[].product must be a string.')
|
||||
}
|
||||
if ('number' !== typeof product.bitmask) {
|
||||
throw new Error(product.product + '.bitmask must be a number.')
|
||||
}
|
||||
}
|
||||
|
||||
const parseBitmask = (bitmask) => {
|
||||
const products = {}
|
||||
let i = 1
|
||||
do {
|
||||
products[bitmasks[i].product] = products[bitmasks[i].product] || !!(bitmask & i)
|
||||
i *= 2
|
||||
} while (bitmasks[i] && bitmasks[i].product)
|
||||
return products
|
||||
const res = Object.assign({}, defaultProducts)
|
||||
|
||||
for (let product of allProducts) {
|
||||
if (bitmask === 0) break
|
||||
if ((product.bitmask & bitmask) > 0) {
|
||||
res[product.product] = true
|
||||
bitmask -= product.bitmask
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
return parseBitmask
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ const isRoughlyEqual = require('is-roughly-equal')
|
|||
const co = require('./co')
|
||||
const createClient = require('..')
|
||||
const dbProfile = require('../p/db')
|
||||
const modes = require('../p/db/modes')
|
||||
const {allProducts} = require('../p/db/modes')
|
||||
const {
|
||||
assertValidStation,
|
||||
assertValidPoi,
|
||||
|
@ -69,11 +69,12 @@ const assertIsJungfernheide = (t, s) => {
|
|||
t.ok(isRoughlyEqual(s.location.longitude, 13.299424, .0005))
|
||||
}
|
||||
|
||||
// todo: this doesnt seem to work
|
||||
// todo: DRY with assertValidStationProducts
|
||||
// todo: DRY with other tests
|
||||
const assertValidProducts = (t, p) => {
|
||||
for (let k of Object.keys(modes)) {
|
||||
t.ok('boolean', typeof modes[k], 'mode ' + k + ' must be a boolean')
|
||||
for (let product of allProducts) {
|
||||
product = product.product // wat
|
||||
t.equal(typeof p[product], 'boolean', 'product ' + p + ' must be a boolean')
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ const validateFptf = require('validate-fptf')
|
|||
const co = require('./co')
|
||||
const createClient = require('..')
|
||||
const insaProfile = require('../p/insa')
|
||||
const products = require('../p/insa/products')
|
||||
const {allProducts} = require('../p/insa/products')
|
||||
const {
|
||||
assertValidStation,
|
||||
assertValidPoi,
|
||||
|
@ -56,11 +56,12 @@ const assertIsMagdeburgHbf = (t, s) => {
|
|||
t.ok(isRoughlyEqual(s.location.longitude, 11.626891, 0.001))
|
||||
}
|
||||
|
||||
// todo: this doesnt seem to work
|
||||
// todo: DRY with assertValidStationProducts
|
||||
// todo: DRY with other tests
|
||||
const assertValidProducts = (t, p) => {
|
||||
for (let k of Object.keys(products)) {
|
||||
t.ok('boolean', typeof products[k], 'mode ' + k + ' must be a boolean')
|
||||
for (let product of allProducts) {
|
||||
product = product.product // wat
|
||||
t.equal(typeof p[product], 'boolean', 'product ' + p + ' must be a boolean')
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ const validateLineWithoutMode = require('./validate-line-without-mode')
|
|||
const co = require('./co')
|
||||
const createClient = require('..')
|
||||
const oebbProfile = require('../p/oebb')
|
||||
const products = require('../p/oebb/products')
|
||||
const {allProducts} = require('../p/oebb/products')
|
||||
const {
|
||||
assertValidStation,
|
||||
assertValidPoi,
|
||||
|
@ -73,11 +73,12 @@ const assertIsSalzburgHbf = (t, s) => {
|
|||
t.ok(isRoughlyEqual(s.location.longitude, 13.045604, .0005))
|
||||
}
|
||||
|
||||
// todo: this doesnt seem to work
|
||||
// todo: DRY with assertValidStationProducts
|
||||
// todo: DRY with other tests
|
||||
const assertValidProducts = (t, p) => {
|
||||
for (let k of Object.keys(products)) {
|
||||
t.ok('boolean', typeof products[k], 'mode ' + k + ' must be a boolean')
|
||||
for (let product of allProducts) {
|
||||
product = product.product // wat
|
||||
t.equal(typeof p[product], 'boolean', 'product ' + p + ' must be a boolean')
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue