UNPKG

3.49 kBJavaScriptView Raw
1
2import { re } from './fswquery-re';
3
4/**
5 * Function to compose FSW query string from object
6 * @function fswquery.compose
7 * @param {object} fswQueryObject - an object of style options
8 * @param {boolean} fswQueryObject.query - required true for FSW query object
9 * @param {object} fswQueryObject.prefix - an object for prefix elements
10 * @param {boolean} fswQueryObject.prefix.required - true if sorting prefix is required
11 * @param {(string|string[]|(string|string[])[])[]} fswQueryObject.prefix.parts - array of symbol strings, range arrays, and OR arrays of strings and range arrays
12 * @param {({symbol:string,coord:number[]}|{range:string[],coord:number[]}|{or:(string|string[])[],coord:number[]})[]} fswQueryObject.signbox - array of objects for symbols, ranges, and list of symbols or ranges, with optional coordinates
13 * @param {number} fswQueryObject.variance - amount that x or y coordinates can vary and find a match, defaults to 20
14 * @param {boolean} fswQueryObject.style - boolean value for including style string in matches
15 * @returns {string} FSW query string
16 * @example
17 * fswquery.compose({
18 * query: true,
19 * prefix: {
20 * required: true,
21 * parts: [
22 * 'S10000',
23 * ['100', '204'],
24 * 'S20500'
25 * ]
26 * },
27 * signbox: [
28 * { symbol: 'S20000' },
29 * {
30 * range: ['100', '105'],
31 * coord: [500, 500]
32 * }
33 * ],
34 * variance: 5,
35 * style: true
36 * })
37 *
38 * return 'QAS10000R100t204S20500TS20000R100t105500x500V5-'
39 */
40const compose = (fswQueryObject) => {
41 if (!fswQueryObject || !fswQueryObject.query) {
42 return undefined;
43 }
44
45 let query = 'Q';
46
47 if (fswQueryObject.prefix && fswQueryObject.prefix.required) {
48 if (Array.isArray(fswQueryObject.prefix.parts)) {
49 query += 'A';
50 query += fswQueryObject.prefix.parts.map(part => {
51 if (typeof part === 'string') {
52 return part;
53 } else {
54 if (Array.isArray(part) && part.length == 2) {
55 return `R${part[0]}t${part[1]}`;
56 } else if (Array.isArray(part) && part.length > 2 && part[0] == 'or') {
57 part.shift();
58 return part.map(part => {
59 if (typeof part === 'string') {
60 return part;
61 } else {
62 if (Array.isArray(part) && part.length == 2) {
63 return `R${part[0]}t${part[1]}`;
64 }
65 }
66 }).join('o')
67 }
68 }
69 }).join('')
70 }
71 query += 'T';
72 }
73
74 if (Array.isArray(fswQueryObject.signbox)) {
75 query += fswQueryObject.signbox.map(part => {
76 let out;
77 if (part.or) {
78 out = part.or.map(item => {
79 if (typeof item === 'string') {
80 return item;
81 } else {
82 if (Array.isArray(item) && item.length == 2) {
83 return `R${item[0]}t${item[1]}`;
84 }
85 }
86 }).join('o');
87 } else if (part.symbol) {
88 out = part.symbol;
89 } else {
90 if (part.range && Array.isArray(part.range) && part.range.length == 2) {
91 out = `R${part.range[0]}t${part.range[1]}`;
92 }
93 }
94 return out + (Array.isArray(part.coord) && part.coord.length == 2 ? (part.coord.join('x')) : '');
95 }).join('')
96 }
97
98 query += fswQueryObject.style ? '-' : '';
99
100 query = query.match(new RegExp(`^${re.full}`))[0];
101
102 return query;
103}
104
105export { compose }