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