1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | let re = {
|
29 | 'colorize': 'C',
|
30 | 'colorhex': '(?:[0-9a-fA-F]{3}){1,2}',
|
31 | 'colorname': '[a-zA-Z]+',
|
32 | 'padding': 'P[0-9]{2}',
|
33 | 'zoom': 'Z(?:[0-9]+(?:\\.[0-9]+)?|x)',
|
34 | 'classbase': '-?[_a-zA-Z][_a-zA-Z0-9-]{0,100}',
|
35 | 'id': '[a-zA-Z][_a-zA-Z0-9-]{0,100}'
|
36 | };
|
37 | re.colorbase = `(?:${re.colorhex}|${re.colorname})`;
|
38 | re.color = `_${re.colorbase}_`;
|
39 | re.colors = `_${re.colorbase}(?:,${re.colorbase})?_`;
|
40 | re.background = `G${re.color}`;
|
41 | re.detail = `D${re.colors}`;
|
42 | re.detailsym = `D[0-9]{2}${re.colors}`;
|
43 | re.classes = `${re.classbase}(?: ${re.classbase})*`;
|
44 | re.full = `-(${re.colorize})?(${re.padding})?(${re.background})?(${re.detail})?(${re.zoom})?(?:-((?:${re.detailsym})*))?(?:-(${re.classes})?!(?:(${re.id})!)?)?`;
|
45 |
|
46 | const prefixColor = color => {
|
47 | const regex = new RegExp(`^${re.colorhex}$`);
|
48 | return (regex.test(color) ? '#' : '') + color;
|
49 | };
|
50 |
|
51 | const definedProps = obj => Object.fromEntries(Object.entries(obj).filter(([k, v]) => v !== undefined));
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | const parse = styleString => {
|
70 | const regex = `^${re.full}`;
|
71 | const m = (typeof styleString === 'string' ? styleString.match(new RegExp(regex)) : []) || [];
|
72 | return definedProps({
|
73 | 'colorize': !m[1] ? undefined : !!m[1],
|
74 | 'padding': !m[2] ? undefined : parseInt(m[2].slice(1)),
|
75 | 'background': !m[3] ? undefined : prefixColor(m[3].slice(2, -1)),
|
76 | 'detail': !m[4] ? undefined : m[4].slice(2, -1).split(',').map(prefixColor),
|
77 | 'zoom': !m[5] ? undefined : m[5] === 'Zx' ? 'x' : parseFloat(m[5].slice(1)),
|
78 | 'detailsym': !m[6] ? undefined : m[6].match(new RegExp(re.detailsym, 'g')).map(val => {
|
79 | const parts = val.split('_');
|
80 | const detail = parts[1].split(',').map(prefixColor);
|
81 | return {
|
82 | 'index': parseInt(parts[0].slice(1)),
|
83 | 'detail': detail
|
84 | };
|
85 | }),
|
86 | 'classes': !m[7] ? undefined : m[7],
|
87 | 'id': !m[8] ? undefined : m[8]
|
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 | const compose = styleObject => {
|
121 | if (typeof styleObject !== 'object' || styleObject === null) return undefined;
|
122 |
|
123 | let style1 = '-';
|
124 | style1 += !styleObject.colorize ? '' : 'C';
|
125 | const padding = parseInt(styleObject.padding);
|
126 | style1 += !padding || padding <= 0 || padding > 99 ? '' : 'P' + (padding > 9 ? padding : '0' + padding);
|
127 | const background = !styleObject.background || !(typeof styleObject.background === 'string') ? undefined : styleObject.background.match(re.colorbase)[0];
|
128 | style1 += !background ? '' : 'G_' + background + '_';
|
129 | const detail1 = !styleObject.detail || !styleObject.detail[0] || !(typeof styleObject.detail[0] === 'string') ? undefined : styleObject.detail[0].match(re.colorbase)[0];
|
130 | const detail2 = !styleObject.detail || !styleObject.detail[1] || !(typeof styleObject.detail[1] === 'string') ? undefined : styleObject.detail[1].match(re.colorbase)[0];
|
131 |
|
132 | if (detail1) {
|
133 | style1 += 'D_' + detail1;
|
134 |
|
135 | if (detail2) {
|
136 | style1 += ',' + detail2;
|
137 | }
|
138 |
|
139 | style1 += '_';
|
140 | }
|
141 |
|
142 | const zoom = styleObject.zoom === 'x' ? 'x' : parseFloat(styleObject.zoom);
|
143 | style1 += !zoom || zoom <= 0 ? '' : 'Z' + zoom;
|
144 | let style2 = '';
|
145 | const detailsym = !styleObject.detailsym || !Array.isArray(styleObject.detailsym) ? [] : styleObject.detailsym.map(styleObject => {
|
146 | const index = parseInt(styleObject.index);
|
147 | if (!index || index <= 0 || index > 99) return '';
|
148 | let style = 'D' + (index > 9 ? index : '0' + index);
|
149 | const detail1 = !styleObject.detail || !styleObject.detail[0] ? undefined : styleObject.detail[0].match(re.colorbase)[0];
|
150 | const detail2 = !styleObject.detail || !styleObject.detail[1] ? undefined : styleObject.detail[1].match(re.colorbase)[0];
|
151 |
|
152 | if (detail1) {
|
153 | style += '_' + detail1;
|
154 |
|
155 | if (detail2) {
|
156 | style += ',' + detail2;
|
157 | }
|
158 |
|
159 | style += '_';
|
160 | }
|
161 |
|
162 | return style;
|
163 | });
|
164 | style2 += detailsym.join('');
|
165 | let style3 = '';
|
166 | const classes = !styleObject.classes || !(typeof styleObject.classes === 'string') ? undefined : styleObject.classes.match(re.classes)[0];
|
167 | style3 += !classes ? '' : classes;
|
168 | const id = !styleObject.id || !(typeof styleObject.id === 'string') ? undefined : styleObject.id.match(re.id)[0];
|
169 | style3 += classes || id ? '!' : '';
|
170 | style3 += !id ? '' : id + '!';
|
171 | return style1 + (style2 || style3 ? '-' + style2 : '') + (style3 ? '-' + style3 : '');
|
172 | };
|
173 |
|
174 | export { compose, parse, re };
|
175 |
|
176 |
|