mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-22 22:59:35 +02:00
call formatProductsFilter via profile, use products from ctx.profile 💥
This commit is contained in:
parent
850cd9ce85
commit
773035c05d
5 changed files with 31 additions and 35 deletions
|
@ -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
|
||||
|
|
12
index.js
12
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 || {})
|
||||
]
|
||||
}
|
||||
})
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -31,6 +31,7 @@ const types = {
|
|||
formatCoord: 'function',
|
||||
formatDate: 'function',
|
||||
formatLocationFilter: 'function',
|
||||
formatProductsFilter: 'function',
|
||||
formatPoi: 'function',
|
||||
formatStation: 'function',
|
||||
formatTime: 'function',
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Add table
Reference in a new issue