call formatProductsFilter via profile, use products from ctx.profile 💥

This commit is contained in:
Jannis R 2019-10-31 19:46:30 +01:00
parent 850cd9ce85
commit 773035c05d
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
5 changed files with 31 additions and 35 deletions

View file

@ -4,34 +4,32 @@ const isObj = require('lodash/isObject')
const hasProp = (o, k) => Object.prototype.hasOwnProperty.call(o, k) 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 byProduct = {}
const defaultProducts = {} const defaultProducts = {}
for (let product of profile.products) { for (let product of profile.products) {
byProduct[product.id] = product byProduct[product.id] = product
defaultProducts[product.id] = product.default defaultProducts[product.id] = product.default
} }
filter = Object.assign({}, defaultProducts, filter)
const formatProductsFilter = (filter) => { let res = 0, products = 0
if (!isObj(filter)) throw new TypeError('products filter must be an object') for (let product in filter) {
filter = Object.assign({}, defaultProducts, filter) if (!hasProp(filter, product) || filter[product] !== true) continue
if (!byProduct[product]) throw new TypeError('unknown product ' + product)
let res = 0, products = 0 products++
for (let product in filter) { for (let bitmask of byProduct[product].bitmasks) res = res | bitmask
if (!hasProp(filter, product) || filter[product] !== true) continue }
if (!byProduct[product]) throw new TypeError('unknown product ' + product) if (products === 0) throw new Error('no products used')
products++
for (let bitmask of byProduct[product].bitmasks) res = res ^ bitmask return {
} type: 'PROD',
if (products === 0) throw new Error('no products used') mode: 'INC',
value: res + ''
return {
type: 'PROD',
mode: 'INC',
value: res + ''
}
} }
return formatProductsFilter
} }
module.exports = createFormatProductsFilter module.exports = formatProductsFilter

View file

@ -7,7 +7,6 @@ const sortBy = require('lodash/sortBy')
const pRetry = require('p-retry') const pRetry = require('p-retry')
const defaultProfile = require('./lib/default-profile') const defaultProfile = require('./lib/default-profile')
const createFormatProductsFilter = require('./format/products-filter')
const validateProfile = require('./lib/validate-profile') const validateProfile = require('./lib/validate-profile')
const isNonEmptyString = str => 'string' === typeof str && str.length > 0 const isNonEmptyString = str => 'string' === typeof str && str.length > 0
@ -26,9 +25,6 @@ const validateLocation = (loc, name = 'location') => {
const createClient = (profile, userAgent, opt = {}) => { const createClient = (profile, userAgent, opt = {}) => {
profile = Object.assign({}, defaultProfile, profile) profile = Object.assign({}, defaultProfile, profile)
if (!profile.formatProductsFilter) {
profile.formatProductsFilter = createFormatProductsFilter(profile)
}
validateProfile(profile) validateProfile(profile)
if ('string' !== typeof userAgent) { if ('string' !== typeof userAgent) {
@ -64,7 +60,7 @@ const createClient = (profile, userAgent, opt = {}) => {
}, opt) }, opt)
opt.when = new Date(opt.when || Date.now()) opt.when = new Date(opt.when || Date.now())
if (Number.isNaN(+opt.when)) throw new Error('opt.when is invalid') 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 dir = opt.direction ? profile.formatStation(opt.direction) : null
const req = { const req = {
@ -165,7 +161,7 @@ const createClient = (profile, userAgent, opt = {}) => {
} }
const filters = [ const filters = [
profile.formatProductsFilter(opt.products || {}) profile.formatProductsFilter({profile}, opt.products || {})
] ]
if ( if (
opt.accessibility && opt.accessibility &&
@ -458,7 +454,7 @@ const createClient = (profile, userAgent, opt = {}) => {
perStep: Math.round(durationPerStep), perStep: Math.round(durationPerStep),
ageOfReport: true, // todo: what is this? ageOfReport: true, // todo: what is this?
jnyFltrL: [ jnyFltrL: [
profile.formatProductsFilter(opt.products || {}) profile.formatProductsFilter({profile}, opt.products || {})
], ],
trainPosMode: 'CALC' // todo: what is this? what about realtime? trainPosMode: 'CALC' // todo: what is this? what about realtime?
} }
@ -493,7 +489,7 @@ const createClient = (profile, userAgent, opt = {}) => {
time: profile.formatTime(profile, opt.when), time: profile.formatTime(profile, opt.when),
period: 120, // todo: what is this? period: 120, // todo: what is this?
jnyFltrL: [ jnyFltrL: [
profile.formatProductsFilter(opt.products || {}) profile.formatProductsFilter({profile}, opt.products || {})
] ]
} }
}) })

View file

@ -26,6 +26,7 @@ const formatAddress = require('../format/address')
const formatCoord = require('../format/coord') const formatCoord = require('../format/coord')
const formatDate = require('../format/date') const formatDate = require('../format/date')
const formatLocationFilter = require('../format/location-filter') const formatLocationFilter = require('../format/location-filter')
const formatProductsFilter = require('../format/products-filter')
const formatPoi = require('../format/poi') const formatPoi = require('../format/poi')
const formatStation = require('../format/station') const formatStation = require('../format/station')
const formatTime = require('../format/time') const formatTime = require('../format/time')
@ -70,6 +71,7 @@ const defaultProfile = {
formatCoord, formatCoord,
formatDate, formatDate,
formatLocationFilter, formatLocationFilter,
formatProductsFilter,
formatPoi, formatPoi,
formatStation, formatStation,
formatTime, formatTime,

View file

@ -31,6 +31,7 @@ const types = {
formatCoord: 'function', formatCoord: 'function',
formatDate: 'function', formatDate: 'function',
formatLocationFilter: 'function', formatLocationFilter: 'function',
formatProductsFilter: 'function',
formatPoi: 'function', formatPoi: 'function',
formatStation: 'function', formatStation: 'function',
formatTime: 'function', formatTime: 'function',

View file

@ -21,17 +21,16 @@ const products = [
}, },
] ]
const profile = {products}
const ctx = { const ctx = {
common: {}, common: {},
opt: {}, opt: {},
profile profile: {products}
} }
test('formatProductsFilter works without customisations', (t) => { test('formatProductsFilter works without customisations', (t) => {
const expected = 1 | 2 | 4 const expected = 1 | 2 | 4
const filter = {} const filter = {}
t.deepEqual(format(profile)(filter), { t.deepEqual(format(ctx, filter), {
type: 'PROD', type: 'PROD',
mode: 'INC', mode: 'INC',
value: expected + '' value: expected + ''
@ -40,13 +39,13 @@ test('formatProductsFilter works without customisations', (t) => {
}) })
test('formatProductsFilter works with customisations', (t) => { test('formatProductsFilter works with customisations', (t) => {
t.equal(+format(profile)({ t.equal(+format(ctx, {
bus: true bus: true
}).value, 1 | 2 | 4) }).value, 1 | 2 | 4)
t.equal(+format(profile)({ t.equal(+format(ctx, {
bus: false bus: false
}).value, 1 | 2) }).value, 1 | 2)
t.equal(+format(profile)({ t.equal(+format(ctx, {
tram: true tram: true
}).value, 1 | 2 | 4 | 8 | 32) }).value, 1 | 2 | 4 | 8 | 32)
t.end() t.end()