mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-22 22:59:35 +02:00
fix bitmask handling 🐛
This commit is contained in:
parent
d0f7ca1b6c
commit
0ce5669899
2 changed files with 8 additions and 18 deletions
|
@ -21,7 +21,7 @@ const createFormatProductsFilter = (profile) => {
|
|||
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 += bitmask
|
||||
for (let bitmask of byProduct[product].bitmasks) res = res ^ bitmask
|
||||
}
|
||||
if (products === 0) throw new Error('no products used')
|
||||
|
||||
|
|
|
@ -2,28 +2,18 @@
|
|||
|
||||
const createParseBitmask = (profile) => {
|
||||
const defaultProducts = {}
|
||||
let withBitmask = []
|
||||
for (let product of profile.products) {
|
||||
defaultProducts[product.id] = false
|
||||
for (let bitmask of product.bitmasks) {
|
||||
withBitmask.push([bitmask, product])
|
||||
}
|
||||
}
|
||||
withBitmask.sort((a, b) => b[0] - a[0]) // descending
|
||||
for (let product of profile.products) defaultProducts[product.id] = false
|
||||
|
||||
const parseBitmask = (bitmask) => {
|
||||
const res = Object.assign({}, defaultProducts)
|
||||
|
||||
for (let [pBitmask, product] of withBitmask) {
|
||||
if ((pBitmask & bitmask) > 0) {
|
||||
res[product.id] = true
|
||||
bitmask -= pBitmask
|
||||
}
|
||||
else{
|
||||
res[product.id] = false
|
||||
}
|
||||
}
|
||||
const bits = bitmask.toString(2).split('').map(i => parseInt(i)).reverse()
|
||||
for (let i = 0; i < bits.length; i++) {
|
||||
if (!bits[i]) continue // ignore `0`
|
||||
|
||||
const product = profile.products.find(p => p.bitmasks.includes(Math.pow(2, i)))
|
||||
if (product) res[product.id] = true
|
||||
}
|
||||
return res
|
||||
}
|
||||
return parseBitmask
|
||||
|
|
Loading…
Add table
Reference in a new issue