1 | 'use strict';
|
2 |
|
3 | const filter = {
|
4 |
|
5 | SHORT_WORD: 2,
|
6 |
|
7 | _cachedReplaceList: null,
|
8 |
|
9 | |
10 |
|
11 |
|
12 |
|
13 |
|
14 | stringToSearchArray (wordOrArray) {
|
15 |
|
16 | const ret = [];
|
17 | let iterate = arguments;
|
18 |
|
19 | if (wordOrArray instanceof Array) {
|
20 | iterate = wordOrArray;
|
21 | }
|
22 |
|
23 | for (let i = 0; i < iterate.length; i++) {
|
24 | this._toSearchArray(iterate[i], ret);
|
25 | }
|
26 |
|
27 | return ret;
|
28 | },
|
29 |
|
30 | |
31 |
|
32 |
|
33 |
|
34 |
|
35 | searchStringToChunks (searchString) {
|
36 | return this._toSearchArray(searchString)
|
37 | .map(function (word) {
|
38 | return new RegExp(`^${this.escapeRegex(word)}`);
|
39 | }, this);
|
40 | },
|
41 |
|
42 | _toSearchArray (word, ret) {
|
43 | const start = this._replaceSpecials(word).toLowerCase();
|
44 | const split = start.split(/\s/);
|
45 |
|
46 | if (ret && ret.indexOf(start) === -1) {
|
47 | ret.push(start);
|
48 | } else if (!ret) {
|
49 | ret = [start];
|
50 | }
|
51 |
|
52 | if (split.length > 1) {
|
53 | split.forEach(function (chunk) {
|
54 | const rep = chunk.replace(/[^a-z0-9]+/g, '_');
|
55 | if (rep.length > this.SHORT_WORD) {
|
56 | if (ret.indexOf(rep) === -1) {
|
57 | ret.push(rep);
|
58 | }
|
59 | }
|
60 | }, this);
|
61 | }
|
62 | return ret;
|
63 | },
|
64 |
|
65 | |
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | generateQueryOr (search, fieldName) {
|
72 | const queryOr = [];
|
73 | const queryAnd = [];
|
74 |
|
75 | const chunksArr = filter.searchStringToChunks(search);
|
76 |
|
77 | if (chunksArr.length > 1) {
|
78 | const searchStringRegex = chunksArr.shift();
|
79 |
|
80 | const or = {};
|
81 | or[fieldName] = searchStringRegex;
|
82 | queryOr.push(or);
|
83 | }
|
84 |
|
85 | chunksArr.forEach((regex) => {
|
86 | const and = {};
|
87 | and[fieldName] = regex;
|
88 | queryAnd.push(and);
|
89 | });
|
90 |
|
91 | queryOr.push({ $and: queryAnd });
|
92 | return queryOr;
|
93 | },
|
94 |
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 | escapeRegex (word) {
|
101 | return word.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
|
102 | },
|
103 |
|
104 | _replaceSpecials (word) {
|
105 | if (this._cachedReplaceList === null) {
|
106 | const list = [];
|
107 |
|
108 | const from = 'ÄäÀàÁáÂâÃãÅåǍǎĄąĂăÆæÇçĆćĈĉČčĎđĐďðÈèÉéÊêËëĚěĘęĜĝĢģĞğĤĥÌìÍíÎîÏïıĴĵĶķĹĺĻļŁłĽ'
|
109 | + 'ľÑñŃńŇňÖöÒòÓóÔôÕõŐőØøŒœŔŕŘřẞߌśŜŝŞşŠšŤťŢţÞþÜüÙùÚúÛûŰűŨũŲųŮůŴŵÝýŸÿŶŷŹźŽžŻż';
|
110 | const replace = 'AaAaAaAaAaAaAaAaAaAaCcCcCcCcDdDddEeEeEeEeEeEeGgGgGgHhIiIiIiIiiJjKkLlLlLlL'
|
111 | + 'lNnNnNnOoOoOoOoOoOoOoOoRrRrSsSsSsSsSsTtTtTtUuUuUuUuUuUuUuUuWsYyYyYyZzZzZz';
|
112 |
|
113 | for (let i = 0; i < from.length; i++) {
|
114 | list.push({
|
115 | f: new RegExp(from[i], 'g'),
|
116 | t: replace[i]
|
117 | });
|
118 | }
|
119 |
|
120 | this._cachedReplaceList = list;
|
121 | }
|
122 |
|
123 | for (let k = 0; k < this._cachedReplaceList.length; k++) {
|
124 | word = word.replace(this._cachedReplaceList[k].f, this._cachedReplaceList[k].t);
|
125 | }
|
126 |
|
127 | return word;
|
128 | },
|
129 |
|
130 | normalize (word) {
|
131 |
|
132 | word = this._replaceSpecials(word)
|
133 | .toLowerCase()
|
134 | .replace(/[^a-z0-9:\-%]+/g, '_').replace(/(^_+)|(_+$)/g, '');
|
135 |
|
136 | return word;
|
137 | },
|
138 |
|
139 | normalizeForTranslations (word) {
|
140 |
|
141 | word = this._replaceSpecials(word)
|
142 | .replace(/[^a-z0-9:?!.\-%]+/gi, '_').replace(/(^_+)|(_+$)/g, '');
|
143 |
|
144 | return word;
|
145 | },
|
146 |
|
147 | |
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | deleteForbidden (obj, forbiddenDbObject) {
|
154 |
|
155 | for (const key in forbiddenDbObject) {
|
156 | if (Object.hasOwnProperty.call(forbiddenDbObject, key)
|
157 | && typeof obj[key] !== 'undefined') {
|
158 |
|
159 | delete obj[key];
|
160 | }
|
161 | }
|
162 |
|
163 | return obj;
|
164 | }
|
165 |
|
166 | };
|
167 |
|
168 | module.exports = filter;
|