UNPKG

4.13 kBJavaScriptView Raw
1
2import * as style from '../style';
3import { fsw2coord, coord2fsw } from '../convert';
4import { re } from './fsw-re';
5
6const compose = {
7 /**
8 * Function to compose an fsw symbol with optional coordinate and style string
9 * @function fsw.compose.symbol
10 * @param {object} fswSymObject - an fsw symbol object
11 * @param {string} fswSymObject.symbol - an fsw symbol key
12 * @param {number[]} fswSymObject.coord - top-left coordinate of symbol
13 * @param {string} fswSymObject.style - a style string for custom appearance
14 * @returns {string} an fsw symbol string
15 * @example
16 * fsw.compose.symbol({
17 * 'symbol': 'S10000',
18 * 'coord': [480, 480],
19 * 'style': '-C'
20 * })
21 *
22 * return 'S10000480x480-C'
23 */
24 symbol: (fswSymObject) => {
25 if (typeof fswSymObject.symbol === 'string') {
26 const symbol = (fswSymObject.symbol.match(re.symbol) || [''])[0];
27 if (symbol) {
28 const x = (fswSymObject.coord && fswSymObject.coord[0] || '').toString();
29 const y = (fswSymObject.coord && fswSymObject.coord[1] || '').toString();
30 const coord = ((x + 'x' + y).match(re.coord) || [''])[0] || '';
31 const styleStr = (typeof fswSymObject.style === 'string') && (fswSymObject.style.match(style.re.full) || [''])[0] || '';
32 return symbol + coord + styleStr;
33
34 }
35 }
36 return undefined;
37 },
38 /**
39 * Function to compose an fsw sign with style string
40 * @function fsw.compose.sign
41 * @param {object} fswSymObject - an fsw sign object
42 * @param {string[]} fswSignObject.sequence - an ordered array of symbols
43 * @param {string} fswSignObject.box - a choice BLMR: horizontal Box, Left, Middle, and Right lane
44 * @param {number[]} fswSignObject.max - max bottom-right coordinate of the signbox space
45 * @param {{symbol:string,coord:number[]}[]} fswSignObject.spatials - array of symbols with top-left coordinate placement
46 * @param {string} fswSignObject.style - a style string for custom appearance
47 * @returns {string} an fsw sign string
48 * @example
49 * fsw.compose.sign({
50 * sequence: ['S10011', 'S10019', 'S2e704', 'S2e748'],
51 * box: 'M',
52 * max: [525, 535],
53 * spatials: [
54 * {
55 * symbol: 'S2e748',
56 * coord: [483, 510]
57 * },
58 * {
59 * symbol: 'S10011',
60 * coord: [501, 466]
61 * },
62 * {
63 * symbol: 'S2e704',
64 * coord: [510, 500]
65 * },
66 * {
67 * symbol: 'S10019',
68 * coord: [476, 475]
69 * }
70 * ],
71 * style: '-C'
72 * })
73 *
74 * return 'AS10011S10019S2e704S2e748M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475-C'
75 */
76 sign: (fswSignObject) => {
77 let box = (typeof fswSignObject.box !== 'string') ? 'M' : (fswSignObject.box + 'M').match(re.box)
78
79 const x = (fswSignObject.max && fswSignObject.max[0] || '').toString();
80 const y = (fswSignObject.max && fswSignObject.max[1] || '').toString();
81 const max = ((x + 'x' + y).match(re.coord) || [''])[0] || '';
82
83 if (!max) return undefined;
84
85 let prefix = '';
86 if (fswSignObject.sequence && Array.isArray(fswSignObject.sequence)) {
87 prefix = fswSignObject.sequence.map(key => (key.match(re.symbol) || [''])[0]).join('')
88 prefix = prefix ? 'A' + prefix : ''
89 }
90
91 let signbox = '';
92 if (fswSignObject.spatials && Array.isArray(fswSignObject.spatials)) {
93 signbox = fswSignObject.spatials.map(spatial => {
94 if (typeof spatial.symbol === 'string') {
95 const symbol = (spatial.symbol.match(re.symbol) || [''])[0];
96 if (symbol) {
97 const x = (spatial.coord && spatial.coord[0] || '').toString();
98 const y = (spatial.coord && spatial.coord[1] || '').toString();
99 const coord = ((x + 'x' + y).match(re.coord) || [''])[0] || '';
100 if (coord) {
101 return symbol + coord;
102 }
103 }
104 }
105 return '';
106 }).join('');
107 }
108 const styleStr = (typeof fswSignObject.style === 'string') && (fswSignObject.style.match(style.re.full) || [''])[0] || '';
109
110 return prefix + box + max + signbox + styleStr;
111 }
112}
113
114export { compose }