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