1 |
|
2 | import { re } from './fswquery-re';
|
3 | import { fsw2coord } from '../convert';
|
4 |
|
5 | const parsePrefix = (text) => {
|
6 | return {
|
7 | required: true,
|
8 | parts: text == 'T' ? undefined :
|
9 | text.match(new RegExp(`${re.list}`, 'g'))
|
10 | .map(part => {
|
11 | if (part.includes('o')) {
|
12 | return ['or'].concat(part.match(new RegExp(`(${re.item})`, 'g'))
|
13 | .map(part => part[0] == 'S' ? part : part.slice(1).split('t')))
|
14 | } else {
|
15 | return part[0] == 'S' ? part : part.slice(1).split('t')
|
16 | }
|
17 | })
|
18 | }
|
19 | }
|
20 | const parseSignbox = (text) => {
|
21 | return text.match(new RegExp(`(${re.list}${re.coord})`, 'g'))
|
22 | .map(part => {
|
23 | let coord, front;
|
24 | if (part.includes('x')) {
|
25 | coord = fsw2coord(part.slice(-7));
|
26 | front = part.slice(0, -7);
|
27 | } else {
|
28 | front = part;
|
29 | }
|
30 | if (front.includes('o')) {
|
31 | return {
|
32 | or: front.split('o').map(part => {
|
33 | if (part.includes('S')) {
|
34 | return part;
|
35 | } else {
|
36 | return part.slice(1).split('t');
|
37 | }
|
38 | }),
|
39 | coord, coord
|
40 | }
|
41 | } else if (front.includes('S')) {
|
42 | return {
|
43 | symbol: front,
|
44 | coord: coord
|
45 | }
|
46 | } else {
|
47 | return {
|
48 | range: front.slice(1).split('t'),
|
49 | coord: coord
|
50 | }
|
51 | }
|
52 | })
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | const parse = (fswQueryString) => {
|
103 | const query = (typeof fswQueryString === 'string') ? fswQueryString.match(new RegExp(`^${re.full}`)) : undefined;
|
104 | return {
|
105 | 'query': query ? true : undefined,
|
106 | 'prefix': query && query[1] ? parsePrefix(query[1]) : undefined,
|
107 | 'signbox': query && query[2] ? parseSignbox(query[2]) : undefined,
|
108 | 'variance': query && query[3] ? parseInt(query[3].slice(1)) : undefined,
|
109 | 'style': query && query[4] ? true : undefined
|
110 | };
|
111 | }
|
112 |
|
113 | export { parse }
|