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-13 03:23:03 +01:00
|
|
|
if ('string' !== typeof product.product) {
|
2018-03-16 15:38:16 +01:00
|
|
|
throw new Error('profile.products[].product must be a string.')
|
2018-03-13 03:23:03 +01:00
|
|
|
}
|
2018-03-16 15:38:16 +01:00
|
|
|
|
|
|
|
defaultProducts[product.product] = false
|
|
|
|
if (!Array.isArray(product.bitmasks)) {
|
|
|
|
throw new Error(product.product + '.bitmasks must be an array.')
|
|
|
|
}
|
|
|
|
for (let bitmask of product.bitmasks) {
|
|
|
|
if ('number' !== typeof bitmask) {
|
|
|
|
throw new Error(product.product + '.bitmasks[] must be a number.')
|
|
|
|
}
|
|
|
|
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-13 03:23:03 +01:00
|
|
|
res[product.product] = true
|
2018-03-16 15:38:16 +01:00
|
|
|
bitmask -= pBitmask
|
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
|