UNPKG

2.04 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.symbol}|${re.range})`, 'g'))
10 .map(part => part[0] == 'S' ? part : part.slice(1).split('t'))
11 }
12}
13const 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 * Function to parse FSW query string to object
39 * @function fswquery.parse
40 * @param {string} fswQueryString - an FSW query string
41 * @returns {object} elements of an FSW query string
42 * @example
43 * fswquery.parse('QAS10000R100t204S20500TS20000R100t105500x500V5-')
44 *
45 * return {
46 * query: true,
47 * prefix: {
48 * required: true,
49 * parts: [
50 * 'S10000',
51 * ['100', '204'],
52 * 'S20500'
53 * ]
54 * },
55 * signbox: [
56 * { symbol: 'S20000' },
57 * {
58 * range: ['100', '105'],
59 * coord: [500, 500]
60 * }
61 * ],
62 * variance: 5,
63 * style: true
64 * }
65 */
66const 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
77export { parse }