UNPKG

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