1 |
|
2 | import { re } from './style-re';
|
3 |
|
4 | const prefixColor = (color) => {
|
5 | const regex = new RegExp(`^${re.colorhex}$`);
|
6 | return (regex.test(color) ? '#' : '') + color;
|
7 | }
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | const parse = (styleString) => {
|
25 | const regex = `^${re.full}`;
|
26 | const m = ((typeof styleString === 'string') ? styleString.match(new RegExp(regex)) : []) || [];
|
27 |
|
28 | return {
|
29 | 'colorize': !m[1] ? undefined : !!m[1],
|
30 | 'padding': !m[2] ? undefined : parseInt(m[2].slice(1)),
|
31 | 'background': !m[3] ? undefined : prefixColor(m[3].slice(2, -1)),
|
32 | 'detail': !m[4] ? undefined : m[4].slice(2, -1).split(',').map(prefixColor),
|
33 | 'zoom': !m[5] ? undefined : (m[5] === 'Zx' ? 'x' : parseFloat(m[5].slice(1))),
|
34 | 'detailsym': !m[6] ? undefined : m[6].match(new RegExp(re.detailsym, 'g')).map((val) => {
|
35 | const parts = val.split('_');
|
36 | const detail = parts[1].split(',').map(prefixColor);
|
37 | return {
|
38 | 'index': parseInt(parts[0].slice(1)),
|
39 | 'detail': detail
|
40 | }
|
41 | }),
|
42 | 'zoomsym': !m[7] ? undefined : m[7].match(new RegExp(re.zoomsym, 'g')).map((val) => {
|
43 | const parts = val.split(',');
|
44 | return {
|
45 | 'index': parseInt(parts[0].slice(1)),
|
46 | 'zoom': parseFloat(parts[1]),
|
47 | 'offset': !parts[2] ? undefined : parts[2].split('x').map((val) => parseInt(val) - 500)
|
48 | }
|
49 | }),
|
50 | 'classes': !m[8] ? undefined : m[8],
|
51 | 'id': !m[9] ? undefined : m[9]
|
52 | }
|
53 | }
|
54 |
|
55 | export { parse }
|