2017-11-27 19:39:18 +01:00
|
|
|
'use strict'
|
|
|
|
|
2018-03-16 15:38:16 +01:00
|
|
|
const createParseBitmask = (profile) => {
|
|
|
|
const defaultProducts = {}
|
2019-03-29 19:06:16 +01:00
|
|
|
for (let product of profile.products) defaultProducts[product.id] = false
|
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)
|
|
|
|
|
2019-03-29 19:06:16 +01:00
|
|
|
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`
|
2018-03-13 03:23:03 +01:00
|
|
|
|
2019-03-29 19:06:16 +01:00
|
|
|
const product = profile.products.find(p => p.bitmasks.includes(Math.pow(2, i)))
|
|
|
|
if (product) res[product.id] = true
|
|
|
|
}
|
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
|