1 |
|
2 | import { regex } from './swuquery-regex';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | const 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 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | const 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 |
|
94 | export { results, lines } |
\ | No newline at end of file |