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.symbol}|${re.range})`, 'g'))
|
10 | .map(part => part[0] == 'S' ? part : part.slice(1).split('t'))
|
11 | }
|
12 | }
|
13 | const parseSignbox = (text) => {
|
14 | return text.match(new RegExp(`(${re.symbol}${re.coord}|${re.range}${re.coord})`, 'g'))
|
15 | .map(part => {
|
16 | let coord, front;
|
17 | if (part.includes('x')) {
|
18 | coord = fsw2coord(part.slice(-7));
|
19 | front = part.slice(0, -7);
|
20 | } else {
|
21 | front = part;
|
22 | }
|
23 | if (front.includes('S')) {
|
24 | return {
|
25 | symbol: front,
|
26 | coord: coord
|
27 | }
|
28 | } else {
|
29 | return {
|
30 | range: front.slice(1).split('t'),
|
31 | coord: coord
|
32 | }
|
33 | }
|
34 | })
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | const parse = (fswQueryString) => {
|
67 | const query = (typeof fswQueryString === 'string') ? fswQueryString.match(new RegExp(`^${re.full}`)) : undefined;
|
68 | return {
|
69 | 'query': query ? true : undefined,
|
70 | 'prefix': query && query[1] ? parsePrefix(query[1]) : undefined,
|
71 | 'signbox': query && query[2] ? parseSignbox(query[2]) : undefined,
|
72 | 'variance': query && query[3] ? parseInt(query[3].slice(1)) : undefined,
|
73 | 'style': query && query[4] ? true : undefined
|
74 | };
|
75 | }
|
76 |
|
77 | export { parse }
|