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