@orchidjs/sifter 1.0.3 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/sifter.d.ts +72 -0
- package/dist/cjs/sifter.js +290 -1101
- package/dist/cjs/sifter.js.map +1 -1
- package/dist/cjs/types.d.ts +57 -0
- package/dist/cjs/types.js +3 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/utils.d.ts +48 -0
- package/dist/cjs/utils.js +102 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/esm/sifter.d.ts +72 -0
- package/dist/esm/sifter.js +267 -330
- package/dist/esm/sifter.js.map +1 -1
- package/dist/esm/types.d.ts +57 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/utils.d.ts +48 -0
- package/dist/esm/utils.js +54 -56
- package/dist/esm/utils.js.map +1 -1
- package/dist/types/sifter.d.ts +3 -2
- package/dist/types/types.d.ts +12 -12
- package/dist/types/utils.d.ts +2 -2
- package/dist/umd/package.json +1 -0
- package/dist/umd/sifter.js +796 -1036
- package/dist/umd/sifter.js.map +1 -1
- package/dist/umd/sifter.min.js +56 -57
- package/dist/umd/sifter.min.js.map +1 -1
- package/lib/sifter.ts +3 -2
- package/lib/types.ts +1 -1
- package/lib/utils.ts +1 -1
- package/package.json +31 -21
- package/dist/esm/lib/sifter.js +0 -368
- package/dist/esm/lib/sifter.js.map +0 -1
- package/dist/esm/lib/utils.js +0 -95
- package/dist/esm/lib/utils.js.map +0 -1
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/index.js +0 -547
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/index.js.map +0 -1
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/regex.js +0 -113
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/regex.js.map +0 -1
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/strings.js +0 -29
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/strings.js.map +0 -1
package/dist/umd/sifter.js
CHANGED
|
@@ -1,1145 +1,905 @@
|
|
|
1
1
|
/*! sifter.js | https://github.com/orchidjs/sifter.js | Apache License (v2) */
|
|
2
2
|
(function (global, factory) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}(this, (function (exports) { 'use strict';
|
|
3
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
4
|
+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
5
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.sifter = {}));
|
|
6
|
+
})(this, (function (exports) { 'use strict';
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const arrayToPattern = chars => {
|
|
18
|
-
chars = chars.filter(Boolean);
|
|
19
|
-
|
|
20
|
-
if (chars.length < 2) {
|
|
21
|
-
return chars[0] || '';
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return maxValueLength(chars) == 1 ? '[' + chars.join('') + ']' : '(?:' + chars.join('|') + ')';
|
|
25
|
-
};
|
|
26
|
-
/**
|
|
27
|
-
* @param {string[]} array
|
|
28
|
-
* @return {string}
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const sequencePattern = array => {
|
|
33
|
-
if (!hasDuplicates(array)) {
|
|
34
|
-
return array.join('');
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
let pattern = '';
|
|
38
|
-
let prev_char_count = 0;
|
|
39
|
-
|
|
40
|
-
const prev_pattern = () => {
|
|
41
|
-
if (prev_char_count > 1) {
|
|
42
|
-
pattern += '{' + prev_char_count + '}';
|
|
8
|
+
/**
|
|
9
|
+
* Convert array of strings to a regular expression
|
|
10
|
+
* ex ['ab','a'] => (?:ab|a)
|
|
11
|
+
* ex ['a','b'] => [ab]
|
|
12
|
+
*/
|
|
13
|
+
const arrayToPattern = chars => {
|
|
14
|
+
chars = chars.filter(Boolean);
|
|
15
|
+
if (chars.length < 2) {
|
|
16
|
+
return chars[0] || '';
|
|
43
17
|
}
|
|
18
|
+
return maxValueLength(chars) == 1 ? '[' + chars.join('') + ']' : '(?:' + chars.join('|') + ')';
|
|
44
19
|
};
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
prev_char_count++;
|
|
49
|
-
return;
|
|
20
|
+
const sequencePattern = array => {
|
|
21
|
+
if (!hasDuplicates(array)) {
|
|
22
|
+
return array.join('');
|
|
50
23
|
}
|
|
51
|
-
|
|
24
|
+
let pattern = '';
|
|
25
|
+
let prev_char_count = 0;
|
|
26
|
+
const prev_pattern = () => {
|
|
27
|
+
if (prev_char_count > 1) {
|
|
28
|
+
pattern += '{' + prev_char_count + '}';
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
array.forEach((char, i) => {
|
|
32
|
+
if (char === array[i - 1]) {
|
|
33
|
+
prev_char_count++;
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
prev_pattern();
|
|
37
|
+
pattern += char;
|
|
38
|
+
prev_char_count = 1;
|
|
39
|
+
});
|
|
52
40
|
prev_pattern();
|
|
53
|
-
pattern
|
|
54
|
-
|
|
55
|
-
});
|
|
56
|
-
prev_pattern();
|
|
57
|
-
return pattern;
|
|
58
|
-
};
|
|
59
|
-
/**
|
|
60
|
-
* Convert array of strings to a regular expression
|
|
61
|
-
* ex ['ab','a'] => (?:ab|a)
|
|
62
|
-
* ex ['a','b'] => [ab]
|
|
63
|
-
* @param {Set<string>} chars
|
|
64
|
-
* @return {string}
|
|
65
|
-
*/
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const setToPattern = chars => {
|
|
69
|
-
let array = toArray(chars);
|
|
70
|
-
return arrayToPattern(array);
|
|
71
|
-
};
|
|
72
|
-
/**
|
|
73
|
-
*
|
|
74
|
-
* https://stackoverflow.com/questions/7376598/in-javascript-how-do-i-check-if-an-array-has-duplicate-values
|
|
75
|
-
* @param {any[]} array
|
|
76
|
-
*/
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const hasDuplicates = array => {
|
|
80
|
-
return new Set(array).size !== array.length;
|
|
81
|
-
};
|
|
82
|
-
/**
|
|
83
|
-
* https://stackoverflow.com/questions/63006601/why-does-u-throw-an-invalid-escape-error
|
|
84
|
-
* @param {string} str
|
|
85
|
-
* @return {string}
|
|
86
|
-
*/
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const escape_regex = str => {
|
|
90
|
-
return (str + '').replace(/([\$\(-\+\.\?\[-\^\{-\}])/g, '\\$1');
|
|
91
|
-
};
|
|
92
|
-
/**
|
|
93
|
-
* Return the max length of array values
|
|
94
|
-
* @param {string[]} array
|
|
95
|
-
*
|
|
96
|
-
*/
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const maxValueLength = array => {
|
|
100
|
-
return array.reduce((longest, value) => Math.max(longest, unicodeLength(value)), 0);
|
|
101
|
-
};
|
|
102
|
-
/**
|
|
103
|
-
* @param {string} str
|
|
104
|
-
*/
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const unicodeLength = str => {
|
|
108
|
-
return toArray(str).length;
|
|
109
|
-
};
|
|
110
|
-
/**
|
|
111
|
-
* @param {any} p
|
|
112
|
-
* @return {any[]}
|
|
113
|
-
*/
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const toArray = p => Array.from(p);
|
|
117
|
-
|
|
118
|
-
/*! @orchidjs/unicode-variants | https://github.com/orchidjs/unicode-variants | Apache License (v2) */
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Get all possible combinations of substrings that add up to the given string
|
|
122
|
-
* https://stackoverflow.com/questions/30169587/find-all-the-combination-of-substrings-that-add-up-to-the-given-string
|
|
123
|
-
* @param {string} input
|
|
124
|
-
* @return {string[][]}
|
|
125
|
-
*/
|
|
126
|
-
const allSubstrings = input => {
|
|
127
|
-
if (input.length === 1) return [[input]];
|
|
128
|
-
/** @type {string[][]} */
|
|
129
|
-
|
|
130
|
-
let result = [];
|
|
131
|
-
const start = input.substring(1);
|
|
132
|
-
const suba = allSubstrings(start);
|
|
133
|
-
suba.forEach(function (subresult) {
|
|
134
|
-
let tmp = subresult.slice(0);
|
|
135
|
-
tmp[0] = input.charAt(0) + tmp[0];
|
|
136
|
-
result.push(tmp);
|
|
137
|
-
tmp = subresult.slice(0);
|
|
138
|
-
tmp.unshift(input.charAt(0));
|
|
139
|
-
result.push(tmp);
|
|
140
|
-
});
|
|
141
|
-
return result;
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
/*! @orchidjs/unicode-variants | https://github.com/orchidjs/unicode-variants | Apache License (v2) */
|
|
145
|
-
/**
|
|
146
|
-
* @typedef {{[key:string]:string}} TUnicodeMap
|
|
147
|
-
* @typedef {{[key:string]:Set<string>}} TUnicodeSets
|
|
148
|
-
* @typedef {[[number,number]]} TCodePoints
|
|
149
|
-
* @typedef {{folded:string,composed:string,code_point:number}} TCodePointObj
|
|
150
|
-
* @typedef {{start:number,end:number,length:number,substr:string}} TSequencePart
|
|
151
|
-
*/
|
|
152
|
-
|
|
153
|
-
/** @type {TCodePoints} */
|
|
154
|
-
|
|
155
|
-
const code_points = [[0, 65535]];
|
|
156
|
-
const accent_pat = '[\u0300-\u036F\u{b7}\u{2be}\u{2bc}]';
|
|
157
|
-
/** @type {TUnicodeMap} */
|
|
158
|
-
|
|
159
|
-
let unicode_map;
|
|
160
|
-
/** @type {RegExp} */
|
|
161
|
-
|
|
162
|
-
let multi_char_reg;
|
|
163
|
-
const max_char_length = 3;
|
|
164
|
-
/** @type {TUnicodeMap} */
|
|
165
|
-
|
|
166
|
-
const latin_convert = {};
|
|
167
|
-
/** @type {TUnicodeMap} */
|
|
168
|
-
|
|
169
|
-
const latin_condensed = {
|
|
170
|
-
'/': '⁄∕',
|
|
171
|
-
'0': '߀',
|
|
172
|
-
"a": "ⱥɐɑ",
|
|
173
|
-
"aa": "ꜳ",
|
|
174
|
-
"ae": "æǽǣ",
|
|
175
|
-
"ao": "ꜵ",
|
|
176
|
-
"au": "ꜷ",
|
|
177
|
-
"av": "ꜹꜻ",
|
|
178
|
-
"ay": "ꜽ",
|
|
179
|
-
"b": "ƀɓƃ",
|
|
180
|
-
"c": "ꜿƈȼↄ",
|
|
181
|
-
"d": "đɗɖᴅƌꮷԁɦ",
|
|
182
|
-
"e": "ɛǝᴇɇ",
|
|
183
|
-
"f": "ꝼƒ",
|
|
184
|
-
"g": "ǥɠꞡᵹꝿɢ",
|
|
185
|
-
"h": "ħⱨⱶɥ",
|
|
186
|
-
"i": "ɨı",
|
|
187
|
-
"j": "ɉȷ",
|
|
188
|
-
"k": "ƙⱪꝁꝃꝅꞣ",
|
|
189
|
-
"l": "łƚɫⱡꝉꝇꞁɭ",
|
|
190
|
-
"m": "ɱɯϻ",
|
|
191
|
-
"n": "ꞥƞɲꞑᴎлԉ",
|
|
192
|
-
"o": "øǿɔɵꝋꝍᴑ",
|
|
193
|
-
"oe": "œ",
|
|
194
|
-
"oi": "ƣ",
|
|
195
|
-
"oo": "ꝏ",
|
|
196
|
-
"ou": "ȣ",
|
|
197
|
-
"p": "ƥᵽꝑꝓꝕρ",
|
|
198
|
-
"q": "ꝗꝙɋ",
|
|
199
|
-
"r": "ɍɽꝛꞧꞃ",
|
|
200
|
-
"s": "ßȿꞩꞅʂ",
|
|
201
|
-
"t": "ŧƭʈⱦꞇ",
|
|
202
|
-
"th": "þ",
|
|
203
|
-
"tz": "ꜩ",
|
|
204
|
-
"u": "ʉ",
|
|
205
|
-
"v": "ʋꝟʌ",
|
|
206
|
-
"vy": "ꝡ",
|
|
207
|
-
"w": "ⱳ",
|
|
208
|
-
"y": "ƴɏỿ",
|
|
209
|
-
"z": "ƶȥɀⱬꝣ",
|
|
210
|
-
"hv": "ƕ"
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
for (let latin in latin_condensed) {
|
|
214
|
-
let unicode = latin_condensed[latin] || '';
|
|
215
|
-
|
|
216
|
-
for (let i = 0; i < unicode.length; i++) {
|
|
217
|
-
let char = unicode.substring(i, i + 1);
|
|
218
|
-
latin_convert[char] = latin;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
const convert_pat = new RegExp(Object.keys(latin_convert).join('|') + '|' + accent_pat, 'gu');
|
|
223
|
-
/**
|
|
224
|
-
* Initialize the unicode_map from the give code point ranges
|
|
225
|
-
*
|
|
226
|
-
* @param {TCodePoints=} _code_points
|
|
227
|
-
*/
|
|
228
|
-
|
|
229
|
-
const initialize = _code_points => {
|
|
230
|
-
if (unicode_map !== undefined) return;
|
|
231
|
-
unicode_map = generateMap(_code_points || code_points);
|
|
232
|
-
};
|
|
233
|
-
/**
|
|
234
|
-
* Helper method for normalize a string
|
|
235
|
-
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize
|
|
236
|
-
* @param {string} str
|
|
237
|
-
* @param {string} form
|
|
238
|
-
*/
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
const normalize = (str, form = 'NFKD') => str.normalize(form);
|
|
242
|
-
/**
|
|
243
|
-
* Remove accents without reordering string
|
|
244
|
-
* calling str.normalize('NFKD') on \u{594}\u{595}\u{596} becomes \u{596}\u{594}\u{595}
|
|
245
|
-
* via https://github.com/krisk/Fuse/issues/133#issuecomment-318692703
|
|
246
|
-
* @param {string} str
|
|
247
|
-
* @return {string}
|
|
248
|
-
*/
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
const asciifold = str => {
|
|
252
|
-
return toArray(str).reduce(
|
|
41
|
+
return pattern;
|
|
42
|
+
};
|
|
253
43
|
/**
|
|
254
|
-
*
|
|
255
|
-
*
|
|
44
|
+
* Convert array of strings to a regular expression
|
|
45
|
+
* ex ['ab','a'] => (?:ab|a)
|
|
46
|
+
* ex ['a','b'] => [ab]
|
|
256
47
|
*/
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* @param {string} str
|
|
263
|
-
* @return {string}
|
|
264
|
-
*/
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
const _asciifold = str => {
|
|
268
|
-
str = normalize(str).toLowerCase().replace(convert_pat,
|
|
269
|
-
/** @type {string} */
|
|
270
|
-
char => {
|
|
271
|
-
return latin_convert[char] || '';
|
|
272
|
-
}); //return str;
|
|
273
|
-
|
|
274
|
-
return normalize(str, 'NFC');
|
|
275
|
-
};
|
|
276
|
-
/**
|
|
277
|
-
* Generate a list of unicode variants from the list of code points
|
|
278
|
-
* @param {TCodePoints} code_points
|
|
279
|
-
* @yield {TCodePointObj}
|
|
280
|
-
*/
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
function* generator(code_points) {
|
|
284
|
-
for (const [code_point_min, code_point_max] of code_points) {
|
|
285
|
-
for (let i = code_point_min; i <= code_point_max; i++) {
|
|
286
|
-
let composed = String.fromCharCode(i);
|
|
287
|
-
let folded = asciifold(composed);
|
|
288
|
-
|
|
289
|
-
if (folded == composed.toLowerCase()) {
|
|
290
|
-
continue;
|
|
291
|
-
} // skip when folded is a string longer than 3 characters long
|
|
292
|
-
// bc the resulting regex patterns will be long
|
|
293
|
-
// eg:
|
|
294
|
-
// folded صلى الله عليه وسلم length 18 code point 65018
|
|
295
|
-
// folded جل جلاله length 8 code point 65019
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
if (folded.length > max_char_length) {
|
|
299
|
-
continue;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
if (folded.length == 0) {
|
|
303
|
-
continue;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
yield {
|
|
307
|
-
folded: folded,
|
|
308
|
-
composed: composed,
|
|
309
|
-
code_point: i
|
|
310
|
-
};
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Generate a unicode map from the list of code points
|
|
316
|
-
* @param {TCodePoints} code_points
|
|
317
|
-
* @return {TUnicodeSets}
|
|
318
|
-
*/
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
const generateSets = code_points => {
|
|
322
|
-
/** @type {{[key:string]:Set<string>}} */
|
|
323
|
-
const unicode_sets = {};
|
|
48
|
+
const setToPattern = chars => {
|
|
49
|
+
let array = Array.from(chars);
|
|
50
|
+
return arrayToPattern(array);
|
|
51
|
+
};
|
|
324
52
|
/**
|
|
325
|
-
*
|
|
326
|
-
* @param {string} to_add
|
|
53
|
+
* https://stackoverflow.com/questions/7376598/in-javascript-how-do-i-check-if-an-array-has-duplicate-values
|
|
327
54
|
*/
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
55
|
+
const hasDuplicates = array => {
|
|
56
|
+
return new Set(array).size !== array.length;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* https://stackoverflow.com/questions/63006601/why-does-u-throw-an-invalid-escape-error
|
|
60
|
+
*/
|
|
61
|
+
const escape_regex = str => {
|
|
62
|
+
return (str + '').replace(/([\$\(-\+\.\?\[-\^\{-\}])/g, '\\$1');
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Return the max length of array values
|
|
66
|
+
*/
|
|
67
|
+
const maxValueLength = array => {
|
|
68
|
+
return array.reduce((longest, value) => Math.max(longest, unicodeLength(value)), 0);
|
|
69
|
+
};
|
|
70
|
+
const unicodeLength = str => {
|
|
71
|
+
return Array.from(str).length;
|
|
340
72
|
};
|
|
341
73
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
/** @type {TUnicodeMap} */
|
|
362
|
-
|
|
363
|
-
const unicode_map = {};
|
|
364
|
-
/** @type {string[]} */
|
|
365
|
-
|
|
366
|
-
let multi_char = [];
|
|
367
|
-
|
|
368
|
-
for (let folded in unicode_sets) {
|
|
369
|
-
let set = unicode_sets[folded];
|
|
370
|
-
|
|
371
|
-
if (set) {
|
|
372
|
-
unicode_map[folded] = setToPattern(set);
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
if (folded.length > 1) {
|
|
376
|
-
multi_char.push(escape_regex(folded));
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
multi_char.sort((a, b) => b.length - a.length);
|
|
381
|
-
const multi_char_patt = arrayToPattern(multi_char);
|
|
382
|
-
multi_char_reg = new RegExp('^' + multi_char_patt, 'u');
|
|
383
|
-
return unicode_map;
|
|
384
|
-
};
|
|
385
|
-
/**
|
|
386
|
-
* Map each element of an array from it's folded value to all possible unicode matches
|
|
387
|
-
* @param {string[]} strings
|
|
388
|
-
* @param {number} min_replacement
|
|
389
|
-
* @return {string}
|
|
390
|
-
*/
|
|
391
|
-
|
|
74
|
+
/**
|
|
75
|
+
* Get all possible combinations of substrings that add up to the given string
|
|
76
|
+
* https://stackoverflow.com/questions/30169587/find-all-the-combination-of-substrings-that-add-up-to-the-given-string
|
|
77
|
+
*/
|
|
78
|
+
const allSubstrings = input => {
|
|
79
|
+
if (input.length === 1) return [[input]];
|
|
80
|
+
let result = [];
|
|
81
|
+
const start = input.substring(1);
|
|
82
|
+
const suba = allSubstrings(start);
|
|
83
|
+
suba.forEach(function (subresult) {
|
|
84
|
+
let tmp = subresult.slice(0);
|
|
85
|
+
tmp[0] = input.charAt(0) + tmp[0];
|
|
86
|
+
result.push(tmp);
|
|
87
|
+
tmp = subresult.slice(0);
|
|
88
|
+
tmp.unshift(input.charAt(0));
|
|
89
|
+
result.push(tmp);
|
|
90
|
+
});
|
|
91
|
+
return result;
|
|
92
|
+
};
|
|
392
93
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
94
|
+
const code_points = [[0, 65535]];
|
|
95
|
+
const accent_pat = '[\u0300-\u036F\u{b7}\u{2be}\u{2bc}]';
|
|
96
|
+
let unicode_map;
|
|
97
|
+
let multi_char_reg;
|
|
98
|
+
const max_char_length = 3;
|
|
99
|
+
const latin_convert = {};
|
|
100
|
+
const latin_condensed = {
|
|
101
|
+
'/': '⁄∕',
|
|
102
|
+
'0': '߀',
|
|
103
|
+
"a": "ⱥɐɑ",
|
|
104
|
+
"aa": "ꜳ",
|
|
105
|
+
"ae": "æǽǣ",
|
|
106
|
+
"ao": "ꜵ",
|
|
107
|
+
"au": "ꜷ",
|
|
108
|
+
"av": "ꜹꜻ",
|
|
109
|
+
"ay": "ꜽ",
|
|
110
|
+
"b": "ƀɓƃ",
|
|
111
|
+
"c": "ꜿƈȼↄ",
|
|
112
|
+
"d": "đɗɖᴅƌꮷԁɦ",
|
|
113
|
+
"e": "ɛǝᴇɇ",
|
|
114
|
+
"f": "ꝼƒ",
|
|
115
|
+
"g": "ǥɠꞡᵹꝿɢ",
|
|
116
|
+
"h": "ħⱨⱶɥ",
|
|
117
|
+
"i": "ɨı",
|
|
118
|
+
"j": "ɉȷ",
|
|
119
|
+
"k": "ƙⱪꝁꝃꝅꞣ",
|
|
120
|
+
"l": "łƚɫⱡꝉꝇꞁɭ",
|
|
121
|
+
"m": "ɱɯϻ",
|
|
122
|
+
"n": "ꞥƞɲꞑᴎлԉ",
|
|
123
|
+
"o": "øǿɔɵꝋꝍᴑ",
|
|
124
|
+
"oe": "œ",
|
|
125
|
+
"oi": "ƣ",
|
|
126
|
+
"oo": "ꝏ",
|
|
127
|
+
"ou": "ȣ",
|
|
128
|
+
"p": "ƥᵽꝑꝓꝕρ",
|
|
129
|
+
"q": "ꝗꝙɋ",
|
|
130
|
+
"r": "ɍɽꝛꞧꞃ",
|
|
131
|
+
"s": "ßȿꞩꞅʂ",
|
|
132
|
+
"t": "ŧƭʈⱦꞇ",
|
|
133
|
+
"th": "þ",
|
|
134
|
+
"tz": "ꜩ",
|
|
135
|
+
"u": "ʉ",
|
|
136
|
+
"v": "ʋꝟʌ",
|
|
137
|
+
"vy": "ꝡ",
|
|
138
|
+
"w": "ⱳ",
|
|
139
|
+
"y": "ƴɏỿ",
|
|
140
|
+
"z": "ƶȥɀⱬꝣ",
|
|
141
|
+
"hv": "ƕ"
|
|
142
|
+
};
|
|
143
|
+
for (let latin in latin_condensed) {
|
|
144
|
+
let unicode = latin_condensed[latin] || '';
|
|
145
|
+
for (let i = 0; i < unicode.length; i++) {
|
|
146
|
+
let char = unicode.substring(i, i + 1);
|
|
147
|
+
latin_convert[char] = latin;
|
|
398
148
|
}
|
|
399
|
-
|
|
400
|
-
return unicode_map[str] || str;
|
|
401
|
-
});
|
|
402
|
-
|
|
403
|
-
if (chars_replaced >= min_replacement) {
|
|
404
|
-
return sequencePattern(strings);
|
|
405
149
|
}
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
return arrayToPattern(allSubstrings(str).map(sub_pat => {
|
|
427
|
-
return mapSequence(sub_pat, min_replacement);
|
|
428
|
-
}));
|
|
429
|
-
};
|
|
430
|
-
/**
|
|
431
|
-
* Convert an array of sequences into a pattern
|
|
432
|
-
* [{start:0,end:3,length:3,substr:'iii'}...] => (?:iii...)
|
|
433
|
-
*
|
|
434
|
-
* @param {Sequence[]} sequences
|
|
435
|
-
* @param {boolean} all
|
|
436
|
-
*/
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
const sequencesToPattern = (sequences, all = true) => {
|
|
440
|
-
let min_replacement = sequences.length > 1 ? 1 : 0;
|
|
441
|
-
return arrayToPattern(sequences.map(sequence => {
|
|
442
|
-
let seq = [];
|
|
443
|
-
const len = all ? sequence.length() : sequence.length() - 1;
|
|
444
|
-
|
|
445
|
-
for (let j = 0; j < len; j++) {
|
|
446
|
-
seq.push(substringsToPattern(sequence.substrs[j] || '', min_replacement));
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
return sequencePattern(seq);
|
|
450
|
-
}));
|
|
451
|
-
};
|
|
452
|
-
/**
|
|
453
|
-
* Return true if the sequence is already in the sequences
|
|
454
|
-
* @param {Sequence} needle_seq
|
|
455
|
-
* @param {Sequence[]} sequences
|
|
456
|
-
*/
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
const inSequences = (needle_seq, sequences) => {
|
|
460
|
-
for (const seq of sequences) {
|
|
461
|
-
if (seq.start != needle_seq.start || seq.end != needle_seq.end) {
|
|
462
|
-
continue;
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
if (seq.substrs.join('') !== needle_seq.substrs.join('')) {
|
|
466
|
-
continue;
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
let needle_parts = needle_seq.parts;
|
|
150
|
+
const convert_pat = new RegExp(Object.keys(latin_convert).join('|') + '|' + accent_pat, 'gu');
|
|
151
|
+
/**
|
|
152
|
+
* Initialize the unicode_map from the give code point ranges
|
|
153
|
+
*/
|
|
154
|
+
const initialize = _code_points => {
|
|
155
|
+
if (unicode_map !== undefined) return;
|
|
156
|
+
unicode_map = generateMap(code_points);
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Helper method for normalize a string
|
|
160
|
+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize
|
|
161
|
+
*/
|
|
162
|
+
const normalize = (str, form = 'NFKD') => str.normalize(form);
|
|
163
|
+
/**
|
|
164
|
+
* Remove accents without reordering string
|
|
165
|
+
* calling str.normalize('NFKD') on \u{594}\u{595}\u{596} becomes \u{596}\u{594}\u{595}
|
|
166
|
+
* via https://github.com/krisk/Fuse/issues/133#issuecomment-318692703
|
|
167
|
+
*/
|
|
168
|
+
const asciifold = str => {
|
|
169
|
+
return Array.from(str).reduce(
|
|
470
170
|
/**
|
|
471
|
-
* @param {
|
|
171
|
+
* @param {string} result
|
|
172
|
+
* @param {string} char
|
|
472
173
|
*/
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
174
|
+
(result, char) => {
|
|
175
|
+
return result + _asciifold(char);
|
|
176
|
+
}, '');
|
|
177
|
+
};
|
|
178
|
+
const _asciifold = str => {
|
|
179
|
+
str = normalize(str).toLowerCase().replace(convert_pat, (/** @type {string} */char) => {
|
|
180
|
+
return latin_convert[char] || '';
|
|
181
|
+
});
|
|
182
|
+
//return str;
|
|
183
|
+
return normalize(str, 'NFC');
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* Generate a list of unicode variants from the list of code points
|
|
187
|
+
*/
|
|
188
|
+
function* generator(code_points) {
|
|
189
|
+
for (const [code_point_min, code_point_max] of code_points) {
|
|
190
|
+
for (let i = code_point_min; i <= code_point_max; i++) {
|
|
191
|
+
let composed = String.fromCharCode(i);
|
|
192
|
+
let folded = asciifold(composed);
|
|
193
|
+
if (folded == composed.toLowerCase()) {
|
|
194
|
+
continue;
|
|
478
195
|
}
|
|
479
|
-
|
|
480
|
-
|
|
196
|
+
// skip when folded is a string longer than 3 characters long
|
|
197
|
+
// bc the resulting regex patterns will be long
|
|
198
|
+
// eg:
|
|
199
|
+
// folded صلى الله عليه وسلم length 18 code point 65018
|
|
200
|
+
// folded جل جلاله length 8 code point 65019
|
|
201
|
+
if (folded.length > max_char_length) {
|
|
481
202
|
continue;
|
|
482
|
-
} // check for overlapping parts
|
|
483
|
-
// a = ['::=','==']
|
|
484
|
-
// b = ['::','===']
|
|
485
|
-
// a = ['r','sm']
|
|
486
|
-
// b = ['rs','m']
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
if (part.start < needle_part.start && part.end > needle_part.start) {
|
|
490
|
-
return true;
|
|
491
203
|
}
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
return true;
|
|
204
|
+
if (folded.length == 0) {
|
|
205
|
+
continue;
|
|
495
206
|
}
|
|
207
|
+
yield {
|
|
208
|
+
folded: folded,
|
|
209
|
+
composed: composed,
|
|
210
|
+
code_point: i
|
|
211
|
+
};
|
|
496
212
|
}
|
|
497
|
-
|
|
498
|
-
return false;
|
|
499
|
-
};
|
|
500
|
-
|
|
501
|
-
let filtered = seq.parts.filter(filter);
|
|
502
|
-
|
|
503
|
-
if (filtered.length > 0) {
|
|
504
|
-
continue;
|
|
505
213
|
}
|
|
506
|
-
|
|
507
|
-
return true;
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
return false;
|
|
511
|
-
};
|
|
512
|
-
|
|
513
|
-
class Sequence {
|
|
514
|
-
constructor() {
|
|
515
|
-
/** @type {TSequencePart[]} */
|
|
516
|
-
this.parts = [];
|
|
517
|
-
/** @type {string[]} */
|
|
518
|
-
|
|
519
|
-
this.substrs = [];
|
|
520
|
-
this.start = 0;
|
|
521
|
-
this.end = 0;
|
|
522
214
|
}
|
|
523
215
|
/**
|
|
524
|
-
*
|
|
216
|
+
* Generate a unicode map from the list of code points
|
|
525
217
|
*/
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
218
|
+
const generateSets = code_points => {
|
|
219
|
+
const unicode_sets = {};
|
|
220
|
+
const addMatching = (folded, to_add) => {
|
|
221
|
+
/** @type {Set<string>} */
|
|
222
|
+
const folded_set = unicode_sets[folded] || new Set();
|
|
223
|
+
const patt = new RegExp('^' + setToPattern(folded_set) + '$', 'iu');
|
|
224
|
+
if (to_add.match(patt)) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
folded_set.add(escape_regex(to_add));
|
|
228
|
+
unicode_sets[folded] = folded_set;
|
|
229
|
+
};
|
|
230
|
+
for (let value of generator(code_points)) {
|
|
231
|
+
addMatching(value.folded, value.folded);
|
|
232
|
+
addMatching(value.folded, value.composed);
|
|
534
233
|
}
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
last() {
|
|
538
|
-
return this.parts[this.parts.length - 1];
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
length() {
|
|
542
|
-
return this.parts.length;
|
|
543
|
-
}
|
|
234
|
+
return unicode_sets;
|
|
235
|
+
};
|
|
544
236
|
/**
|
|
545
|
-
*
|
|
546
|
-
*
|
|
237
|
+
* Generate a unicode map from the list of code points
|
|
238
|
+
* ae => (?:(?:ae|Æ|Ǽ|Ǣ)|(?:A|Ⓐ|A...)(?:E|ɛ|Ⓔ...))
|
|
547
239
|
*/
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
let
|
|
552
|
-
let
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
240
|
+
const generateMap = code_points => {
|
|
241
|
+
const unicode_sets = generateSets(code_points);
|
|
242
|
+
const unicode_map = {};
|
|
243
|
+
let multi_char = [];
|
|
244
|
+
for (let folded in unicode_sets) {
|
|
245
|
+
let set = unicode_sets[folded];
|
|
246
|
+
if (set) {
|
|
247
|
+
unicode_map[folded] = setToPattern(set);
|
|
248
|
+
}
|
|
249
|
+
if (folded.length > 1) {
|
|
250
|
+
multi_char.push(escape_regex(folded));
|
|
251
|
+
}
|
|
557
252
|
}
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
253
|
+
multi_char.sort((a, b) => b.length - a.length);
|
|
254
|
+
const multi_char_patt = arrayToPattern(multi_char);
|
|
255
|
+
multi_char_reg = new RegExp('^' + multi_char_patt, 'u');
|
|
256
|
+
return unicode_map;
|
|
257
|
+
};
|
|
258
|
+
/**
|
|
259
|
+
* Map each element of an array from its folded value to all possible unicode matches
|
|
260
|
+
*/
|
|
261
|
+
const mapSequence = (strings, min_replacement = 1) => {
|
|
262
|
+
let chars_replaced = 0;
|
|
263
|
+
strings = strings.map(str => {
|
|
264
|
+
if (unicode_map[str]) {
|
|
265
|
+
chars_replaced += str.length;
|
|
266
|
+
}
|
|
267
|
+
return unicode_map[str] || str;
|
|
566
268
|
});
|
|
567
|
-
|
|
269
|
+
if (chars_replaced >= min_replacement) {
|
|
270
|
+
return sequencePattern(strings);
|
|
271
|
+
}
|
|
272
|
+
return '';
|
|
273
|
+
};
|
|
274
|
+
/**
|
|
275
|
+
* Convert a short string and split it into all possible patterns
|
|
276
|
+
* Keep a pattern only if min_replacement is met
|
|
277
|
+
*
|
|
278
|
+
* 'abc'
|
|
279
|
+
* => [['abc'],['ab','c'],['a','bc'],['a','b','c']]
|
|
280
|
+
* => ['abc-pattern','ab-c-pattern'...]
|
|
281
|
+
*/
|
|
282
|
+
const substringsToPattern = (str, min_replacement = 1) => {
|
|
283
|
+
min_replacement = Math.max(min_replacement, str.length - 1);
|
|
284
|
+
return arrayToPattern(allSubstrings(str).map(sub_pat => {
|
|
285
|
+
return mapSequence(sub_pat, min_replacement);
|
|
286
|
+
}));
|
|
287
|
+
};
|
|
288
|
+
/**
|
|
289
|
+
* Convert an array of sequences into a pattern
|
|
290
|
+
* [{start:0,end:3,length:3,substr:'iii'}...] => (?:iii...)
|
|
291
|
+
*/
|
|
292
|
+
const sequencesToPattern = (sequences, all = true) => {
|
|
293
|
+
let min_replacement = sequences.length > 1 ? 1 : 0;
|
|
294
|
+
return arrayToPattern(sequences.map(sequence => {
|
|
295
|
+
let seq = [];
|
|
296
|
+
const len = all ? sequence.length() : sequence.length() - 1;
|
|
297
|
+
for (let j = 0; j < len; j++) {
|
|
298
|
+
seq.push(substringsToPattern(sequence.substrs[j] || '', min_replacement));
|
|
299
|
+
}
|
|
300
|
+
return sequencePattern(seq);
|
|
301
|
+
}));
|
|
302
|
+
};
|
|
303
|
+
/**
|
|
304
|
+
* Return true if the sequence is already in the sequences
|
|
305
|
+
*/
|
|
306
|
+
const inSequences = (needle_seq, sequences) => {
|
|
307
|
+
for (const seq of sequences) {
|
|
308
|
+
if (seq.start != needle_seq.start || seq.end != needle_seq.end) {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (seq.substrs.join('') !== needle_seq.substrs.join('')) {
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
let needle_parts = needle_seq.parts;
|
|
315
|
+
const filter = part => {
|
|
316
|
+
for (const needle_part of needle_parts) {
|
|
317
|
+
if (needle_part.start === part.start && needle_part.substr === part.substr) {
|
|
318
|
+
return false;
|
|
319
|
+
}
|
|
320
|
+
if (part.length == 1 || needle_part.length == 1) {
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
// check for overlapping parts
|
|
324
|
+
// a = ['::=','==']
|
|
325
|
+
// b = ['::','===']
|
|
326
|
+
// a = ['r','sm']
|
|
327
|
+
// b = ['rs','m']
|
|
328
|
+
if (part.start < needle_part.start && part.end > needle_part.start) {
|
|
329
|
+
return true;
|
|
330
|
+
}
|
|
331
|
+
if (needle_part.start < part.start && needle_part.end > part.start) {
|
|
332
|
+
return true;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return false;
|
|
336
|
+
};
|
|
337
|
+
let filtered = seq.parts.filter(filter);
|
|
338
|
+
if (filtered.length > 0) {
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
return true;
|
|
342
|
+
}
|
|
343
|
+
return false;
|
|
344
|
+
};
|
|
345
|
+
class Sequence {
|
|
346
|
+
constructor() {
|
|
347
|
+
this.parts = void 0;
|
|
348
|
+
this.substrs = void 0;
|
|
349
|
+
this.start = void 0;
|
|
350
|
+
this.end = void 0;
|
|
351
|
+
this.parts = [];
|
|
352
|
+
this.substrs = [];
|
|
353
|
+
this.start = 0;
|
|
354
|
+
this.end = 0;
|
|
355
|
+
}
|
|
356
|
+
add(part) {
|
|
357
|
+
if (part) {
|
|
358
|
+
this.parts.push(part);
|
|
359
|
+
this.substrs.push(part.substr);
|
|
360
|
+
this.start = Math.min(part.start, this.start);
|
|
361
|
+
this.end = Math.max(part.end, this.end);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
last() {
|
|
365
|
+
return this.parts[this.parts.length - 1];
|
|
366
|
+
}
|
|
367
|
+
length() {
|
|
368
|
+
return this.parts.length;
|
|
369
|
+
}
|
|
370
|
+
clone(position, last_piece) {
|
|
371
|
+
let clone = new Sequence();
|
|
372
|
+
let parts = JSON.parse(JSON.stringify(this.parts));
|
|
373
|
+
let last_part = parts.pop();
|
|
374
|
+
for (const part of parts) {
|
|
375
|
+
clone.add(part);
|
|
376
|
+
}
|
|
377
|
+
let last_substr = last_piece.substr.substring(0, position - last_part.start);
|
|
378
|
+
let clone_last_len = last_substr.length;
|
|
379
|
+
clone.add({
|
|
380
|
+
start: last_part.start,
|
|
381
|
+
end: last_part.start + clone_last_len,
|
|
382
|
+
length: clone_last_len,
|
|
383
|
+
substr: last_substr
|
|
384
|
+
});
|
|
385
|
+
return clone;
|
|
386
|
+
}
|
|
568
387
|
}
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
388
|
+
/**
|
|
389
|
+
* Expand a regular expression pattern to include unicode variants
|
|
390
|
+
* eg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/
|
|
391
|
+
*
|
|
392
|
+
* Issue:
|
|
393
|
+
* ﺊﺋ [ 'ﺊ = \\u{fe8a}', 'ﺋ = \\u{fe8b}' ]
|
|
394
|
+
* becomes: ئئ [ 'ي = \\u{64a}', 'ٔ = \\u{654}', 'ي = \\u{64a}', 'ٔ = \\u{654}' ]
|
|
395
|
+
*
|
|
396
|
+
* İIJ = IIJ = ⅡJ
|
|
397
|
+
*
|
|
398
|
+
* 1/2/4
|
|
399
|
+
*/
|
|
400
|
+
const getPattern = str => {
|
|
401
|
+
initialize();
|
|
402
|
+
str = asciifold(str);
|
|
403
|
+
let pattern = '';
|
|
404
|
+
let sequences = [new Sequence()];
|
|
405
|
+
for (let i = 0; i < str.length; i++) {
|
|
406
|
+
let substr = str.substring(i);
|
|
407
|
+
let match = substr.match(multi_char_reg);
|
|
408
|
+
const char = str.substring(i, i + 1);
|
|
409
|
+
const match_str = match ? match[0] : null;
|
|
410
|
+
// loop through sequences
|
|
411
|
+
// add either the char or multi_match
|
|
412
|
+
let overlapping = [];
|
|
413
|
+
let added_types = new Set();
|
|
414
|
+
for (const sequence of sequences) {
|
|
415
|
+
const last_piece = sequence.last();
|
|
416
|
+
if (!last_piece || last_piece.length == 1 || last_piece.end <= i) {
|
|
417
|
+
// if we have a multi match
|
|
418
|
+
if (match_str) {
|
|
419
|
+
const len = match_str.length;
|
|
420
|
+
sequence.add({
|
|
421
|
+
start: i,
|
|
422
|
+
end: i + len,
|
|
423
|
+
length: len,
|
|
424
|
+
substr: match_str
|
|
425
|
+
});
|
|
426
|
+
added_types.add('1');
|
|
427
|
+
} else {
|
|
428
|
+
sequence.add({
|
|
429
|
+
start: i,
|
|
430
|
+
end: i + 1,
|
|
431
|
+
length: 1,
|
|
432
|
+
substr: char
|
|
433
|
+
});
|
|
434
|
+
added_types.add('2');
|
|
435
|
+
}
|
|
436
|
+
} else if (match_str) {
|
|
437
|
+
let clone = sequence.clone(i, last_piece);
|
|
610
438
|
const len = match_str.length;
|
|
611
|
-
|
|
439
|
+
clone.add({
|
|
612
440
|
start: i,
|
|
613
441
|
end: i + len,
|
|
614
442
|
length: len,
|
|
615
443
|
substr: match_str
|
|
616
444
|
});
|
|
617
|
-
|
|
445
|
+
overlapping.push(clone);
|
|
618
446
|
} else {
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
length: 1,
|
|
623
|
-
substr: char
|
|
624
|
-
});
|
|
625
|
-
added_types.add('2');
|
|
447
|
+
// don't add char
|
|
448
|
+
// adding would create invalid patterns: 234 => [2,34,4]
|
|
449
|
+
added_types.add('3');
|
|
626
450
|
}
|
|
627
|
-
} else if (match_str) {
|
|
628
|
-
let clone = sequence.clone(i, last_piece);
|
|
629
|
-
const len = match_str.length;
|
|
630
|
-
clone.add({
|
|
631
|
-
start: i,
|
|
632
|
-
end: i + len,
|
|
633
|
-
length: len,
|
|
634
|
-
substr: match_str
|
|
635
|
-
});
|
|
636
|
-
overlapping.push(clone);
|
|
637
|
-
} else {
|
|
638
|
-
// don't add char
|
|
639
|
-
// adding would create invalid patterns: 234 => [2,34,4]
|
|
640
|
-
added_types.add('3');
|
|
641
451
|
}
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
continue;
|
|
452
|
+
// if we have overlapping
|
|
453
|
+
if (overlapping.length > 0) {
|
|
454
|
+
// ['ii','iii'] before ['i','i','iii']
|
|
455
|
+
overlapping = overlapping.sort((a, b) => {
|
|
456
|
+
return a.length() - b.length();
|
|
457
|
+
});
|
|
458
|
+
for (let clone of overlapping) {
|
|
459
|
+
// don't add if we already have an equivalent sequence
|
|
460
|
+
if (inSequences(clone, sequences)) {
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
sequences.push(clone);
|
|
655
464
|
}
|
|
656
|
-
|
|
657
|
-
sequences.push(clone);
|
|
465
|
+
continue;
|
|
658
466
|
}
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
if (old_seq) {
|
|
673
|
-
new_seq.add(old_seq.last());
|
|
467
|
+
// if we haven't done anything unique
|
|
468
|
+
// clean up the patterns
|
|
469
|
+
// helps keep patterns smaller
|
|
470
|
+
// if str = 'r₨㎧aarss', pattern will be 446 instead of 655
|
|
471
|
+
if (i > 0 && added_types.size == 1 && !added_types.has('3')) {
|
|
472
|
+
pattern += sequencesToPattern(sequences, false);
|
|
473
|
+
let new_seq = new Sequence();
|
|
474
|
+
const old_seq = sequences[0];
|
|
475
|
+
if (old_seq) {
|
|
476
|
+
new_seq.add(old_seq.last());
|
|
477
|
+
}
|
|
478
|
+
sequences = [new_seq];
|
|
674
479
|
}
|
|
675
|
-
|
|
676
|
-
sequences = [new_seq];
|
|
677
480
|
}
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
return pattern;
|
|
682
|
-
};
|
|
481
|
+
pattern += sequencesToPattern(sequences, true);
|
|
482
|
+
return pattern;
|
|
483
|
+
};
|
|
683
484
|
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
/**
|
|
695
|
-
* A property getter resolving dot-notation
|
|
696
|
-
* @param {Object} obj The root object to fetch property on
|
|
697
|
-
* @param {String} name The optionally dotted property name to fetch
|
|
698
|
-
* @return {Object} The resolved property value
|
|
699
|
-
*/
|
|
485
|
+
/**
|
|
486
|
+
* A property getter resolving dot-notation
|
|
487
|
+
* @param {Object} obj The root object to fetch property on
|
|
488
|
+
* @param {String} name The optionally dotted property name to fetch
|
|
489
|
+
* @return {Object} The resolved property value
|
|
490
|
+
*/
|
|
491
|
+
const getAttr = (obj, name) => {
|
|
492
|
+
if (!obj) return;
|
|
493
|
+
return obj[name];
|
|
494
|
+
};
|
|
700
495
|
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
496
|
+
/**
|
|
497
|
+
* A property getter resolving dot-notation
|
|
498
|
+
* @param {Object} obj The root object to fetch property on
|
|
499
|
+
* @param {String} name The optionally dotted property name to fetch
|
|
500
|
+
* @return {Object} The resolved property value
|
|
501
|
+
*/
|
|
502
|
+
const getAttrNesting = (obj, name) => {
|
|
503
|
+
if (!obj) return;
|
|
504
|
+
var part,
|
|
704
505
|
names = name.split(".");
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
return obj;
|
|
709
|
-
};
|
|
710
|
-
/**
|
|
711
|
-
* Calculates how close of a match the
|
|
712
|
-
* given value is against a search token.
|
|
713
|
-
*
|
|
714
|
-
*/
|
|
715
|
-
|
|
716
|
-
const scoreValue = (value, token, weight) => {
|
|
717
|
-
var score, pos;
|
|
718
|
-
if (!value) return 0;
|
|
719
|
-
value = value + '';
|
|
720
|
-
if (token.regex == null) return 0;
|
|
721
|
-
pos = value.search(token.regex);
|
|
722
|
-
if (pos === -1) return 0;
|
|
723
|
-
score = token.string.length / value.length;
|
|
724
|
-
if (pos === 0) score += 0.5;
|
|
725
|
-
return score * weight;
|
|
726
|
-
};
|
|
727
|
-
/**
|
|
728
|
-
* Cast object property to an array if it exists and has a value
|
|
729
|
-
*
|
|
730
|
-
*/
|
|
731
|
-
|
|
732
|
-
const propToArray = (obj, key) => {
|
|
733
|
-
var value = obj[key];
|
|
734
|
-
if (typeof value == 'function') return value;
|
|
735
|
-
|
|
736
|
-
if (value && !Array.isArray(value)) {
|
|
737
|
-
obj[key] = [value];
|
|
738
|
-
}
|
|
739
|
-
};
|
|
740
|
-
/**
|
|
741
|
-
* Iterates over arrays and hashes.
|
|
742
|
-
*
|
|
743
|
-
* ```
|
|
744
|
-
* iterate(this.items, function(item, id) {
|
|
745
|
-
* // invoked for each item
|
|
746
|
-
* });
|
|
747
|
-
* ```
|
|
748
|
-
*
|
|
749
|
-
*/
|
|
750
|
-
|
|
751
|
-
const iterate = (object, callback) => {
|
|
752
|
-
if (Array.isArray(object)) {
|
|
753
|
-
object.forEach(callback);
|
|
754
|
-
} else {
|
|
755
|
-
for (var key in object) {
|
|
756
|
-
if (object.hasOwnProperty(key)) {
|
|
757
|
-
callback(object[key], key);
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
-
}
|
|
761
|
-
};
|
|
762
|
-
const cmp = (a, b) => {
|
|
763
|
-
if (typeof a === 'number' && typeof b === 'number') {
|
|
764
|
-
return a > b ? 1 : a < b ? -1 : 0;
|
|
765
|
-
}
|
|
766
|
-
|
|
767
|
-
a = asciifold(a + '').toLowerCase();
|
|
768
|
-
b = asciifold(b + '').toLowerCase();
|
|
769
|
-
if (a > b) return 1;
|
|
770
|
-
if (b > a) return -1;
|
|
771
|
-
return 0;
|
|
772
|
-
};
|
|
773
|
-
|
|
774
|
-
/**
|
|
775
|
-
* sifter.js
|
|
776
|
-
* Copyright (c) 2013–2020 Brian Reavis & contributors
|
|
777
|
-
*
|
|
778
|
-
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
|
|
779
|
-
* file except in compliance with the License. You may obtain a copy of the License at:
|
|
780
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
781
|
-
*
|
|
782
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
783
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
|
784
|
-
* ANY KIND, either express or implied. See the License for the specific language
|
|
785
|
-
* governing permissions and limitations under the License.
|
|
786
|
-
*
|
|
787
|
-
* @author Brian Reavis <brian@thirdroute.com>
|
|
788
|
-
*/
|
|
789
|
-
|
|
790
|
-
class Sifter {
|
|
791
|
-
// []|{};
|
|
506
|
+
while ((part = names.shift()) && (obj = obj[part]));
|
|
507
|
+
return obj;
|
|
508
|
+
};
|
|
792
509
|
|
|
793
510
|
/**
|
|
794
|
-
*
|
|
795
|
-
*
|
|
796
|
-
* specifically for autocomplete.
|
|
511
|
+
* Calculates how close of a match the
|
|
512
|
+
* given value is against a search token.
|
|
797
513
|
*
|
|
798
514
|
*/
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
515
|
+
const scoreValue = (value, token, weight) => {
|
|
516
|
+
var score, pos;
|
|
517
|
+
if (!value) return 0;
|
|
518
|
+
value = value + '';
|
|
519
|
+
if (token.regex == null) return 0;
|
|
520
|
+
pos = value.search(token.regex);
|
|
521
|
+
if (pos === -1) return 0;
|
|
522
|
+
score = token.string.length / value.length;
|
|
523
|
+
if (pos === 0) score += 0.5;
|
|
524
|
+
return score * weight;
|
|
525
|
+
};
|
|
807
526
|
|
|
808
527
|
/**
|
|
809
|
-
*
|
|
810
|
-
* regexps to be used to match results.
|
|
528
|
+
* Cast object property to an array if it exists and has a value
|
|
811
529
|
*
|
|
812
530
|
*/
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
if (weights) {
|
|
820
|
-
field_regex = new RegExp('^(' + Object.keys(weights).map(escape_regex).join('|') + ')\:(.*)$');
|
|
531
|
+
const propToArray = (obj, key) => {
|
|
532
|
+
var value = obj[key];
|
|
533
|
+
if (typeof value == 'function') return value;
|
|
534
|
+
if (value && !Array.isArray(value)) {
|
|
535
|
+
obj[key] = [value];
|
|
821
536
|
}
|
|
537
|
+
};
|
|
822
538
|
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
539
|
+
/**
|
|
540
|
+
* Iterates over arrays and hashes.
|
|
541
|
+
*
|
|
542
|
+
* ```
|
|
543
|
+
* iterate(this.items, function(item, id) {
|
|
544
|
+
* // invoked for each item
|
|
545
|
+
* });
|
|
546
|
+
* ```
|
|
547
|
+
*
|
|
548
|
+
*/
|
|
549
|
+
const iterate = (object, callback) => {
|
|
550
|
+
if (Array.isArray(object)) {
|
|
551
|
+
object.forEach(callback);
|
|
552
|
+
} else {
|
|
553
|
+
for (var key in object) {
|
|
554
|
+
if (object.hasOwnProperty(key)) {
|
|
555
|
+
callback(object[key], key);
|
|
838
556
|
}
|
|
839
|
-
|
|
840
|
-
if (regex && respect_word_boundaries) regex = "\\b" + regex;
|
|
841
557
|
}
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
const cmp = (a, b) => {
|
|
561
|
+
if (typeof a === 'number' && typeof b === 'number') {
|
|
562
|
+
return a > b ? 1 : a < b ? -1 : 0;
|
|
563
|
+
}
|
|
564
|
+
a = asciifold(a + '').toLowerCase();
|
|
565
|
+
b = asciifold(b + '').toLowerCase();
|
|
566
|
+
if (a > b) return 1;
|
|
567
|
+
if (b > a) return -1;
|
|
568
|
+
return 0;
|
|
569
|
+
};
|
|
851
570
|
|
|
852
571
|
/**
|
|
853
|
-
*
|
|
572
|
+
* sifter.js
|
|
573
|
+
* Copyright (c) 2013–2020 Brian Reavis & contributors
|
|
854
574
|
*
|
|
855
|
-
*
|
|
856
|
-
*
|
|
575
|
+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
|
|
576
|
+
* file except in compliance with the License. You may obtain a copy of the License at:
|
|
577
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
857
578
|
*
|
|
858
|
-
*
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
return this._getScoreFunction(search);
|
|
863
|
-
}
|
|
864
|
-
/**
|
|
865
|
-
* @returns {T.ScoreFn}
|
|
579
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
580
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
|
581
|
+
* ANY KIND, either express or implied. See the License for the specific language
|
|
582
|
+
* governing permissions and limitations under the License.
|
|
866
583
|
*
|
|
584
|
+
* @author Brian Reavis <brian@thirdroute.com>
|
|
867
585
|
*/
|
|
868
586
|
|
|
587
|
+
class Sifter {
|
|
588
|
+
// []|{};
|
|
869
589
|
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
const fields = search.options.fields,
|
|
881
|
-
weights = search.weights,
|
|
882
|
-
field_count = fields.length,
|
|
883
|
-
getAttrFn = search.getAttrFn;
|
|
884
|
-
|
|
885
|
-
if (!field_count) {
|
|
886
|
-
return function () {
|
|
887
|
-
return 1;
|
|
590
|
+
/**
|
|
591
|
+
* Textually searches arrays and hashes of objects
|
|
592
|
+
* by property (or multiple properties). Designed
|
|
593
|
+
* specifically for autocomplete.
|
|
594
|
+
*
|
|
595
|
+
*/
|
|
596
|
+
constructor(items, settings) {
|
|
597
|
+
this.items = items;
|
|
598
|
+
this.settings = settings || {
|
|
599
|
+
diacritics: true
|
|
888
600
|
};
|
|
889
601
|
}
|
|
890
602
|
/**
|
|
891
|
-
*
|
|
892
|
-
*
|
|
603
|
+
* Splits a search string into an array of individual
|
|
604
|
+
* regexps to be used to match results.
|
|
893
605
|
*
|
|
894
606
|
*/
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
};
|
|
607
|
+
tokenize(query, respect_word_boundaries, weights) {
|
|
608
|
+
if (!query || !query.length) return [];
|
|
609
|
+
const tokens = [];
|
|
610
|
+
const words = query.split(/\s+/);
|
|
611
|
+
var field_regex;
|
|
612
|
+
if (weights) {
|
|
613
|
+
field_regex = new RegExp('^(' + Object.keys(weights).map(escape_regex).join('|') + ')\:(.*)$');
|
|
903
614
|
}
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
615
|
+
words.forEach(word => {
|
|
616
|
+
let field_match;
|
|
617
|
+
let field = null;
|
|
618
|
+
let regex = null;
|
|
619
|
+
|
|
620
|
+
// look for "field:query" tokens
|
|
621
|
+
if (field_regex && (field_match = word.match(field_regex))) {
|
|
622
|
+
field = field_match[1];
|
|
623
|
+
word = field_match[2];
|
|
624
|
+
}
|
|
625
|
+
if (word.length > 0) {
|
|
626
|
+
if (this.settings.diacritics) {
|
|
627
|
+
regex = getPattern(word) || null;
|
|
913
628
|
} else {
|
|
914
|
-
|
|
629
|
+
regex = escape_regex(word);
|
|
915
630
|
}
|
|
916
|
-
|
|
917
|
-
iterate(weights, (weight, field) => {
|
|
918
|
-
sum += scoreValue(getAttrFn(data, field), token, weight);
|
|
919
|
-
});
|
|
920
|
-
}
|
|
921
|
-
|
|
922
|
-
return sum / field_count;
|
|
923
|
-
};
|
|
924
|
-
}();
|
|
925
|
-
|
|
926
|
-
if (token_count === 1) {
|
|
927
|
-
return function (data) {
|
|
928
|
-
return scoreObject(tokens[0], data);
|
|
929
|
-
};
|
|
930
|
-
}
|
|
931
|
-
|
|
932
|
-
if (search.options.conjunction === 'and') {
|
|
933
|
-
return function (data) {
|
|
934
|
-
var score,
|
|
935
|
-
sum = 0;
|
|
936
|
-
|
|
937
|
-
for (let token of tokens) {
|
|
938
|
-
score = scoreObject(token, data);
|
|
939
|
-
if (score <= 0) return 0;
|
|
940
|
-
sum += score;
|
|
631
|
+
if (regex && respect_word_boundaries) regex = "\\b" + regex;
|
|
941
632
|
}
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
return function (data) {
|
|
947
|
-
var sum = 0;
|
|
948
|
-
iterate(tokens, token => {
|
|
949
|
-
sum += scoreObject(token, data);
|
|
633
|
+
tokens.push({
|
|
634
|
+
string: word,
|
|
635
|
+
regex: regex ? new RegExp(regex, 'iu') : null,
|
|
636
|
+
field: field
|
|
950
637
|
});
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
}
|
|
954
|
-
}
|
|
955
|
-
|
|
956
|
-
/**
|
|
957
|
-
* Returns a function that can be used to compare two
|
|
958
|
-
* results, for sorting purposes. If no sorting should
|
|
959
|
-
* be performed, `null` will be returned.
|
|
960
|
-
*
|
|
961
|
-
* @return function(a,b)
|
|
962
|
-
*/
|
|
963
|
-
getSortFunction(query, options) {
|
|
964
|
-
var search = this.prepareSearch(query, options);
|
|
965
|
-
return this._getSortFunction(search);
|
|
966
|
-
}
|
|
967
|
-
|
|
968
|
-
_getSortFunction(search) {
|
|
969
|
-
var implicit_score,
|
|
970
|
-
sort_flds = [];
|
|
971
|
-
const self = this,
|
|
972
|
-
options = search.options,
|
|
973
|
-
sort = !search.query && options.sort_empty ? options.sort_empty : options.sort;
|
|
974
|
-
|
|
975
|
-
if (typeof sort == 'function') {
|
|
976
|
-
return sort.bind(this);
|
|
638
|
+
});
|
|
639
|
+
return tokens;
|
|
977
640
|
}
|
|
978
641
|
/**
|
|
979
|
-
*
|
|
980
|
-
* from a search result item.
|
|
642
|
+
* Returns a function to be used to score individual results.
|
|
981
643
|
*
|
|
644
|
+
* Good matches will have a higher score than poor matches.
|
|
645
|
+
* If an item is not a match, 0 will be returned by the function.
|
|
646
|
+
*
|
|
647
|
+
* @returns {T.ScoreFn}
|
|
982
648
|
*/
|
|
649
|
+
getScoreFunction(query, options) {
|
|
650
|
+
var search = this.prepareSearch(query, options);
|
|
651
|
+
return this._getScoreFunction(search);
|
|
652
|
+
}
|
|
983
653
|
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
654
|
+
/**
|
|
655
|
+
* @returns {T.ScoreFn}
|
|
656
|
+
*
|
|
657
|
+
*/
|
|
658
|
+
_getScoreFunction(search) {
|
|
659
|
+
const tokens = search.tokens,
|
|
660
|
+
token_count = tokens.length;
|
|
661
|
+
if (!token_count) {
|
|
662
|
+
return function () {
|
|
663
|
+
return 0;
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
const fields = search.options.fields,
|
|
667
|
+
weights = search.weights,
|
|
668
|
+
field_count = fields.length,
|
|
669
|
+
getAttrFn = search.getAttrFn;
|
|
670
|
+
if (!field_count) {
|
|
671
|
+
return function () {
|
|
672
|
+
return 1;
|
|
673
|
+
};
|
|
996
674
|
}
|
|
997
|
-
} // the "$score" field is implied to be the primary
|
|
998
|
-
// sort field, unless it's manually specified
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
if (search.query) {
|
|
1002
|
-
implicit_score = true;
|
|
1003
675
|
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
676
|
+
/**
|
|
677
|
+
* Calculates the score of an object
|
|
678
|
+
* against the search query.
|
|
679
|
+
*
|
|
680
|
+
*/
|
|
681
|
+
const scoreObject = function () {
|
|
682
|
+
if (field_count === 1) {
|
|
683
|
+
return function (token, data) {
|
|
684
|
+
const field = fields[0].field;
|
|
685
|
+
return scoreValue(getAttrFn(data, field), token, weights[field] || 1);
|
|
686
|
+
};
|
|
1008
687
|
}
|
|
688
|
+
return function (token, data) {
|
|
689
|
+
var sum = 0;
|
|
690
|
+
|
|
691
|
+
// is the token specific to a field?
|
|
692
|
+
if (token.field) {
|
|
693
|
+
const value = getAttrFn(data, token.field);
|
|
694
|
+
if (!token.regex && value) {
|
|
695
|
+
sum += 1 / field_count;
|
|
696
|
+
} else {
|
|
697
|
+
sum += scoreValue(value, token, 1);
|
|
698
|
+
}
|
|
699
|
+
} else {
|
|
700
|
+
iterate(weights, (weight, field) => {
|
|
701
|
+
sum += scoreValue(getAttrFn(data, field), token, weight);
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
return sum / field_count;
|
|
705
|
+
};
|
|
706
|
+
}();
|
|
707
|
+
if (token_count === 1) {
|
|
708
|
+
return function (data) {
|
|
709
|
+
return scoreObject(tokens[0], data);
|
|
710
|
+
};
|
|
711
|
+
}
|
|
712
|
+
if (search.options.conjunction === 'and') {
|
|
713
|
+
return function (data) {
|
|
714
|
+
var score,
|
|
715
|
+
sum = 0;
|
|
716
|
+
for (let token of tokens) {
|
|
717
|
+
score = scoreObject(token, data);
|
|
718
|
+
if (score <= 0) return 0;
|
|
719
|
+
sum += score;
|
|
720
|
+
}
|
|
721
|
+
return sum / token_count;
|
|
722
|
+
};
|
|
723
|
+
} else {
|
|
724
|
+
return function (data) {
|
|
725
|
+
var sum = 0;
|
|
726
|
+
iterate(tokens, token => {
|
|
727
|
+
sum += scoreObject(token, data);
|
|
728
|
+
});
|
|
729
|
+
return sum / token_count;
|
|
730
|
+
};
|
|
1009
731
|
}
|
|
1010
|
-
|
|
1011
|
-
if (implicit_score) {
|
|
1012
|
-
sort_flds.unshift({
|
|
1013
|
-
field: '$score',
|
|
1014
|
-
direction: 'desc'
|
|
1015
|
-
});
|
|
1016
|
-
} // without a search.query, all items will have the same score
|
|
1017
|
-
|
|
1018
|
-
} else {
|
|
1019
|
-
sort_flds = sort_flds.filter(fld => fld.field !== '$score');
|
|
1020
|
-
} // build function
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
const sort_flds_count = sort_flds.length;
|
|
1024
|
-
|
|
1025
|
-
if (!sort_flds_count) {
|
|
1026
|
-
return null;
|
|
1027
732
|
}
|
|
733
|
+
/**
|
|
734
|
+
* Returns a function that can be used to compare two
|
|
735
|
+
* results, for sorting purposes. If no sorting should
|
|
736
|
+
* be performed, `null` will be returned.
|
|
737
|
+
*
|
|
738
|
+
* @return function(a,b)
|
|
739
|
+
*/
|
|
740
|
+
getSortFunction(query, options) {
|
|
741
|
+
var search = this.prepareSearch(query, options);
|
|
742
|
+
return this._getSortFunction(search);
|
|
743
|
+
}
|
|
744
|
+
_getSortFunction(search) {
|
|
745
|
+
var implicit_score,
|
|
746
|
+
sort_flds = [];
|
|
747
|
+
const self = this,
|
|
748
|
+
options = search.options,
|
|
749
|
+
sort = !search.query && options.sort_empty ? options.sort_empty : options.sort;
|
|
750
|
+
if (typeof sort == 'function') {
|
|
751
|
+
return sort.bind(this);
|
|
752
|
+
}
|
|
1028
753
|
|
|
1029
|
-
|
|
1030
|
-
|
|
754
|
+
/**
|
|
755
|
+
* Fetches the specified sort field value
|
|
756
|
+
* from a search result item.
|
|
757
|
+
*
|
|
758
|
+
*/
|
|
759
|
+
const get_field = function get_field(name, result) {
|
|
760
|
+
if (name === '$score') return result.score;
|
|
761
|
+
return search.getAttrFn(self.items[result.id], name);
|
|
762
|
+
};
|
|
1031
763
|
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
let
|
|
1035
|
-
|
|
1036
|
-
|
|
764
|
+
// parse options
|
|
765
|
+
if (sort) {
|
|
766
|
+
for (let s of sort) {
|
|
767
|
+
if (search.query || s.field !== '$score') {
|
|
768
|
+
sort_flds.push(s);
|
|
769
|
+
}
|
|
770
|
+
}
|
|
1037
771
|
}
|
|
1038
772
|
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
773
|
+
// the "$score" field is implied to be the primary
|
|
774
|
+
// sort field, unless it's manually specified
|
|
775
|
+
if (search.query) {
|
|
776
|
+
implicit_score = true;
|
|
777
|
+
for (let fld of sort_flds) {
|
|
778
|
+
if (fld.field === '$score') {
|
|
779
|
+
implicit_score = false;
|
|
780
|
+
break;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
if (implicit_score) {
|
|
784
|
+
sort_flds.unshift({
|
|
785
|
+
field: '$score',
|
|
786
|
+
direction: 'desc'
|
|
787
|
+
});
|
|
788
|
+
}
|
|
1042
789
|
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
*
|
|
1048
|
-
*/
|
|
1049
|
-
prepareSearch(query, optsUser) {
|
|
1050
|
-
const weights = {};
|
|
1051
|
-
var options = Object.assign({}, optsUser);
|
|
1052
|
-
propToArray(options, 'sort');
|
|
1053
|
-
propToArray(options, 'sort_empty'); // convert fields to new format
|
|
790
|
+
// without a search.query, all items will have the same score
|
|
791
|
+
} else {
|
|
792
|
+
sort_flds = sort_flds.filter(fld => fld.field !== '$score');
|
|
793
|
+
}
|
|
1054
794
|
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
795
|
+
// build function
|
|
796
|
+
const sort_flds_count = sort_flds.length;
|
|
797
|
+
if (!sort_flds_count) {
|
|
798
|
+
return null;
|
|
799
|
+
}
|
|
800
|
+
return function (a, b) {
|
|
801
|
+
var result, field;
|
|
802
|
+
for (let sort_fld of sort_flds) {
|
|
803
|
+
field = sort_fld.field;
|
|
804
|
+
let multiplier = sort_fld.direction === 'desc' ? -1 : 1;
|
|
805
|
+
result = multiplier * cmp(get_field(field, a), get_field(field, b));
|
|
806
|
+
if (result) return result;
|
|
1064
807
|
}
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
weights[field.field] = 'weight' in field ? field.weight : 1;
|
|
1068
|
-
});
|
|
1069
|
-
options.fields = fields;
|
|
808
|
+
return 0;
|
|
809
|
+
};
|
|
1070
810
|
}
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
weights
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
811
|
+
/**
|
|
812
|
+
* Parses a search query and returns an object
|
|
813
|
+
* with tokens and fields ready to be populated
|
|
814
|
+
* with results.
|
|
815
|
+
*
|
|
816
|
+
*/
|
|
817
|
+
prepareSearch(query, optsUser) {
|
|
818
|
+
const weights = {};
|
|
819
|
+
var options = Object.assign({}, optsUser);
|
|
820
|
+
propToArray(options, 'sort');
|
|
821
|
+
propToArray(options, 'sort_empty');
|
|
822
|
+
|
|
823
|
+
// convert fields to new format
|
|
824
|
+
if (options.fields) {
|
|
825
|
+
propToArray(options, 'fields');
|
|
826
|
+
const fields = [];
|
|
827
|
+
options.fields.forEach(field => {
|
|
828
|
+
if (typeof field == 'string') {
|
|
829
|
+
field = {
|
|
830
|
+
field: field,
|
|
831
|
+
weight: 1
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
fields.push(field);
|
|
835
|
+
weights[field.field] = 'weight' in field ? field.weight : 1;
|
|
836
|
+
});
|
|
837
|
+
options.fields = fields;
|
|
838
|
+
}
|
|
839
|
+
return {
|
|
840
|
+
options: options,
|
|
841
|
+
query: query.toLowerCase().trim(),
|
|
842
|
+
tokens: this.tokenize(query, options.respect_word_boundaries, weights),
|
|
843
|
+
total: 0,
|
|
844
|
+
items: [],
|
|
845
|
+
weights: weights,
|
|
846
|
+
getAttrFn: options.nesting ? getAttrNesting : getAttr
|
|
847
|
+
};
|
|
848
|
+
}
|
|
849
|
+
/**
|
|
850
|
+
* Searches through all items and returns a sorted array of matches.
|
|
851
|
+
*
|
|
852
|
+
*/
|
|
853
|
+
search(query, options) {
|
|
854
|
+
var self = this,
|
|
1089
855
|
score,
|
|
1090
856
|
search;
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
857
|
+
search = this.prepareSearch(query, options);
|
|
858
|
+
options = search.options;
|
|
859
|
+
query = search.query;
|
|
860
|
+
|
|
861
|
+
// generate result scoring function
|
|
862
|
+
const fn_score = options.score || self._getScoreFunction(search);
|
|
863
|
+
|
|
864
|
+
// perform search and sort
|
|
865
|
+
if (query.length) {
|
|
866
|
+
iterate(self.items, (item, id) => {
|
|
867
|
+
score = fn_score(item);
|
|
868
|
+
if (options.filter === false || score > 0) {
|
|
869
|
+
search.items.push({
|
|
870
|
+
'score': score,
|
|
871
|
+
'id': id
|
|
872
|
+
});
|
|
873
|
+
}
|
|
874
|
+
});
|
|
875
|
+
} else {
|
|
876
|
+
iterate(self.items, (_, id) => {
|
|
1103
877
|
search.items.push({
|
|
1104
|
-
'score':
|
|
878
|
+
'score': 1,
|
|
1105
879
|
'id': id
|
|
1106
880
|
});
|
|
1107
|
-
}
|
|
1108
|
-
});
|
|
1109
|
-
} else {
|
|
1110
|
-
iterate(self.items, (_, id) => {
|
|
1111
|
-
search.items.push({
|
|
1112
|
-
'score': 1,
|
|
1113
|
-
'id': id
|
|
1114
881
|
});
|
|
1115
|
-
}
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
const fn_sort = self._getSortFunction(search);
|
|
1119
|
-
|
|
1120
|
-
if (fn_sort) search.items.sort(fn_sort); // apply limits
|
|
1121
|
-
|
|
1122
|
-
search.total = search.items.length;
|
|
882
|
+
}
|
|
883
|
+
const fn_sort = self._getSortFunction(search);
|
|
884
|
+
if (fn_sort) search.items.sort(fn_sort);
|
|
1123
885
|
|
|
1124
|
-
|
|
1125
|
-
search.
|
|
886
|
+
// apply limits
|
|
887
|
+
search.total = search.items.length;
|
|
888
|
+
if (typeof options.limit === 'number') {
|
|
889
|
+
search.items = search.items.slice(0, options.limit);
|
|
890
|
+
}
|
|
891
|
+
return search;
|
|
1126
892
|
}
|
|
1127
|
-
|
|
1128
|
-
return search;
|
|
1129
893
|
}
|
|
1130
894
|
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
exports.propToArray = propToArray;
|
|
1140
|
-
exports.scoreValue = scoreValue;
|
|
1141
|
-
|
|
1142
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
895
|
+
exports.Sifter = Sifter;
|
|
896
|
+
exports.cmp = cmp;
|
|
897
|
+
exports.getAttr = getAttr;
|
|
898
|
+
exports.getAttrNesting = getAttrNesting;
|
|
899
|
+
exports.getPattern = getPattern;
|
|
900
|
+
exports.iterate = iterate;
|
|
901
|
+
exports.propToArray = propToArray;
|
|
902
|
+
exports.scoreValue = scoreValue;
|
|
1143
903
|
|
|
1144
|
-
}))
|
|
904
|
+
}));
|
|
1145
905
|
//# sourceMappingURL=sifter.js.map
|