parseProducts: move factory from profiles into main code

This commit is contained in:
Jannis R 2018-03-16 15:38:16 +01:00
parent b7c1ee3b05
commit ce43f15ad5
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
7 changed files with 27 additions and 20 deletions

View file

@ -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 = {}) => {

View file

@ -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

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -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
}
}