2017-11-11 22:35:41 +01:00
|
|
|
'use strict'
|
|
|
|
|
2017-12-16 01:23:11 +01:00
|
|
|
const slugg = require('slugg')
|
|
|
|
|
2018-01-05 18:46:19 +01:00
|
|
|
const createParseLine = (profile, operators) => {
|
2018-03-16 17:00:06 +01:00
|
|
|
const byBitmask = []
|
|
|
|
for (let product of profile.products) {
|
|
|
|
for (let bitmask of product.bitmasks) {
|
|
|
|
byBitmask[bitmask] = product
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 18:46:19 +01:00
|
|
|
const parseLine = (p) => {
|
|
|
|
if (!p) return null // todo: handle this upstream
|
|
|
|
const res = {
|
|
|
|
type: 'line',
|
|
|
|
id: null,
|
|
|
|
name: p.line || p.name,
|
|
|
|
public: true
|
|
|
|
}
|
2018-01-23 02:37:08 +01:00
|
|
|
// todo: what is p.prodCtx && p.prodCtx.num?
|
2018-03-16 17:00:06 +01:00
|
|
|
// todo: what is p.number?
|
2017-11-11 22:35:41 +01:00
|
|
|
|
2018-03-16 17:00:06 +01:00
|
|
|
// This is terrible, but FPTF demands an ID. Let's pray for HaCon to expose an ID.
|
2018-01-23 02:37:08 +01:00
|
|
|
// todo: find a better way
|
2018-01-23 02:40:39 +01:00
|
|
|
if (p.line) res.id = slugg(p.line.trim())
|
2018-01-05 18:46:19 +01:00
|
|
|
else if (p.name) res.id = slugg(p.name.trim())
|
2017-12-16 01:23:11 +01:00
|
|
|
|
2018-01-05 18:46:19 +01:00
|
|
|
if (p.cls) res.class = p.cls
|
|
|
|
if (p.prodCtx && p.prodCtx.catCode !== undefined) {
|
|
|
|
res.productCode = +p.prodCtx.catCode
|
|
|
|
}
|
2017-11-11 23:56:09 +01:00
|
|
|
|
2018-03-16 17:00:06 +01:00
|
|
|
if ('class' in res) {
|
|
|
|
// todo: what if `res.class` is the sum of two bitmasks?
|
2018-03-16 17:06:32 +01:00
|
|
|
const product = byBitmask[parseInt(res.class)]
|
|
|
|
res.mode = product && product.mode || null
|
|
|
|
res.product = product && product.id || null
|
2018-03-16 17:00:06 +01:00
|
|
|
}
|
2017-11-12 21:23:29 +01:00
|
|
|
|
2018-01-05 18:46:19 +01:00
|
|
|
if ('number' === typeof p.oprX) {
|
|
|
|
res.operator = operators[p.oprX] || null
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
return parseLine
|
2017-11-11 22:35:41 +01:00
|
|
|
}
|
|
|
|
|
2018-01-05 18:46:19 +01:00
|
|
|
module.exports = createParseLine
|