From 0ce5669899b4b2801417b7dbc03ef7833b93ec6b Mon Sep 17 00:00:00 2001 From: Jannis R Date: Fri, 29 Mar 2019 19:06:16 +0100 Subject: [PATCH] fix bitmask handling :bug: --- format/products-filter.js | 2 +- parse/products-bitmask.js | 24 +++++++----------------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/format/products-filter.js b/format/products-filter.js index b0780635..b539de23 100644 --- a/format/products-filter.js +++ b/format/products-filter.js @@ -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') diff --git a/parse/products-bitmask.js b/parse/products-bitmask.js index c1fb9b69..087e21a9 100644 --- a/parse/products-bitmask.js +++ b/parse/products-bitmask.js @@ -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