fix bitmask handling 🐛

This commit is contained in:
Jannis R 2019-03-29 19:06:16 +01:00
parent d0f7ca1b6c
commit 0ce5669899
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
2 changed files with 8 additions and 18 deletions

View file

@ -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')

View file

@ -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