2018-10-27 14:47:19 +02:00
|
|
|
'use strict'
|
|
|
|
|
2019-08-23 15:59:38 +02:00
|
|
|
const get = require('lodash/get')
|
2019-09-02 16:56:26 +02:00
|
|
|
const findInTree = require('../lib/find-in-tree')
|
2019-08-23 15:59:38 +02:00
|
|
|
|
|
|
|
const parseCommonData = (profile, opt, res) => {
|
|
|
|
const c = res.common || {}
|
2018-10-27 14:47:19 +02:00
|
|
|
|
2019-08-23 15:36:57 +02:00
|
|
|
res.operators = []
|
2018-10-27 14:47:19 +02:00
|
|
|
if (Array.isArray(c.opL)) {
|
|
|
|
res.operators = c.opL.map(op => profile.parseOperator(profile, op))
|
2019-09-02 16:56:26 +02:00
|
|
|
findInTree(res, '**.oprX', (idx, parent) => {
|
|
|
|
if ('number' === typeof idx) parent.operator = res.operators[idx]
|
|
|
|
})
|
2018-10-27 14:47:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res.icons = []
|
2019-08-23 16:02:34 +02:00
|
|
|
if (Array.isArray(c.icoL)) {
|
2019-08-30 19:19:31 +02:00
|
|
|
res.icons = c.icoL.map(icon => profile.parseIcon(profile, icon))
|
2019-09-02 16:56:26 +02:00
|
|
|
findInTree(res, '**.icoX', (idx, parent) => {
|
|
|
|
if ('number' === typeof idx) parent.icon = res.icons[idx]
|
|
|
|
})
|
2019-08-23 16:02:34 +02:00
|
|
|
}
|
2018-10-27 14:47:19 +02:00
|
|
|
|
2019-08-23 15:36:57 +02:00
|
|
|
res.lines = []
|
2018-10-27 14:47:19 +02:00
|
|
|
if (Array.isArray(c.prodL)) {
|
|
|
|
const parse = profile.parseLine(profile, opt, res)
|
|
|
|
res.lines = c.prodL.map(parse)
|
2019-08-23 16:06:21 +02:00
|
|
|
|
2019-09-02 16:56:26 +02:00
|
|
|
findInTree(res, '**.prodX', (idx, parent) => {
|
|
|
|
if ('number' === typeof idx) parent.line = res.lines[idx]
|
|
|
|
})
|
|
|
|
findInTree(res, '**.pRefL', (idxs, parent) => {
|
2019-08-23 16:06:21 +02:00
|
|
|
parent.lines = idxs.filter(idx => !!res.lines[idx]).map(idx => res.lines[idx])
|
|
|
|
})
|
|
|
|
// todo
|
2019-09-02 16:56:26 +02:00
|
|
|
// **.dep.dProdX: departureLine -> res.lines[idx]
|
|
|
|
// **.arr.aProdX: arrivalLine -> res.lines[idx]
|
2018-10-27 14:47:19 +02:00
|
|
|
}
|
|
|
|
|
2019-08-23 15:36:57 +02:00
|
|
|
res.locations = []
|
2018-10-27 14:47:19 +02:00
|
|
|
if (Array.isArray(c.locL)) {
|
|
|
|
const parse = loc => profile.parseLocation(profile, opt, res, loc)
|
|
|
|
res.locations = c.locL.map(parse)
|
|
|
|
|
|
|
|
for (let i = 0; i < res.locations.length; i++) {
|
|
|
|
const raw = c.locL[i]
|
|
|
|
const loc = res.locations[i]
|
|
|
|
if ('number' === typeof raw.mMastLocX) {
|
|
|
|
loc.station = Object.assign({}, res.locations[raw.mMastLocX])
|
|
|
|
loc.station.type = 'station'
|
|
|
|
} else if (raw.isMainMast) loc.type = 'station'
|
|
|
|
}
|
2019-08-23 16:17:20 +02:00
|
|
|
|
|
|
|
// todo: correct props?
|
2019-09-02 16:56:26 +02:00
|
|
|
findInTree(res, '**.locX', (idx, parent) => {
|
|
|
|
if ('number' === typeof idx) parent.location = res.locations[idx]
|
|
|
|
})
|
|
|
|
findInTree(res, '**.ani.fLocX', (idxs, parent) => {
|
2019-08-23 16:17:20 +02:00
|
|
|
parent.fromLocations = idxs.map(idx => res.locations[idx])
|
|
|
|
})
|
2019-09-02 16:56:26 +02:00
|
|
|
findInTree(res, '**.ani.tLocX', (idxs, parent) => {
|
2019-08-23 16:17:20 +02:00
|
|
|
parent.toLocations = idxs.map(idx => res.locations[idx])
|
|
|
|
})
|
2019-09-02 16:56:26 +02:00
|
|
|
findInTree(res, '**.fLocX', (idx, parent) => {
|
|
|
|
if ('number' === typeof idx) parent.fromLocation = res.locations[idx]
|
|
|
|
})
|
|
|
|
findInTree(res, '**.tLocX', (idx, parent) => {
|
|
|
|
if ('number' === typeof idx) parent.toLocation = res.locations[idx]
|
|
|
|
})
|
2018-10-27 14:47:19 +02:00
|
|
|
}
|
|
|
|
|
2019-02-25 15:03:33 +01:00
|
|
|
res.hints = []
|
|
|
|
if (opt.remarks && Array.isArray(c.remL)) {
|
|
|
|
res.hints = c.remL.map(hint => profile.parseHint(profile, hint, {...c, ...res}))
|
2019-09-02 16:56:26 +02:00
|
|
|
findInTree(res, '**.remX', (idx, parent) => {
|
|
|
|
if ('number' === typeof idx) parent.hint = res.hints[idx]
|
|
|
|
})
|
2019-02-25 15:03:33 +01:00
|
|
|
}
|
|
|
|
res.warnings = []
|
|
|
|
if (opt.remarks && Array.isArray(c.himL)) {
|
|
|
|
res.warnings = c.himL.map(w => profile.parseWarning(profile, w, {...c, ...res}))
|
2019-09-02 16:56:26 +02:00
|
|
|
findInTree(res, '**.himX', (idx, parent) => {
|
|
|
|
if ('number' === typeof idx) parent.warning = res.warnings[idx]
|
|
|
|
})
|
2019-02-25 15:03:33 +01:00
|
|
|
}
|
|
|
|
|
2018-10-27 14:47:19 +02:00
|
|
|
res.polylines = []
|
2019-08-23 15:39:42 +02:00
|
|
|
if (opt.polylines && Array.isArray(c.polyL)) {
|
|
|
|
const parse = profile.parsePolyline(profile, opt, res)
|
|
|
|
res.polylines = c.polyL.map(parse)
|
|
|
|
// todo: **.ani.poly -> parsePolyline()
|
|
|
|
|
2019-09-02 16:56:26 +02:00
|
|
|
findInTree(res, '**.polyG.polyXL', (idxs, _, path) => {
|
2019-08-23 15:39:42 +02:00
|
|
|
const idx = idxs.find(idx => !!res.polylines[idx]) // find any given polyline
|
|
|
|
const jny = get(res, path.slice(0, -2))
|
|
|
|
jny.polyline = res.polylines[idx]
|
|
|
|
})
|
|
|
|
}
|
2018-10-27 14:47:19 +02:00
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = parseCommonData
|