2017-11-27 19:39:18 +01:00
|
|
|
'use strict'
|
|
|
|
|
2018-03-16 15:38:16 +01:00
|
|
|
const createParseBitmask = (profile) => {
|
|
|
|
const defaultProducts = {}
|
|
|
|
let withBitmask = []
|
|
|
|
for (let product of profile.products) {
|
2018-03-16 17:06:32 +01:00
|
|
|
defaultProducts[product.id] = false
|
2018-03-16 15:38:16 +01:00
|
|
|
for (let bitmask of product.bitmasks) {
|
|
|
|
withBitmask.push([bitmask, product])
|
2018-03-13 03:23:03 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-16 15:38:16 +01:00
|
|
|
withBitmask.sort((a, b) => b[0] - a[0]) // descending
|
2018-03-13 03:23:03 +01:00
|
|
|
|
2017-12-11 14:41:28 +01:00
|
|
|
const parseBitmask = (bitmask) => {
|
2018-03-13 03:23:03 +01:00
|
|
|
const res = Object.assign({}, defaultProducts)
|
|
|
|
|
2018-03-16 15:38:16 +01:00
|
|
|
for (let [pBitmask, product] of withBitmask) {
|
|
|
|
if ((pBitmask & bitmask) > 0) {
|
2018-03-16 17:06:32 +01:00
|
|
|
res[product.id] = true
|
2018-03-16 15:38:16 +01:00
|
|
|
bitmask -= pBitmask
|
2018-03-13 03:23:03 +01:00
|
|
|
}
|
2018-03-14 14:50:34 +01:00
|
|
|
else{
|
2018-04-09 19:37:44 +02:00
|
|
|
res[product.id] = false
|
2018-03-14 14:50:34 +01:00
|
|
|
}
|
2018-03-13 03:23:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
2017-11-27 19:39:18 +01:00
|
|
|
}
|
2017-12-11 14:41:28 +01:00
|
|
|
return parseBitmask
|
2017-11-27 19:39:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = createParseBitmask
|