UNPKG

2.87 kBJavaScriptView Raw
1
2import { re } from './fswquery-re';
3import { fsw2coord } from '../convert';
4
5const 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}
20const 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 * Function to parse FSW query string to object
57 * @function fswquery.parse
58 * @param {string} fswQueryString - an FSW query string
59 * @returns {object} elements of an FSW query string
60 * @example
61 * fswquery.parse('QAS10000S10500oS20500oR2fft304TS100uuR205t206oS207uu510x510V5-')
62 *
63 * return {
64 * "query": true,
65 * "prefix": {
66 * "required": true,
67 * "parts": [
68 * "S10000",
69 * [
70 * "or",
71 * "S10500",
72 * "S20500",
73 * [
74 * "2ff",
75 * "304"
76 * ]
77 * ]
78 * ]
79 * },
80 * "signbox": [
81 * {
82 * "symbol": "S100uu"
83 * },
84 * {
85 * "or": [
86 * [
87 * "205",
88 * "206"
89 * ],
90 * "S207uu"
91 * ],
92 * "coord": [
93 * 510,
94 * 510
95 * ]
96 * }
97 * ],
98 * "variance": 5,
99 * "style": true
100 * }
101 */
102const 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
113export { parse }