UNPKG

2.81 kBJavaScriptView Raw
1
2import { regex } from './swuquery-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 swuquery.results
8 * @param {string} query - an SWU query string
9 * @param {string} text - a string of text containing multiple signs
10 * @returns {string[]} an array of SWU signs
11 * @example
12 * swuquery.results('QA񀀒T','𝠀񀀒񀀚񋚥񋛩𝠃𝤟𝤩񋛩𝣵𝤐񀀒𝤇𝣤񋚥𝤐𝤆񀀚𝣮𝣭 𝠀񂇢񂇈񆙡񋎥񋎵𝠃𝤛𝤬񂇈𝤀𝣺񂇢𝤄𝣻񋎥𝤄𝤗񋎵𝤃𝣟񆙡𝣱𝣸 𝠀񅨑񀀙񆉁𝠃𝤙𝤞񀀙𝣷𝤀񅨑𝣼𝤀񆉁𝣳𝣮')
13 *
14 * return [
15 * '𝠀񀀒񀀚񋚥񋛩𝠃𝤟𝤩񋛩𝣵𝤐񀀒𝤇𝣤񋚥𝤐𝤆񀀚𝣮𝣭'
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 res = regex(query);
26 if (!res) { return []; }
27 let i;
28 for (i = 0; i < res.length; i += 1) {
29 pattern = res[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 SWU query string to match signs from multiple lines of text.
51 * @function swuquery.lines
52 * @param {string} query - an SWU query string
53 * @param {string} text - multiple lines of text, each starting with an SWU sign
54 * @returns {string[]} an array of lines of text, each starting with an SWU sign
55 * @example
56 * swuquery.lines('QA񀀒T',`𝠀񀀒񀀚񋚥񋛩𝠃𝤟𝤩񋛩𝣵𝤐񀀒𝤇𝣤񋚥𝤐𝤆񀀚𝣮𝣭 line one
57 * 𝠀񂇢񂇈񆙡񋎥񋎵𝠃𝤛𝤬񂇈𝤀𝣺񂇢𝤄𝣻񋎥𝤄𝤗񋎵𝤃𝣟񆙡𝣱𝣸 line two
58 * 𝠀񅨑񀀙񆉁𝠃𝤙𝤞񀀙𝣷𝤀񅨑𝣼𝤀񆉁𝣳𝣮 line three`)
59 *
60 * return [
61 * '𝠀񀀒񀀚񋚥񋛩𝠃𝤟𝤩񋛩𝣵𝤐񀀒𝤇𝣤񋚥𝤐𝤆񀀚𝣮𝣭 line one'
62 * ]
63 */
64const lines = (query, text) => {
65 if (!text) { return []; }
66 let pattern;
67 let matches;
68 let parts;
69 let words;
70 let res = regex(query);
71 if (!res) { return []; }
72 let i;
73 for (i = 0; i < res.length; i += 1) {
74 pattern = res[i];
75 pattern = '^' + pattern + '.*';
76 matches = text.match(new RegExp(pattern, 'mg'));
77 if (matches) {
78 text = matches.join("\n");
79 } else {
80 text = '';
81 }
82 }
83 if (text) {
84 parts = text.split("\n");
85 words = parts.filter(function (element) {
86 return element in parts ? false : parts[element] = true;
87 }, {});
88 } else {
89 words = [];
90 }
91 return words;
92}
93
94export { results, lines }
\No newline at end of file