@orchidjs/sifter 0.8.1 → 0.9.1
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 +25 -3
- package/dist/cjs/diacritics.js +19 -3
- package/dist/cjs/diacritics.js.map +1 -1
- package/dist/cjs/sifter.js +6 -1
- package/dist/cjs/sifter.js.map +1 -1
- package/dist/cjs/utils.js +1 -0
- package/dist/cjs/utils.js.map +1 -1
- package/dist/esm/diacritics.js +19 -3
- package/dist/esm/diacritics.js.map +1 -1
- package/dist/esm/sifter.js +6 -1
- package/dist/esm/sifter.js.map +1 -1
- package/dist/esm/utils.js +1 -0
- package/dist/esm/utils.js.map +1 -1
- package/dist/types/diacritics.d.ts +1 -0
- package/dist/types/sifter.d.ts +3 -3
- package/dist/types/types.d.ts +10 -8
- package/dist/types/utils.d.ts +1 -1
- package/dist/umd/sifter.js +26 -4
- package/dist/umd/sifter.js.map +1 -1
- package/dist/umd/sifter.min.js +11 -4
- package/dist/umd/sifter.min.js.map +1 -1
- package/lib/diacritics.ts +22 -6
- package/lib/sifter.ts +6 -2
- package/lib/types.ts +12 -8
- package/lib/utils.ts +3 -0
- package/package.json +6 -2
- package/.config/babel.config.json +0 -17
- package/.config/rollup.config.js +0 -95
- package/.config/tsconfig.json +0 -16
- package/.travis.yml +0 -9
- package/Makefile +0 -27
- package/build/cjs/diacritics.js +0 -157
- package/build/cjs/diacritics.js.map +0 -1
- package/build/cjs/sifter.js +0 -372
- package/build/cjs/sifter.js.map +0 -1
- package/build/cjs/utils.js +0 -113
- package/build/cjs/utils.js.map +0 -1
- package/build/esm/diacritics.js +0 -148
- package/build/esm/diacritics.js.map +0 -1
- package/build/esm/sifter.js +0 -370
- package/build/esm/sifter.js.map +0 -1
- package/build/esm/utils.js +0 -103
- package/build/esm/utils.js.map +0 -1
- package/build/umd/sifter.js +0 -618
- package/build/umd/sifter.js.map +0 -1
- package/build/umd/sifter.min.js +0 -77
- package/build/umd/sifter.min.js.map +0 -1
- package/coverage/clover.xml +0 -251
- package/coverage/coverage-final.json +0 -4
- package/coverage/lcov-report/base.css +0 -224
- package/coverage/lcov-report/block-navigation.js +0 -79
- package/coverage/lcov-report/diacritics.ts.html +0 -1085
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +0 -141
- package/coverage/lcov-report/prettify.css +0 -1
- package/coverage/lcov-report/prettify.js +0 -2
- package/coverage/lcov-report/sifter.ts.html +0 -1157
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +0 -170
- package/coverage/lcov-report/utils.ts.html +0 -419
- package/coverage/lcov.info +0 -492
- package/release.sh +0 -119
- package/test/diacritics.js +0 -64
- package/test/getScoreFunction.js +0 -55
- package/test/prepareSearch.js +0 -40
- package/test/search.js +0 -335
- package/test/sorting.js +0 -193
- package/test/tokenize.js +0 -63
- package/test/utils.js +0 -28
- package/x-test.js +0 -129
package/build/umd/sifter.js
DELETED
|
@@ -1,618 +0,0 @@
|
|
|
1
|
-
/*! sifter.js | https://github.com/orchidjs/sifter.js | Apache License (v2) */
|
|
2
|
-
(function (global, factory) {
|
|
3
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
4
|
-
typeof define === 'function' && define.amd ? define(factory) :
|
|
5
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.sifter = factory());
|
|
6
|
-
}(this, (function () { 'use strict';
|
|
7
|
-
|
|
8
|
-
// https://github.com/andrewrk/node-diacritics/blob/master/index.js
|
|
9
|
-
var latin_pat;
|
|
10
|
-
const accent_pat = '[\u0300-\u036F\u{b7}\u{2be}]'; // \u{2bc}
|
|
11
|
-
|
|
12
|
-
const accent_reg = new RegExp(accent_pat, 'g');
|
|
13
|
-
var diacritic_patterns;
|
|
14
|
-
const latin_convert = {
|
|
15
|
-
'æ': 'ae',
|
|
16
|
-
'ⱥ': 'a',
|
|
17
|
-
'ø': 'o'
|
|
18
|
-
};
|
|
19
|
-
const convert_pat = new RegExp(Object.keys(latin_convert).join('|'), 'g');
|
|
20
|
-
/**
|
|
21
|
-
* code points generated from toCodePoints();
|
|
22
|
-
* removed 65339 to 65345
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
const code_points = [[67, 67], [160, 160], [192, 438], [452, 652], [961, 961], [1019, 1019], [1083, 1083], [1281, 1289], [1984, 1984], [5095, 5095], [7429, 7441], [7545, 7549], [7680, 7935], [8580, 8580], [9398, 9449], [11360, 11391], [42792, 42793], [42802, 42851], [42873, 42897], [42912, 42922], [64256, 64260], [65313, 65338], [65345, 65370]];
|
|
26
|
-
/**
|
|
27
|
-
* Remove accents
|
|
28
|
-
* via https://github.com/krisk/Fuse/issues/133#issuecomment-318692703
|
|
29
|
-
*
|
|
30
|
-
*/
|
|
31
|
-
|
|
32
|
-
const asciifold = str => {
|
|
33
|
-
return str.normalize('NFKD').replace(accent_reg, '').toLowerCase().replace(convert_pat, function (foreignletter) {
|
|
34
|
-
return latin_convert[foreignletter];
|
|
35
|
-
});
|
|
36
|
-
};
|
|
37
|
-
/**
|
|
38
|
-
* Convert array of strings to a regular expression
|
|
39
|
-
* ex ['ab','a'] => (?:ab|a)
|
|
40
|
-
*
|
|
41
|
-
*/
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const arrayToPattern = (chars, glue = '|') => {
|
|
45
|
-
if (chars.length > 1) {
|
|
46
|
-
return '(?:' + chars.join(glue) + ')';
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return chars[0];
|
|
50
|
-
};
|
|
51
|
-
/**
|
|
52
|
-
* Get all possible combinations of substrings that add up to the given string
|
|
53
|
-
* https://stackoverflow.com/questions/30169587/find-all-the-combination-of-substrings-that-add-up-to-the-given-string
|
|
54
|
-
*
|
|
55
|
-
*/
|
|
56
|
-
|
|
57
|
-
const allSubstrings = input => {
|
|
58
|
-
if (input.length === 1) return [[input]];
|
|
59
|
-
var result = [];
|
|
60
|
-
allSubstrings(input.substring(1)).forEach(function (subresult) {
|
|
61
|
-
var tmp = subresult.slice(0);
|
|
62
|
-
tmp[0] = input.charAt(0) + tmp[0];
|
|
63
|
-
result.push(tmp);
|
|
64
|
-
tmp = subresult.slice(0);
|
|
65
|
-
tmp.unshift(input.charAt(0));
|
|
66
|
-
result.push(tmp);
|
|
67
|
-
});
|
|
68
|
-
return result;
|
|
69
|
-
};
|
|
70
|
-
/**
|
|
71
|
-
* Generate a list of diacritics from the list of code points
|
|
72
|
-
*
|
|
73
|
-
*/
|
|
74
|
-
|
|
75
|
-
const generateDiacritics = () => {
|
|
76
|
-
var diacritics = {};
|
|
77
|
-
code_points.forEach(code_range => {
|
|
78
|
-
for (let i = code_range[0]; i <= code_range[1]; i++) {
|
|
79
|
-
let diacritic = String.fromCharCode(i);
|
|
80
|
-
let latin = asciifold(diacritic);
|
|
81
|
-
|
|
82
|
-
if (latin == diacritic.toLowerCase()) {
|
|
83
|
-
continue;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (!(latin in diacritics)) {
|
|
87
|
-
diacritics[latin] = [latin];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
diacritics[latin].push(diacritic);
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
var latin_chars = Object.keys(diacritics); // latin character pattern
|
|
94
|
-
// match longer substrings first
|
|
95
|
-
|
|
96
|
-
latin_chars = latin_chars.sort((a, b) => b.length - a.length);
|
|
97
|
-
latin_pat = new RegExp('(' + arrayToPattern(latin_chars) + accent_pat + '*)', 'g'); // build diacritic patterns
|
|
98
|
-
// ae needs:
|
|
99
|
-
// (?:(?:ae|Æ|Ǽ|Ǣ)|(?:A|Ⓐ|A...)(?:E|ɛ|Ⓔ...))
|
|
100
|
-
|
|
101
|
-
var diacritic_patterns = {};
|
|
102
|
-
latin_chars.sort((a, b) => a.length - b.length).forEach(latin => {
|
|
103
|
-
var substrings = allSubstrings(latin);
|
|
104
|
-
var pattern = substrings.map(sub_pat => {
|
|
105
|
-
sub_pat = sub_pat.map(l => {
|
|
106
|
-
if (diacritics.hasOwnProperty(l)) {
|
|
107
|
-
return arrayToPattern(diacritics[l]);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return l;
|
|
111
|
-
});
|
|
112
|
-
return arrayToPattern(sub_pat, '');
|
|
113
|
-
});
|
|
114
|
-
diacritic_patterns[latin] = arrayToPattern(pattern);
|
|
115
|
-
});
|
|
116
|
-
return diacritic_patterns;
|
|
117
|
-
};
|
|
118
|
-
/**
|
|
119
|
-
* Expand a regular expression pattern to include diacritics
|
|
120
|
-
* eg /a/ becomes /aⓐaẚàáâầấẫẩãāăằắẵẳȧǡäǟảåǻǎȁȃạậặḁąⱥɐɑAⒶAÀÁÂẦẤẪẨÃĀĂẰẮẴẲȦǠÄǞẢÅǺǍȀȂẠẬẶḀĄȺⱯ/
|
|
121
|
-
*
|
|
122
|
-
*/
|
|
123
|
-
|
|
124
|
-
const diacriticRegexPoints = regex => {
|
|
125
|
-
if (diacritic_patterns === undefined) {
|
|
126
|
-
diacritic_patterns = generateDiacritics();
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const decomposed = regex.normalize('NFKD').toLowerCase();
|
|
130
|
-
return decomposed.split(latin_pat).map(part => {
|
|
131
|
-
if (part == '') {
|
|
132
|
-
return '';
|
|
133
|
-
} // "ffl" or "ffl"
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const no_accent = asciifold(part);
|
|
137
|
-
|
|
138
|
-
if (diacritic_patterns.hasOwnProperty(no_accent)) {
|
|
139
|
-
return diacritic_patterns[no_accent];
|
|
140
|
-
} // 'أهلا' (\u{623}\u{647}\u{644}\u{627}) or 'أهلا' (\u{627}\u{654}\u{647}\u{644}\u{627})
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
const composed_part = part.normalize('NFC');
|
|
144
|
-
|
|
145
|
-
if (composed_part != part) {
|
|
146
|
-
return arrayToPattern([part, composed_part]);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
return part;
|
|
150
|
-
}).join('');
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
// @ts-ignore TS2691 "An import path cannot end with a '.ts' extension"
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* A property getter resolving dot-notation
|
|
157
|
-
* @param {Object} obj The root object to fetch property on
|
|
158
|
-
* @param {String} name The optionally dotted property name to fetch
|
|
159
|
-
* @return {Object} The resolved property value
|
|
160
|
-
*/
|
|
161
|
-
const getAttr = (obj, name) => {
|
|
162
|
-
if (!obj) return;
|
|
163
|
-
return obj[name];
|
|
164
|
-
};
|
|
165
|
-
/**
|
|
166
|
-
* A property getter resolving dot-notation
|
|
167
|
-
* @param {Object} obj The root object to fetch property on
|
|
168
|
-
* @param {String} name The optionally dotted property name to fetch
|
|
169
|
-
* @return {Object} The resolved property value
|
|
170
|
-
*/
|
|
171
|
-
|
|
172
|
-
const getAttrNesting = (obj, name) => {
|
|
173
|
-
if (!obj) return;
|
|
174
|
-
var part,
|
|
175
|
-
names = name.split(".");
|
|
176
|
-
|
|
177
|
-
while ((part = names.shift()) && (obj = obj[part]));
|
|
178
|
-
|
|
179
|
-
return obj;
|
|
180
|
-
};
|
|
181
|
-
/**
|
|
182
|
-
* Calculates how close of a match the
|
|
183
|
-
* given value is against a search token.
|
|
184
|
-
*
|
|
185
|
-
*/
|
|
186
|
-
|
|
187
|
-
const scoreValue = (value, token, weight) => {
|
|
188
|
-
var score, pos;
|
|
189
|
-
if (!value) return 0;
|
|
190
|
-
value = value + '';
|
|
191
|
-
pos = value.search(token.regex);
|
|
192
|
-
if (pos === -1) return 0;
|
|
193
|
-
score = token.string.length / value.length;
|
|
194
|
-
if (pos === 0) score += 0.5;
|
|
195
|
-
return score * weight;
|
|
196
|
-
};
|
|
197
|
-
/**
|
|
198
|
-
*
|
|
199
|
-
* https://stackoverflow.com/questions/63006601/why-does-u-throw-an-invalid-escape-error
|
|
200
|
-
*/
|
|
201
|
-
|
|
202
|
-
const escape_regex = str => {
|
|
203
|
-
return (str + '').replace(/([\$\(-\+\.\?\[-\^\{-\}])/g, '\\$1');
|
|
204
|
-
};
|
|
205
|
-
/**
|
|
206
|
-
* Cast object property to an array if it exists and has a value
|
|
207
|
-
*
|
|
208
|
-
*/
|
|
209
|
-
|
|
210
|
-
const propToArray = (obj, key) => {
|
|
211
|
-
var value = obj[key];
|
|
212
|
-
|
|
213
|
-
if (value && !Array.isArray(value)) {
|
|
214
|
-
obj[key] = [value];
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
/**
|
|
218
|
-
* Iterates over arrays and hashes.
|
|
219
|
-
*
|
|
220
|
-
* ```
|
|
221
|
-
* iterate(this.items, function(item, id) {
|
|
222
|
-
* // invoked for each item
|
|
223
|
-
* });
|
|
224
|
-
* ```
|
|
225
|
-
*
|
|
226
|
-
*/
|
|
227
|
-
|
|
228
|
-
const iterate = (object, callback) => {
|
|
229
|
-
if (Array.isArray(object)) {
|
|
230
|
-
object.forEach(callback);
|
|
231
|
-
} else {
|
|
232
|
-
for (var key in object) {
|
|
233
|
-
if (object.hasOwnProperty(key)) {
|
|
234
|
-
callback(object[key], key);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
const cmp = (a, b) => {
|
|
240
|
-
if (typeof a === 'number' && typeof b === 'number') {
|
|
241
|
-
return a > b ? 1 : a < b ? -1 : 0;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
a = asciifold(a + '').toLowerCase();
|
|
245
|
-
b = asciifold(b + '').toLowerCase();
|
|
246
|
-
if (a > b) return 1;
|
|
247
|
-
if (b > a) return -1;
|
|
248
|
-
return 0;
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* sifter.js
|
|
253
|
-
* Copyright (c) 2013–2020 Brian Reavis & contributors
|
|
254
|
-
*
|
|
255
|
-
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
|
|
256
|
-
* file except in compliance with the License. You may obtain a copy of the License at:
|
|
257
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
258
|
-
*
|
|
259
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
260
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
|
261
|
-
* ANY KIND, either express or implied. See the License for the specific language
|
|
262
|
-
* governing permissions and limitations under the License.
|
|
263
|
-
*
|
|
264
|
-
* @author Brian Reavis <brian@thirdroute.com>
|
|
265
|
-
*/
|
|
266
|
-
|
|
267
|
-
class Sifter {
|
|
268
|
-
// []|{};
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* Textually searches arrays and hashes of objects
|
|
272
|
-
* by property (or multiple properties). Designed
|
|
273
|
-
* specifically for autocomplete.
|
|
274
|
-
*
|
|
275
|
-
*/
|
|
276
|
-
constructor(items, settings) {
|
|
277
|
-
this.items = void 0;
|
|
278
|
-
this.settings = void 0;
|
|
279
|
-
this.items = items;
|
|
280
|
-
this.settings = settings || {
|
|
281
|
-
diacritics: true
|
|
282
|
-
};
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
/**
|
|
286
|
-
* Splits a search string into an array of individual
|
|
287
|
-
* regexps to be used to match results.
|
|
288
|
-
*
|
|
289
|
-
*/
|
|
290
|
-
tokenize(query, respect_word_boundaries, weights) {
|
|
291
|
-
if (!query || !query.length) return [];
|
|
292
|
-
const tokens = [];
|
|
293
|
-
const words = query.split(/\s+/);
|
|
294
|
-
var field_regex;
|
|
295
|
-
|
|
296
|
-
if (weights) {
|
|
297
|
-
field_regex = new RegExp('^(' + Object.keys(weights).map(escape_regex).join('|') + ')\:(.*)$');
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
words.forEach(word => {
|
|
301
|
-
let field_match;
|
|
302
|
-
let field = null;
|
|
303
|
-
let regex = null; // look for "field:query" tokens
|
|
304
|
-
|
|
305
|
-
if (field_regex && (field_match = word.match(field_regex))) {
|
|
306
|
-
field = field_match[1];
|
|
307
|
-
word = field_match[2];
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
if (word.length > 0) {
|
|
311
|
-
regex = escape_regex(word);
|
|
312
|
-
|
|
313
|
-
if (this.settings.diacritics) {
|
|
314
|
-
regex = diacriticRegexPoints(regex);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
if (respect_word_boundaries) regex = "\\b" + regex;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
tokens.push({
|
|
321
|
-
string: word,
|
|
322
|
-
regex: regex ? new RegExp(regex, 'iu') : null,
|
|
323
|
-
field: field
|
|
324
|
-
});
|
|
325
|
-
});
|
|
326
|
-
return tokens;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Returns a function to be used to score individual results.
|
|
331
|
-
*
|
|
332
|
-
* Good matches will have a higher score than poor matches.
|
|
333
|
-
* If an item is not a match, 0 will be returned by the function.
|
|
334
|
-
*
|
|
335
|
-
* @returns {function}
|
|
336
|
-
*/
|
|
337
|
-
getScoreFunction(query, options) {
|
|
338
|
-
var search = this.prepareSearch(query, options);
|
|
339
|
-
return this._getScoreFunction(search);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
_getScoreFunction(search) {
|
|
343
|
-
const tokens = search.tokens,
|
|
344
|
-
token_count = tokens.length;
|
|
345
|
-
|
|
346
|
-
if (!token_count) {
|
|
347
|
-
return function () {
|
|
348
|
-
return 0;
|
|
349
|
-
};
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
const fields = search.options.fields,
|
|
353
|
-
weights = search.weights,
|
|
354
|
-
field_count = fields.length,
|
|
355
|
-
getAttrFn = search.getAttrFn;
|
|
356
|
-
|
|
357
|
-
if (!field_count) {
|
|
358
|
-
return function () {
|
|
359
|
-
return 1;
|
|
360
|
-
};
|
|
361
|
-
}
|
|
362
|
-
/**
|
|
363
|
-
* Calculates the score of an object
|
|
364
|
-
* against the search query.
|
|
365
|
-
*
|
|
366
|
-
*/
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
const scoreObject = function () {
|
|
370
|
-
if (field_count === 1) {
|
|
371
|
-
return function (token, data) {
|
|
372
|
-
const field = fields[0].field;
|
|
373
|
-
return scoreValue(getAttrFn(data, field), token, weights[field]);
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
return function (token, data) {
|
|
378
|
-
var sum = 0; // is the token specific to a field?
|
|
379
|
-
|
|
380
|
-
if (token.field) {
|
|
381
|
-
const value = getAttrFn(data, token.field);
|
|
382
|
-
|
|
383
|
-
if (!token.regex && value) {
|
|
384
|
-
sum += 1 / field_count;
|
|
385
|
-
} else {
|
|
386
|
-
sum += scoreValue(value, token, 1);
|
|
387
|
-
}
|
|
388
|
-
} else {
|
|
389
|
-
iterate(weights, (weight, field) => {
|
|
390
|
-
sum += scoreValue(getAttrFn(data, field), token, weight);
|
|
391
|
-
});
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
return sum / field_count;
|
|
395
|
-
};
|
|
396
|
-
}();
|
|
397
|
-
|
|
398
|
-
if (token_count === 1) {
|
|
399
|
-
return function (data) {
|
|
400
|
-
return scoreObject(tokens[0], data);
|
|
401
|
-
};
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
if (search.options.conjunction === 'and') {
|
|
405
|
-
return function (data) {
|
|
406
|
-
var i = 0,
|
|
407
|
-
score,
|
|
408
|
-
sum = 0;
|
|
409
|
-
|
|
410
|
-
for (; i < token_count; i++) {
|
|
411
|
-
score = scoreObject(tokens[i], data);
|
|
412
|
-
if (score <= 0) return 0;
|
|
413
|
-
sum += score;
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
return sum / token_count;
|
|
417
|
-
};
|
|
418
|
-
} else {
|
|
419
|
-
return function (data) {
|
|
420
|
-
var sum = 0;
|
|
421
|
-
iterate(tokens, token => {
|
|
422
|
-
sum += scoreObject(token, data);
|
|
423
|
-
});
|
|
424
|
-
return sum / token_count;
|
|
425
|
-
};
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
/**
|
|
430
|
-
* Returns a function that can be used to compare two
|
|
431
|
-
* results, for sorting purposes. If no sorting should
|
|
432
|
-
* be performed, `null` will be returned.
|
|
433
|
-
*
|
|
434
|
-
* @return function(a,b)
|
|
435
|
-
*/
|
|
436
|
-
getSortFunction(query, options) {
|
|
437
|
-
var search = this.prepareSearch(query, options);
|
|
438
|
-
return this._getSortFunction(search);
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
_getSortFunction(search) {
|
|
442
|
-
var i, n, implicit_score;
|
|
443
|
-
const self = this,
|
|
444
|
-
options = search.options,
|
|
445
|
-
sort = !search.query && options.sort_empty ? options.sort_empty : options.sort,
|
|
446
|
-
sort_flds = [],
|
|
447
|
-
multipliers = [];
|
|
448
|
-
/**
|
|
449
|
-
* Fetches the specified sort field value
|
|
450
|
-
* from a search result item.
|
|
451
|
-
*
|
|
452
|
-
*/
|
|
453
|
-
|
|
454
|
-
const get_field = function get_field(name, result) {
|
|
455
|
-
if (name === '$score') return result.score;
|
|
456
|
-
return search.getAttrFn(self.items[result.id], name);
|
|
457
|
-
}; // parse options
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
if (sort) {
|
|
461
|
-
for (i = 0, n = sort.length; i < n; i++) {
|
|
462
|
-
if (search.query || sort[i].field !== '$score') {
|
|
463
|
-
sort_flds.push(sort[i]);
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
} // the "$score" field is implied to be the primary
|
|
467
|
-
// sort field, unless it's manually specified
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
if (search.query) {
|
|
471
|
-
implicit_score = true;
|
|
472
|
-
|
|
473
|
-
for (i = 0, n = sort_flds.length; i < n; i++) {
|
|
474
|
-
if (sort_flds[i].field === '$score') {
|
|
475
|
-
implicit_score = false;
|
|
476
|
-
break;
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
if (implicit_score) {
|
|
481
|
-
sort_flds.unshift({
|
|
482
|
-
field: '$score',
|
|
483
|
-
direction: 'desc'
|
|
484
|
-
});
|
|
485
|
-
}
|
|
486
|
-
} else {
|
|
487
|
-
for (i = 0, n = sort_flds.length; i < n; i++) {
|
|
488
|
-
if (sort_flds[i].field === '$score') {
|
|
489
|
-
sort_flds.splice(i, 1);
|
|
490
|
-
break;
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
for (i = 0, n = sort_flds.length; i < n; i++) {
|
|
496
|
-
multipliers.push(sort_flds[i].direction === 'desc' ? -1 : 1);
|
|
497
|
-
} // build function
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
const sort_flds_count = sort_flds.length;
|
|
501
|
-
|
|
502
|
-
if (!sort_flds_count) {
|
|
503
|
-
return null;
|
|
504
|
-
} else if (sort_flds_count === 1) {
|
|
505
|
-
const sort_fld = sort_flds[0].field;
|
|
506
|
-
const multiplier = multipliers[0];
|
|
507
|
-
return function (a, b) {
|
|
508
|
-
return multiplier * cmp(get_field(sort_fld, a), get_field(sort_fld, b));
|
|
509
|
-
};
|
|
510
|
-
} else {
|
|
511
|
-
return function (a, b) {
|
|
512
|
-
var i, result, field;
|
|
513
|
-
|
|
514
|
-
for (i = 0; i < sort_flds_count; i++) {
|
|
515
|
-
field = sort_flds[i].field;
|
|
516
|
-
result = multipliers[i] * cmp(get_field(field, a), get_field(field, b));
|
|
517
|
-
if (result) return result;
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
return 0;
|
|
521
|
-
};
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
/**
|
|
526
|
-
* Parses a search query and returns an object
|
|
527
|
-
* with tokens and fields ready to be populated
|
|
528
|
-
* with results.
|
|
529
|
-
*
|
|
530
|
-
*/
|
|
531
|
-
prepareSearch(query, optsUser) {
|
|
532
|
-
const weights = {};
|
|
533
|
-
var options = Object.assign({}, optsUser);
|
|
534
|
-
propToArray(options, 'sort');
|
|
535
|
-
propToArray(options, 'sort_empty'); // convert fields to new format
|
|
536
|
-
|
|
537
|
-
if (options.fields) {
|
|
538
|
-
propToArray(options, 'fields');
|
|
539
|
-
const fields = [];
|
|
540
|
-
options.fields.forEach(field => {
|
|
541
|
-
if (typeof field == 'string') {
|
|
542
|
-
field = {
|
|
543
|
-
field: field,
|
|
544
|
-
weight: 1
|
|
545
|
-
};
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
fields.push(field);
|
|
549
|
-
weights[field.field] = 'weight' in field ? field.weight : 1;
|
|
550
|
-
});
|
|
551
|
-
options.fields = fields;
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
return {
|
|
555
|
-
options: options,
|
|
556
|
-
query: query.toLowerCase().trim(),
|
|
557
|
-
tokens: this.tokenize(query, options.respect_word_boundaries, weights),
|
|
558
|
-
total: 0,
|
|
559
|
-
items: [],
|
|
560
|
-
weights: weights,
|
|
561
|
-
getAttrFn: options.nesting ? getAttrNesting : getAttr
|
|
562
|
-
};
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
/**
|
|
566
|
-
* Searches through all items and returns a sorted array of matches.
|
|
567
|
-
*
|
|
568
|
-
*/
|
|
569
|
-
search(query, options) {
|
|
570
|
-
var self = this,
|
|
571
|
-
score,
|
|
572
|
-
search;
|
|
573
|
-
search = this.prepareSearch(query, options);
|
|
574
|
-
options = search.options;
|
|
575
|
-
query = search.query; // generate result scoring function
|
|
576
|
-
|
|
577
|
-
const fn_score = options.score || self._getScoreFunction(search); // perform search and sort
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
if (query.length) {
|
|
581
|
-
iterate(self.items, (item, id) => {
|
|
582
|
-
score = fn_score(item);
|
|
583
|
-
|
|
584
|
-
if (options.filter === false || score > 0) {
|
|
585
|
-
search.items.push({
|
|
586
|
-
'score': score,
|
|
587
|
-
'id': id
|
|
588
|
-
});
|
|
589
|
-
}
|
|
590
|
-
});
|
|
591
|
-
} else {
|
|
592
|
-
iterate(self.items, (item, id) => {
|
|
593
|
-
search.items.push({
|
|
594
|
-
'score': 1,
|
|
595
|
-
'id': id
|
|
596
|
-
});
|
|
597
|
-
});
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
const fn_sort = self._getSortFunction(search);
|
|
601
|
-
|
|
602
|
-
if (fn_sort) search.items.sort(fn_sort); // apply limits
|
|
603
|
-
|
|
604
|
-
search.total = search.items.length;
|
|
605
|
-
|
|
606
|
-
if (typeof options.limit === 'number') {
|
|
607
|
-
search.items = search.items.slice(0, options.limit);
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
return search;
|
|
611
|
-
}
|
|
612
|
-
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
return Sifter;
|
|
616
|
-
|
|
617
|
-
})));
|
|
618
|
-
//# sourceMappingURL=sifter.js.map
|