mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 07:09: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,16 +4,16 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
const formatProductsFilter = (filter) => {
|
|
||||||
if (!isObj(filter)) throw new TypeError('products filter must be an object')
|
|
||||||
filter = Object.assign({}, defaultProducts, filter)
|
filter = Object.assign({}, defaultProducts, filter)
|
||||||
|
|
||||||
let res = 0, products = 0
|
let res = 0, products = 0
|
||||||
|
@ -21,7 +21,7 @@ const createFormatProductsFilter = (profile) => {
|
||||||
if (!hasProp(filter, product) || filter[product] !== true) continue
|
if (!hasProp(filter, product) || filter[product] !== true) continue
|
||||||
if (!byProduct[product]) throw new TypeError('unknown product ' + product)
|
if (!byProduct[product]) throw new TypeError('unknown product ' + product)
|
||||||
products++
|
products++
|
||||||
for (let bitmask of byProduct[product].bitmasks) res = res ^ bitmask
|
for (let bitmask of byProduct[product].bitmasks) res = res | bitmask
|
||||||
}
|
}
|
||||||
if (products === 0) throw new Error('no products used')
|
if (products === 0) throw new Error('no products used')
|
||||||
|
|
||||||
|
@ -30,8 +30,6 @@ const createFormatProductsFilter = (profile) => {
|
||||||
mode: 'INC',
|
mode: 'INC',
|
||||||
value: res + ''
|
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 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 || {})
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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',
|
||||||
|
|
|
@ -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()
|
||||||
|
|
Loading…
Add table
Reference in a new issue