UNPKG

2.08 kBJavaScriptView Raw
1
2import { re } from './swuquery-re';
3import { swu2coord } 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] != 'R' ? part : [part.slice(1, 3), part.slice(3, 5)])
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.length > 5) {
18 coord = swu2coord(part.slice(-4));
19 front = part.slice(0, -4);
20 } else {
21 front = part;
22 }
23 if (!front.includes('R')) {
24 return {
25 symbol: front,
26 coord: coord
27 }
28 } else {
29 return {
30 range: [front.slice(1, 3), front.slice(3, 5)],
31 coord: coord
32 }
33 }
34 })
35}
36
37/**
38 * Function to parse SWU query string to object
39 * @function swuquery.parse
40 * @param {string} swuQueryString - an SWU query string
41 * @returns {object} elements of an SWU query string
42 * @example
43 * swuquery.parse('QA񀀁R񀀁񆆑񆇡T񆀁R񀀁񀇱𝤆𝤆V5-')
44 *
45 * return {
46 * query: true,
47 * prefix: {
48 * required: true,
49 * parts: [
50 * '񀀁',
51 * ['񀀁', '񆆑'],
52 * '񆇡'
53 * ]
54 * },
55 * signbox: [
56 * { symbol: '񆀁' },
57 * {
58 * range: ['񀀁', '񀇱'],
59 * coord: [500, 500]
60 * }
61 * ],
62 * variance: 5,
63 * style: true
64 * }
65 */
66const parse = (swuQueryString) => {
67 const query = (typeof swuQueryString === 'string') ? swuQueryString.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 }