UNPKG

2.85 kBJavaScriptView Raw
1
2import { regex } from './fswquery-regex';
3
4//needs rewritten, but it works
5/**
6 * Function that uses a query string to match signs from a string of text.
7 * @function fswquery.results
8 * @param {string} query - an FSW query string
9 * @param {string} text - a string of text containing multiple signs
10 * @returns {string[]} an array of FSW signs
11 * @example
12 * fswquery.results('QAS10011T','AS10011S10019S2e704S2e748M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475 AS15a21S15a07S21100S2df04S2df14M521x538S15a07494x488S15a21498x489S2df04498x517S2df14497x461S21100479x486 AS1f010S10018S20600M519x524S10018485x494S1f010490x494S20600481x476')
13 *
14 * return [
15 * 'AS10011S10019S2e704S2e748M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475'
16 * ]
17 */
18const results = (query, text) => {
19 if (!text) { return []; }
20
21 let pattern;
22 let matches;
23 let parts;
24 let words;
25 let re = regex(query);
26 if (!re) { return []; }
27 let i;
28 for (i = 0; i < re.length; i += 1) {
29 pattern = re[i];
30 matches = text.match(new RegExp(pattern, 'g'));
31 if (matches) {
32 text = matches.join(' ');
33 } else {
34 text = '';
35 }
36 }
37 if (text) {
38 parts = text.split(' ');
39 words = parts.filter(function (element) {
40 return element in parts ? false : parts[element] = true;
41 }, {});
42 } else {
43 words = [];
44 }
45 return words;
46}
47
48//needs rewritten, but it works
49/**
50 * Function that uses an FSW query string to match signs from multiple lines of text.
51 * @function fswquery.lines
52 * @param {string} query - an FSW query string
53 * @param {string} text - multiple lines of text, each starting with an FSW sign
54 * @returns {string[]} an array of lines of text, each starting with an FSW sign
55 * @example
56 * fswquery.lines('QAS10011T',`AS10011S10019S2e704S2e748M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475 line one
57 * AS15a21S15a07S21100S2df04S2df14M521x538S15a07494x488S15a21498x489S2df04498x517S2df14497x461S21100479x486 line two
58 * AS1f010S10018S20600M519x524S10018485x494S1f010490x494S20600481x476 line three`)
59 *
60 * return [
61 * 'AS10011S10019S2e704S2e748M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475 line one'
62 * ]
63 */
64const lines = (query, text) => {
65 if (!text) { return []; }
66
67 let pattern;
68 let matches;
69 let parts;
70 let words;
71 let re = regex(query);
72 if (!re) { return []; }
73 let i;
74 for (i = 0; i < re.length; i += 1) {
75 pattern = re[i];
76 pattern = '^' + pattern + '.*';
77 matches = text.match(new RegExp(pattern, 'mg'));
78 if (matches) {
79 text = matches.join("\n");
80 } else {
81 text = '';
82 }
83 }
84 if (text) {
85 parts = text.split("\n");
86 words = parts.filter(function (element) {
87 return element in parts ? false : parts[element] = true;
88 }, {});
89 } else {
90 words = [];
91 }
92 return words;
93}
94
95export { results, lines }
\No newline at end of file