1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | let re = {
|
15 | 'colorize': 'C',
|
16 | 'colorhex': '(?:[0-9a-fA-F]{3}){1,2}',
|
17 | 'colorname': '[a-zA-Z]+',
|
18 | 'padding': 'P[0-9]{2}',
|
19 | 'zoom': 'Z(?:[0-9]+(?:\\.[0-9]+)?|x)',
|
20 | 'zoomsym': 'Z[0-9]{2},[0-9]+(?:\\.[0-9]+)?(?:,[0-9]{3}x[0-9]{3})?',
|
21 | 'classbase': '-?[_a-zA-Z][_a-zA-Z0-9-]{0,100}',
|
22 | 'id': '[a-zA-Z][_a-zA-Z0-9-]{0,100}'
|
23 | };
|
24 | re.colorbase = `(?:${re.colorhex}|${re.colorname})`;
|
25 | re.color = `_${re.colorbase}_`;
|
26 | re.colors = `_${re.colorbase}(?:,${re.colorbase})?_`;
|
27 | re.background = `G${re.color}`;
|
28 | re.detail = `D${re.colors}`;
|
29 | re.detailsym = `D[0-9]{2}${re.colors}`;
|
30 | re.classes = `${re.classbase}(?: ${re.classbase})*`;
|
31 | re.full = `-(${re.colorize})?(${re.padding})?(${re.background})?(${re.detail})?(${re.zoom})?(?:-((?:${re.detailsym})*)((?:${re.zoomsym})*))?(?:-(${re.classes})?!(?:(${re.id})!)?)?`;
|
32 |
|
33 | const prefixColor = color => {
|
34 | const regex = new RegExp(`^${re.colorhex}$`);
|
35 | return (regex.test(color) ? '#' : '') + color;
|
36 | };
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | const parse = styleString => {
|
55 | const regex = `^${re.full}`;
|
56 | const m = (typeof styleString === 'string' ? styleString.match(new RegExp(regex)) : []) || [];
|
57 | return {
|
58 | 'colorize': !m[1] ? undefined : !!m[1],
|
59 | 'padding': !m[2] ? undefined : parseInt(m[2].slice(1)),
|
60 | 'background': !m[3] ? undefined : prefixColor(m[3].slice(2, -1)),
|
61 | 'detail': !m[4] ? undefined : m[4].slice(2, -1).split(',').map(prefixColor),
|
62 | 'zoom': !m[5] ? undefined : m[5] === 'Zx' ? 'x' : parseFloat(m[5].slice(1)),
|
63 | 'detailsym': !m[6] ? undefined : m[6].match(new RegExp(re.detailsym, 'g')).map(val => {
|
64 | const parts = val.split('_');
|
65 | const detail = parts[1].split(',').map(prefixColor);
|
66 | return {
|
67 | 'index': parseInt(parts[0].slice(1)),
|
68 | 'detail': detail
|
69 | };
|
70 | }),
|
71 | 'zoomsym': !m[7] ? undefined : m[7].match(new RegExp(re.zoomsym, 'g')).map(val => {
|
72 | const parts = val.split(',');
|
73 | return {
|
74 | 'index': parseInt(parts[0].slice(1)),
|
75 | 'zoom': parseFloat(parts[1]),
|
76 | 'offset': !parts[2] ? undefined : parts[2].split('x').map(val => parseInt(val) - 500)
|
77 | };
|
78 | }),
|
79 | 'classes': !m[8] ? undefined : m[8],
|
80 | 'id': !m[9] ? undefined : m[9]
|
81 | };
|
82 | };
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | const compose = styleObject => {
|
134 | if (typeof styleObject !== 'object' || styleObject === null) return undefined;
|
135 |
|
136 | let style1 = '-';
|
137 | style1 += !styleObject.colorize ? '' : 'C';
|
138 | const padding = parseInt(styleObject.padding);
|
139 | style1 += !padding || padding <= 0 || padding > 99 ? '' : 'P' + (padding > 9 ? padding : '0' + padding);
|
140 | const background = !styleObject.background || !(typeof styleObject.background === 'string') ? undefined : styleObject.background.match(re.colorbase)[0];
|
141 | style1 += !background ? '' : 'G_' + background + '_';
|
142 | const detail1 = !styleObject.detail || !styleObject.detail[0] || !(typeof styleObject.detail[0] === 'string') ? undefined : styleObject.detail[0].match(re.colorbase)[0];
|
143 | const detail2 = !styleObject.detail || !styleObject.detail[1] || !(typeof styleObject.detail[1] === 'string') ? undefined : styleObject.detail[1].match(re.colorbase)[0];
|
144 |
|
145 | if (detail1) {
|
146 | style1 += 'D_' + detail1;
|
147 |
|
148 | if (detail2) {
|
149 | style1 += ',' + detail2;
|
150 | }
|
151 |
|
152 | style1 += '_';
|
153 | }
|
154 |
|
155 | const zoom = styleObject.zoom === 'x' ? 'x' : parseFloat(styleObject.zoom);
|
156 | style1 += !zoom || zoom <= 0 ? '' : 'Z' + zoom;
|
157 | let style2 = '';
|
158 | const detailsym = !styleObject.detailsym || !Array.isArray(styleObject.detailsym) ? [] : styleObject.detailsym.map(styleObject => {
|
159 | const index = parseInt(styleObject.index);
|
160 | if (!index || index <= 0 || index > 99) return '';
|
161 | let style = 'D' + (index > 9 ? index : '0' + index);
|
162 | const detail1 = !styleObject.detail || !styleObject.detail[0] ? undefined : styleObject.detail[0].match(re.colorbase)[0];
|
163 | const detail2 = !styleObject.detail || !styleObject.detail[1] ? undefined : styleObject.detail[1].match(re.colorbase)[0];
|
164 |
|
165 | if (detail1) {
|
166 | style += '_' + detail1;
|
167 |
|
168 | if (detail2) {
|
169 | style += ',' + detail2;
|
170 | }
|
171 |
|
172 | style += '_';
|
173 | }
|
174 |
|
175 | return style;
|
176 | });
|
177 | style2 += detailsym.join('');
|
178 | const zoomsym = !styleObject.zoomsym || !Array.isArray(styleObject.zoomsym) ? [] : styleObject.zoomsym.map(styleObject => {
|
179 | const index = parseInt(styleObject.index);
|
180 | if (!index || index <= 0 || index > 99) return '';
|
181 | let style = 'Z' + (index > 9 ? index : '0' + index);
|
182 | const zoom = parseFloat(styleObject.zoom);
|
183 | style += !zoom || zoom <= 0 ? '' : ',' + zoom;
|
184 |
|
185 | if (styleObject.offset && 0 in styleObject.offset && 1 in styleObject.offset) {
|
186 | const x = parseInt(styleObject.offset[0]) + 500;
|
187 | const y = parseInt(styleObject.offset[1]) + 500;
|
188 |
|
189 | if (x >= 250 && x < 750 && y >= 250 && y < 750) {
|
190 | style += ',' + x + 'x' + y;
|
191 | }
|
192 | }
|
193 |
|
194 | return style;
|
195 | });
|
196 | style2 += zoomsym.join('');
|
197 | let style3 = '';
|
198 | const classes = !styleObject.classes || !(typeof styleObject.classes === 'string') ? undefined : styleObject.classes.match(re.classes)[0];
|
199 | style3 += !classes ? '' : classes;
|
200 | const id = !styleObject.id || !(typeof styleObject.id === 'string') ? undefined : styleObject.id.match(re.id)[0];
|
201 | style3 += classes || id ? '!' : '';
|
202 | style3 += !id ? '' : id + '!';
|
203 | return style1 + (style2 || style3 ? '-' + style2 : '') + (style3 ? '-' + style3 : '');
|
204 | };
|
205 |
|
206 | export { compose, parse, re };
|
207 |
|
208 |
|