From 773035c05df7fb2c6681d1e80e610a68c355ef15 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Thu, 31 Oct 2019 19:46:30 +0100 Subject: [PATCH] call formatProductsFilter via profile, use products from ctx.profile :boom: --- format/products-filter.js | 40 ++++++++++++++++------------------ index.js | 12 ++++------ lib/default-profile.js | 2 ++ lib/validate-profile.js | 1 + test/format/products-filter.js | 11 +++++----- 5 files changed, 31 insertions(+), 35 deletions(-) diff --git a/format/products-filter.js b/format/products-filter.js index b539de23..ad1344d0 100644 --- a/format/products-filter.js +++ b/format/products-filter.js @@ -4,34 +4,32 @@ const isObj = require('lodash/isObject') const hasProp = (o, k) => Object.prototype.hasOwnProperty.call(o, k) -const createFormatProductsFilter = (profile) => { +const formatProductsFilter = (ctx, filter) => { + if (!isObj(filter)) throw new TypeError('products filter must be an object') + const {profile} = ctx + const byProduct = {} const defaultProducts = {} for (let product of profile.products) { byProduct[product.id] = product defaultProducts[product.id] = product.default } + filter = Object.assign({}, defaultProducts, filter) - const formatProductsFilter = (filter) => { - if (!isObj(filter)) throw new TypeError('products filter must be an object') - filter = Object.assign({}, defaultProducts, filter) - - let res = 0, products = 0 - for (let product in filter) { - if (!hasProp(filter, product) || filter[product] !== true) continue - if (!byProduct[product]) throw new TypeError('unknown product ' + product) - products++ - for (let bitmask of byProduct[product].bitmasks) res = res ^ bitmask - } - if (products === 0) throw new Error('no products used') - - return { - type: 'PROD', - mode: 'INC', - value: res + '' - } + let res = 0, products = 0 + for (let product in filter) { + if (!hasProp(filter, product) || filter[product] !== true) continue + if (!byProduct[product]) throw new TypeError('unknown product ' + product) + products++ + for (let bitmask of byProduct[product].bitmasks) res = res | bitmask + } + if (products === 0) throw new Error('no products used') + + return { + type: 'PROD', + mode: 'INC', + value: res + '' } - return formatProductsFilter } -module.exports = createFormatProductsFilter +module.exports = formatProductsFilter diff --git a/index.js b/index.js index a4350704..f381da38 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,6 @@ const sortBy = require('lodash/sortBy') const pRetry = require('p-retry') const defaultProfile = require('./lib/default-profile') -const createFormatProductsFilter = require('./format/products-filter') const validateProfile = require('./lib/validate-profile') const isNonEmptyString = str => 'string' === typeof str && str.length > 0 @@ -26,9 +25,6 @@ const validateLocation = (loc, name = 'location') => { const createClient = (profile, userAgent, opt = {}) => { profile = Object.assign({}, defaultProfile, profile) - if (!profile.formatProductsFilter) { - profile.formatProductsFilter = createFormatProductsFilter(profile) - } validateProfile(profile) if ('string' !== typeof userAgent) { @@ -64,7 +60,7 @@ const createClient = (profile, userAgent, opt = {}) => { }, opt) opt.when = new Date(opt.when || Date.now()) if (Number.isNaN(+opt.when)) throw new Error('opt.when is invalid') - const products = profile.formatProductsFilter(opt.products || {}) + const products = profile.formatProductsFilter({profile}, opt.products || {}) const dir = opt.direction ? profile.formatStation(opt.direction) : null const req = { @@ -165,7 +161,7 @@ const createClient = (profile, userAgent, opt = {}) => { } const filters = [ - profile.formatProductsFilter(opt.products || {}) + profile.formatProductsFilter({profile}, opt.products || {}) ] if ( opt.accessibility && @@ -458,7 +454,7 @@ const createClient = (profile, userAgent, opt = {}) => { perStep: Math.round(durationPerStep), ageOfReport: true, // todo: what is this? jnyFltrL: [ - profile.formatProductsFilter(opt.products || {}) + profile.formatProductsFilter({profile}, opt.products || {}) ], trainPosMode: 'CALC' // todo: what is this? what about realtime? } @@ -493,7 +489,7 @@ const createClient = (profile, userAgent, opt = {}) => { time: profile.formatTime(profile, opt.when), period: 120, // todo: what is this? jnyFltrL: [ - profile.formatProductsFilter(opt.products || {}) + profile.formatProductsFilter({profile}, opt.products || {}) ] } }) diff --git a/lib/default-profile.js b/lib/default-profile.js index 62572667..c75c2f56 100644 --- a/lib/default-profile.js +++ b/lib/default-profile.js @@ -26,6 +26,7 @@ const formatAddress = require('../format/address') const formatCoord = require('../format/coord') const formatDate = require('../format/date') const formatLocationFilter = require('../format/location-filter') +const formatProductsFilter = require('../format/products-filter') const formatPoi = require('../format/poi') const formatStation = require('../format/station') const formatTime = require('../format/time') @@ -70,6 +71,7 @@ const defaultProfile = { formatCoord, formatDate, formatLocationFilter, + formatProductsFilter, formatPoi, formatStation, formatTime, diff --git a/lib/validate-profile.js b/lib/validate-profile.js index 6a08fcb6..27149cba 100644 --- a/lib/validate-profile.js +++ b/lib/validate-profile.js @@ -31,6 +31,7 @@ const types = { formatCoord: 'function', formatDate: 'function', formatLocationFilter: 'function', + formatProductsFilter: 'function', formatPoi: 'function', formatStation: 'function', formatTime: 'function', diff --git a/test/format/products-filter.js b/test/format/products-filter.js index e00131fc..6149bddb 100644 --- a/test/format/products-filter.js +++ b/test/format/products-filter.js @@ -21,17 +21,16 @@ const products = [ }, ] -const profile = {products} const ctx = { common: {}, opt: {}, - profile + profile: {products} } test('formatProductsFilter works without customisations', (t) => { const expected = 1 | 2 | 4 const filter = {} - t.deepEqual(format(profile)(filter), { + t.deepEqual(format(ctx, filter), { type: 'PROD', mode: 'INC', value: expected + '' @@ -40,13 +39,13 @@ test('formatProductsFilter works without customisations', (t) => { }) test('formatProductsFilter works with customisations', (t) => { - t.equal(+format(profile)({ + t.equal(+format(ctx, { bus: true }).value, 1 | 2 | 4) - t.equal(+format(profile)({ + t.equal(+format(ctx, { bus: false }).value, 1 | 2) - t.equal(+format(profile)({ + t.equal(+format(ctx, { tram: true }).value, 1 | 2 | 4 | 8 | 32) t.end()