| 
									
										
										
										
											2018-10-27 14:47:19 +02:00
										 |  |  | 'use strict' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 00:31:25 +01:00
										 |  |  | const omit = require('lodash/omit') | 
					
						
							| 
									
										
										
										
											2020-04-08 09:55:15 -07:00
										 |  |  | const createFindInTree = require('../lib/find-in-tree'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const findInTree = createFindInTree([ | 
					
						
							|  |  |  | 	'**.oprX', '**.icoX', '**.prodX', '**.pRefL', '**.locX', | 
					
						
							|  |  |  | 	'**.ani.fLocX', '**.ani.tLocX', '**.fLocX', '**.tLocX', | 
					
						
							|  |  |  | 	'**.remX', '**.himX', '**.polyG.polyXL', '**.rRefL', | 
					
						
							| 
									
										
										
										
											2021-01-26 21:04:10 +01:00
										 |  |  | 	'**.msgL', '**.remL', | 
					
						
							| 
									
										
										
										
											2020-04-08 09:55:15 -07:00
										 |  |  | ]) | 
					
						
							| 
									
										
										
										
											2019-08-23 15:59:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 22:12:09 +01:00
										 |  |  | // there are circular references (e.g. warning -> loc -> warning)
 | 
					
						
							|  |  |  | // todo: parse either on-the-fly or in a recursive/iterative process
 | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | const parseCommonData = (_ctx) => { | 
					
						
							|  |  |  | 	const {profile, opt, res} = _ctx | 
					
						
							| 
									
										
										
										
											2019-08-23 15:59:38 +02:00
										 |  |  | 	const c = res.common || {} | 
					
						
							| 
									
										
										
										
											2020-04-08 09:55:15 -07:00
										 |  |  | 	const matches = findInTree(res) | 
					
						
							| 
									
										
										
										
											2018-10-27 14:47:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | 	const common = {} | 
					
						
							|  |  |  | 	const ctx = {..._ctx, common} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	common.operators = [] | 
					
						
							| 
									
										
										
										
											2018-10-27 14:47:19 +02:00
										 |  |  | 	if (Array.isArray(c.opL)) { | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | 		common.operators = c.opL.map(op => profile.parseOperator(ctx, op)) | 
					
						
							| 
									
										
										
										
											2020-02-22 11:13:56 -08:00
										 |  |  | 		matches['**.oprX'].forEach(([idx, parents]) => { | 
					
						
							|  |  |  | 			if ('number' === typeof idx) parents[0].operator = common.operators[idx] | 
					
						
							| 
									
										
										
										
											2019-09-02 16:56:26 +02:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-10-27 14:47:19 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | 	common.icons = [] | 
					
						
							| 
									
										
										
										
											2019-08-23 16:02:34 +02:00
										 |  |  | 	if (Array.isArray(c.icoL)) { | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | 		common.icons = c.icoL.map(icon => profile.parseIcon(ctx, icon)) | 
					
						
							| 
									
										
										
										
											2020-02-22 11:13:56 -08:00
										 |  |  | 		matches['**.icoX'].forEach(([idx, parents]) => { | 
					
						
							|  |  |  | 			if ('number' === typeof idx) parents[0].icon = common.icons[idx] | 
					
						
							| 
									
										
										
										
											2019-09-02 16:56:26 +02:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2019-08-23 16:02:34 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-27 14:47:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | 	common.lines = [] | 
					
						
							| 
									
										
										
										
											2018-10-27 14:47:19 +02:00
										 |  |  | 	if (Array.isArray(c.prodL)) { | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | 		common.lines = c.prodL.map(l => profile.parseLine(ctx, l)) | 
					
						
							| 
									
										
										
										
											2019-08-23 16:06:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-22 11:13:56 -08:00
										 |  |  | 		matches['**.prodX'].forEach(([idx, parents]) => { | 
					
						
							|  |  |  | 			if ('number' === typeof idx) parents[0].line = common.lines[idx] | 
					
						
							| 
									
										
										
										
											2019-09-02 16:56:26 +02:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2020-02-22 11:13:56 -08:00
										 |  |  | 		matches['**.pRefL'].forEach(([idxs, parents]) => { | 
					
						
							|  |  |  | 			parents[0].lines = idxs.filter(idx => !!common.lines[idx]).map(idx => common.lines[idx]) | 
					
						
							| 
									
										
										
										
											2019-08-23 16:06:21 +02:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 		// todo
 | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | 		// **.dep.dProdX: departureLine -> common.lines[idx]
 | 
					
						
							|  |  |  | 		// **.arr.aProdX: arrivalLine -> common.lines[idx]
 | 
					
						
							| 
									
										
										
										
											2018-10-27 14:47:19 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 19:09:05 +00:00
										 |  |  | 	common.hints = [] | 
					
						
							|  |  |  | 	if (Array.isArray(c.remL)) { | 
					
						
							|  |  |  | 		common.hints = c.remL.map(hint => profile.parseHint(ctx, hint)) | 
					
						
							|  |  |  | 		matches['**.remX'].forEach(([idx, parents]) => { | 
					
						
							|  |  |  | 			if ('number' === typeof idx) parents[0].hint = common.hints[idx] | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2021-01-26 21:04:10 +01:00
										 |  |  | 		matches['**.remL'].forEach(([idxs, parents]) => { | 
					
						
							|  |  |  | 			if (!Array.isArray(idxs)) return; | 
					
						
							|  |  |  | 			parents[0].hints = idxs | 
					
						
							|  |  |  | 			.filter(idx => !!common.hints[idx]) | 
					
						
							|  |  |  | 			.map(idx => common.hints[idx]) | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2020-02-15 19:09:05 +00:00
										 |  |  | 		matches['**.rRefL'].forEach(([idxs, parents]) => { | 
					
						
							|  |  |  | 			parents[0].hints = idxs | 
					
						
							|  |  |  | 			.filter(idx => !!common.hints[idx]) | 
					
						
							|  |  |  | 			.map(idx => common.hints[idx]) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 21:02:50 +01:00
										 |  |  | 	// resolve .msgL[] references
 | 
					
						
							|  |  |  | 	// todo: `himMsgEdgeL[].msgRefL[]` look different, it seems they only reference
 | 
					
						
							|  |  |  | 	// `common.himL[]` items?
 | 
					
						
							|  |  |  | 	const parseRemarkRef = (ref) => { | 
					
						
							|  |  |  | 		if (ref.type === 'REM' && ref.hint) { | 
					
						
							|  |  |  | 			return omit(ref, ['type', 'remX']) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (ref.type === 'HIM' && ref.warning) { | 
					
						
							|  |  |  | 			return omit(ref, ['type', 'himX']) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return null | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	matches['**.msgL'].forEach(([refs, parents]) => { | 
					
						
							|  |  |  | 		// todo: store as parents[0].(hints|warnings)
 | 
					
						
							|  |  |  | 		parents[0].remarkRefs = refs | 
					
						
							|  |  |  | 		.map(parseRemarkRef) | 
					
						
							|  |  |  | 		.filter(ref => ref !== null) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | 	common.locations = [] | 
					
						
							| 
									
										
										
										
											2018-10-27 14:47:19 +02:00
										 |  |  | 	if (Array.isArray(c.locL)) { | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | 		common.locations = c.locL.map(loc => profile.parseLocation(ctx, loc)) | 
					
						
							| 
									
										
										
										
											2018-10-27 14:47:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | 		for (let i = 0; i < common.locations.length; i++) { | 
					
						
							| 
									
										
										
										
											2018-10-27 14:47:19 +02:00
										 |  |  | 			const raw = c.locL[i] | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | 			const loc = common.locations[i] | 
					
						
							| 
									
										
										
										
											2018-10-27 14:47:19 +02:00
										 |  |  | 			if ('number' === typeof raw.mMastLocX) { | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | 				loc.station = Object.assign({}, common.locations[raw.mMastLocX]) | 
					
						
							| 
									
										
										
										
											2018-10-27 14:47:19 +02:00
										 |  |  | 				loc.station.type = 'station' | 
					
						
							|  |  |  | 			} else if (raw.isMainMast) loc.type = 'station' | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-08-23 16:17:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// todo: correct props?
 | 
					
						
							| 
									
										
										
										
											2020-02-22 11:13:56 -08:00
										 |  |  | 		matches['**.locX'].forEach(([idx, parents]) => { | 
					
						
							|  |  |  | 			if ('number' === typeof idx) parents[0].location = common.locations[idx] | 
					
						
							| 
									
										
										
										
											2019-09-02 16:56:26 +02:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2020-02-22 11:13:56 -08:00
										 |  |  | 		matches['**.ani.fLocX'].forEach(([idxs, parents]) => { | 
					
						
							|  |  |  | 			parents[0].fromLocations = idxs.map(idx => common.locations[idx]) | 
					
						
							| 
									
										
										
										
											2019-08-23 16:17:20 +02:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2020-02-22 11:13:56 -08:00
										 |  |  | 		matches['**.ani.tLocX'].forEach(([idxs, parents]) => { | 
					
						
							|  |  |  | 			parents[0].toLocations = idxs.map(idx => common.locations[idx]) | 
					
						
							| 
									
										
										
										
											2019-08-23 16:17:20 +02:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2020-02-22 11:13:56 -08:00
										 |  |  | 		matches['**.fLocX'].forEach(([idx, parents]) => { | 
					
						
							|  |  |  | 			if ('number' === typeof idx) parents[0].fromLocation = common.locations[idx] | 
					
						
							| 
									
										
										
										
											2019-09-02 16:56:26 +02:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2020-02-22 11:13:56 -08:00
										 |  |  | 		matches['**.tLocX'].forEach(([idx, parents]) => { | 
					
						
							|  |  |  | 			if ('number' === typeof idx) parents[0].toLocation = common.locations[idx] | 
					
						
							| 
									
										
										
										
											2019-09-02 16:56:26 +02:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-10-27 14:47:19 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | 	common.warnings = [] | 
					
						
							| 
									
										
										
										
											2020-03-07 00:31:25 +01:00
										 |  |  | 	if (Array.isArray(c.himL)) { | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | 		common.warnings = c.himL.map(w => profile.parseWarning(ctx, w)) | 
					
						
							| 
									
										
										
										
											2020-02-22 11:13:56 -08:00
										 |  |  | 		matches['**.himX'].forEach(([idx, parents]) => { | 
					
						
							|  |  |  | 			if ('number' === typeof idx) parents[0].warning = common.warnings[idx] | 
					
						
							| 
									
										
										
										
											2019-09-02 16:56:26 +02:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2019-02-25 15:03:33 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | 	common.polylines = [] | 
					
						
							| 
									
										
										
										
											2020-06-11 16:12:33 +02:00
										 |  |  | 	if ((opt.polylines || opt.polyline) && Array.isArray(c.polyL)) { | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | 		common.polylines = c.polyL.map(p => profile.parsePolyline(ctx, p)) | 
					
						
							| 
									
										
										
										
											2019-08-23 15:39:42 +02:00
										 |  |  | 		// todo: **.ani.poly -> parsePolyline()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-22 11:13:56 -08:00
										 |  |  | 		matches['**.polyG.polyXL'].forEach(([idxs, parents]) => { | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | 			const idx = idxs.find(idx => !!common.polylines[idx]) // find any given polyline
 | 
					
						
							| 
									
										
										
										
											2020-02-22 11:13:56 -08:00
										 |  |  | 			parents[1].polyline = common.polylines[idx] | 
					
						
							| 
									
										
										
										
											2019-08-23 15:39:42 +02:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-27 14:47:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 00:19:11 +02:00
										 |  |  | 	return common | 
					
						
							| 
									
										
										
										
											2018-10-27 14:47:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = parseCommonData |