mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 15:19:35 +02:00
parseProducts: move factory from profiles into main code
This commit is contained in:
parent
b7c1ee3b05
commit
ce43f15ad5
7 changed files with 27 additions and 20 deletions
4
index.js
4
index.js
|
@ -3,8 +3,9 @@
|
|||
const minBy = require('lodash/minBy')
|
||||
const maxBy = require('lodash/maxBy')
|
||||
|
||||
const validateProfile = require('./lib/validate-profile')
|
||||
const defaultProfile = require('./lib/default-profile')
|
||||
const createParseBitmask = require('./parse/products-bitmask')
|
||||
const validateProfile = require('./lib/validate-profile')
|
||||
const _request = require('./lib/request')
|
||||
|
||||
const isObj = o => o !== null && 'object' === typeof o && !Array.isArray(o)
|
||||
|
@ -12,6 +13,7 @@ const isNonEmptyString = str => 'string' === typeof str && str.length > 0
|
|||
|
||||
const createClient = (profile, request = _request) => {
|
||||
profile = Object.assign({}, defaultProfile, profile)
|
||||
profile.parseProducts = createParseBitmask(profile)
|
||||
validateProfile(profile)
|
||||
|
||||
const departures = (station, opt = {}) => {
|
||||
|
|
|
@ -47,6 +47,11 @@ const validateProfile = (profile) => {
|
|||
throw new Error(`profile.${key} must not be null.`)
|
||||
}
|
||||
}
|
||||
|
||||
if (!Array.isArray(profile.products)) {
|
||||
throw new Error('profile.products must be an array.')
|
||||
}
|
||||
if (profile.products.length === 0) throw new Error('profile.products is empty.')
|
||||
}
|
||||
|
||||
module.exports = validateProfile
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
const _createParseLine = require('../../parse/line')
|
||||
const _createParseJourney = require('../../parse/journey')
|
||||
const _formatStation = require('../../format/station')
|
||||
const createParseBitmask = require('../../parse/products-bitmask')
|
||||
const createFormatBitmask = require('../../format/products-bitmask')
|
||||
const {bike} = require('../../format/filters')
|
||||
|
||||
|
@ -141,7 +140,6 @@ const dbProfile = {
|
|||
|
||||
// todo: parseLocation
|
||||
parseLine: createParseLine,
|
||||
parseProducts: createParseBitmask(products.allProducts, defaultProducts),
|
||||
parseJourney: createParseJourney,
|
||||
|
||||
formatStation,
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
const _createParseLine = require('../../parse/line')
|
||||
const products = require('./products')
|
||||
const createParseBitmask = require('../../parse/products-bitmask')
|
||||
const createFormatBitmask = require('../../format/products-bitmask')
|
||||
|
||||
const defaultProducts = {
|
||||
|
@ -70,7 +69,6 @@ const insaProfile = {
|
|||
transformReqBody,
|
||||
|
||||
products: products.allProducts,
|
||||
parseProducts: createParseBitmask(products.allProducts, defaultProducts),
|
||||
formatProducts,
|
||||
|
||||
parseLine: createParseLine,
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
// todo: https://gist.github.com/anonymous/a5fc856bc80ae7364721943243f934f4#file-haf_config_base-properties-L5
|
||||
// todo: https://gist.github.com/anonymous/a5fc856bc80ae7364721943243f934f4#file-haf_config_base-properties-L47-L234
|
||||
|
||||
const createParseBitmask = require('../../parse/products-bitmask')
|
||||
const createFormatBitmask = require('../../format/products-bitmask')
|
||||
const _createParseLine = require('../../parse/line')
|
||||
const _parseLocation = require('../../parse/location')
|
||||
|
@ -113,7 +112,6 @@ const oebbProfile = {
|
|||
|
||||
products: products.allProducts,
|
||||
|
||||
parseProducts: createParseBitmask(products.allProducts, defaultProducts),
|
||||
parseLine: createParseLine,
|
||||
parseLocation,
|
||||
parseMovement: createParseMovement,
|
||||
|
|
|
@ -12,7 +12,6 @@ const _createParseJourney = require('../../parse/journey')
|
|||
const _createParseStopover = require('../../parse/stopover')
|
||||
const _createParseDeparture = require('../../parse/departure')
|
||||
const _formatStation = require('../../format/station')
|
||||
const createParseBitmask = require('../../parse/products-bitmask')
|
||||
const createFormatBitmask = require('../../format/products-bitmask')
|
||||
|
||||
const products = require('./products')
|
||||
|
@ -181,7 +180,6 @@ const vbbProfile = {
|
|||
parseStationName: shorten,
|
||||
parseLocation,
|
||||
parseLine: createParseLine,
|
||||
parseProducts: createParseBitmask(products.allProducts, defaultProducts),
|
||||
parseJourney: createParseJourney,
|
||||
parseDeparture: createParseDeparture,
|
||||
parseStopover: createParseStopover,
|
||||
|
|
|
@ -1,25 +1,33 @@
|
|||
'use strict'
|
||||
|
||||
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) {
|
||||
const createParseBitmask = (profile) => {
|
||||
const defaultProducts = {}
|
||||
let withBitmask = []
|
||||
for (let product of profile.products) {
|
||||
if ('string' !== typeof product.product) {
|
||||
throw new Error('allProducts[].product must be a string.')
|
||||
throw new Error('profile.products[].product must be a string.')
|
||||
}
|
||||
if ('number' !== typeof product.bitmask) {
|
||||
throw new Error(product.product + '.bitmask must be a number.')
|
||||
|
||||
defaultProducts[product.product] = false
|
||||
if (!Array.isArray(product.bitmasks)) {
|
||||
throw new Error(product.product + '.bitmasks must be an array.')
|
||||
}
|
||||
for (let bitmask of product.bitmasks) {
|
||||
if ('number' !== typeof bitmask) {
|
||||
throw new Error(product.product + '.bitmasks[] must be a number.')
|
||||
}
|
||||
withBitmask.push([bitmask, product])
|
||||
}
|
||||
}
|
||||
withBitmask.sort((a, b) => b[0] - a[0]) // descending
|
||||
|
||||
const parseBitmask = (bitmask) => {
|
||||
const res = Object.assign({}, defaultProducts)
|
||||
|
||||
for (let product of allProducts) {
|
||||
if (bitmask === 0) break
|
||||
if ((product.bitmask & bitmask) > 0) {
|
||||
for (let [pBitmask, product] of withBitmask) {
|
||||
if ((pBitmask & bitmask) > 0) {
|
||||
res[product.product] = true
|
||||
bitmask -= product.bitmask
|
||||
bitmask -= pBitmask
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue