db-vendo-client/parse/line.js

50 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-11-11 22:35:41 +01:00
'use strict'
const slugg = require('slugg')
const createParseLine = (profile, opt, {operators}) => {
const byBitmask = []
for (let product of profile.products) {
for (let bitmask of product.bitmasks) {
byBitmask[bitmask] = product
}
}
2019-08-23 15:36:57 +02:00
// todo: p.himIdL
2018-01-05 18:46:19 +01:00
const parseLine = (p) => {
if (!p) return null // todo: handle this upstream
2019-06-21 18:43:10 +02:00
const name = p.line || p.addName || p.name || null // wtf
2018-01-05 18:46:19 +01:00
const res = {
type: 'line',
// This is terrible, but FPTF demands an ID. Let's pray for HAFAS.
id: (
p.prodCtx && p.prodCtx.lineId && slugg(p.prodCtx.lineId.trim())
|| name && slugg(name.trim())
|| null
),
2019-06-21 18:43:10 +02:00
// todo: what is p.prodCtx.matchId? use as `id`? expose it.
fahrtNr: p.prodCtx && p.prodCtx.num || null,
name,
2018-01-05 18:46:19 +01:00
public: true
}
// todo: what is p.number?
// todo: what is p.prodCtx.catCode?
2017-11-11 22:35:41 +01:00
if ('cls' in p) {
// todo: what if `p.cls` is the sum of two bitmasks?
const product = byBitmask[parseInt(p.cls)]
2018-03-16 17:06:32 +01:00
res.mode = product && product.mode || null
res.product = product && product.id || null
}
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