From ce43f15ad50181455f1efe8bfd0db304d677bc6f Mon Sep 17 00:00:00 2001 From: Jannis R Date: Fri, 16 Mar 2018 15:38:16 +0100 Subject: [PATCH] parseProducts: move factory from profiles into main code --- index.js | 4 +++- lib/validate-profile.js | 5 +++++ p/db/index.js | 2 -- p/insa/index.js | 2 -- p/oebb/index.js | 2 -- p/vbb/index.js | 2 -- parse/products-bitmask.js | 30 +++++++++++++++++++----------- 7 files changed, 27 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 158d0151..44a296d1 100644 --- a/index.js +++ b/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 = {}) => { diff --git a/lib/validate-profile.js b/lib/validate-profile.js index 19465c3f..2a8ac81b 100644 --- a/lib/validate-profile.js +++ b/lib/validate-profile.js @@ -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 diff --git a/p/db/index.js b/p/db/index.js index 2f6870c5..d6fb2708 100644 --- a/p/db/index.js +++ b/p/db/index.js @@ -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, diff --git a/p/insa/index.js b/p/insa/index.js index 8b7bea1b..948da700 100644 --- a/p/insa/index.js +++ b/p/insa/index.js @@ -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, diff --git a/p/oebb/index.js b/p/oebb/index.js index 5196ab49..c0da7427 100644 --- a/p/oebb/index.js +++ b/p/oebb/index.js @@ -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, diff --git a/p/vbb/index.js b/p/vbb/index.js index 10b5e743..b58d819b 100644 --- a/p/vbb/index.js +++ b/p/vbb/index.js @@ -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, diff --git a/parse/products-bitmask.js b/parse/products-bitmask.js index 5dc5e8bc..f8522dc6 100644 --- a/parse/products-bitmask.js +++ b/parse/products-bitmask.js @@ -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 } }