@orchidjs/sifter 1.0.3 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/sifter.d.ts +72 -0
- package/dist/cjs/sifter.js +290 -1101
- package/dist/cjs/sifter.js.map +1 -1
- package/dist/cjs/types.d.ts +57 -0
- package/dist/cjs/types.js +3 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/utils.d.ts +48 -0
- package/dist/cjs/utils.js +102 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/esm/sifter.d.ts +72 -0
- package/dist/esm/sifter.js +267 -330
- package/dist/esm/sifter.js.map +1 -1
- package/dist/esm/types.d.ts +57 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/utils.d.ts +48 -0
- package/dist/esm/utils.js +54 -56
- package/dist/esm/utils.js.map +1 -1
- package/dist/types/sifter.d.ts +3 -2
- package/dist/types/types.d.ts +12 -12
- package/dist/types/utils.d.ts +2 -2
- package/dist/umd/package.json +1 -0
- package/dist/umd/sifter.js +796 -1036
- package/dist/umd/sifter.js.map +1 -1
- package/dist/umd/sifter.min.js +56 -57
- package/dist/umd/sifter.min.js.map +1 -1
- package/lib/sifter.ts +3 -2
- package/lib/types.ts +1 -1
- package/lib/utils.ts +1 -1
- package/package.json +31 -21
- package/dist/esm/lib/sifter.js +0 -368
- package/dist/esm/lib/sifter.js.map +0 -1
- package/dist/esm/lib/utils.js +0 -95
- package/dist/esm/lib/utils.js.map +0 -1
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/index.js +0 -547
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/index.js.map +0 -1
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/regex.js +0 -113
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/regex.js.map +0 -1
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/strings.js +0 -29
- package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/strings.js.map +0 -1
package/dist/esm/sifter.js
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
/*! sifter.js | https://github.com/orchidjs/sifter.js | Apache License (v2) */
|
|
2
|
-
import { iterate, cmp, propToArray, getAttrNesting, getAttr, scoreValue } from './utils.js';
|
|
3
|
-
export { cmp, getAttr, getAttrNesting, iterate, propToArray, scoreValue } from './utils.js';
|
|
4
|
-
import { escape_regex, getPattern } from '@orchidjs/unicode-variants';
|
|
5
|
-
export { getPattern } from '@orchidjs/unicode-variants';
|
|
6
|
-
|
|
7
1
|
/**
|
|
8
2
|
* sifter.js
|
|
9
3
|
* Copyright (c) 2013–2020 Brian Reavis & contributors
|
|
@@ -19,349 +13,292 @@ export { getPattern } from '@orchidjs/unicode-variants';
|
|
|
19
13
|
*
|
|
20
14
|
* @author Brian Reavis <brian@thirdroute.com>
|
|
21
15
|
*/
|
|
22
|
-
|
|
16
|
+
import { scoreValue, getAttr, getAttrNesting, propToArray, iterate, cmp } from "./utils.js";
|
|
17
|
+
import { getPattern, escape_regex } from '@orchidjs/unicode-variants';
|
|
23
18
|
class Sifter {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
this.items = items;
|
|
36
|
-
this.settings = settings || {
|
|
37
|
-
diacritics: true
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Splits a search string into an array of individual
|
|
43
|
-
* regexps to be used to match results.
|
|
44
|
-
*
|
|
45
|
-
*/
|
|
46
|
-
tokenize(query, respect_word_boundaries, weights) {
|
|
47
|
-
if (!query || !query.length) return [];
|
|
48
|
-
const tokens = [];
|
|
49
|
-
const words = query.split(/\s+/);
|
|
50
|
-
var field_regex;
|
|
51
|
-
|
|
52
|
-
if (weights) {
|
|
53
|
-
field_regex = new RegExp('^(' + Object.keys(weights).map(escape_regex).join('|') + ')\:(.*)$');
|
|
19
|
+
items; // []|{};
|
|
20
|
+
settings;
|
|
21
|
+
/**
|
|
22
|
+
* Textually searches arrays and hashes of objects
|
|
23
|
+
* by property (or multiple properties). Designed
|
|
24
|
+
* specifically for autocomplete.
|
|
25
|
+
*
|
|
26
|
+
*/
|
|
27
|
+
constructor(items, settings) {
|
|
28
|
+
this.items = items;
|
|
29
|
+
this.settings = settings || { diacritics: true };
|
|
54
30
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (
|
|
68
|
-
|
|
69
|
-
} else {
|
|
70
|
-
regex = escape_regex(word);
|
|
31
|
+
;
|
|
32
|
+
/**
|
|
33
|
+
* Splits a search string into an array of individual
|
|
34
|
+
* regexps to be used to match results.
|
|
35
|
+
*
|
|
36
|
+
*/
|
|
37
|
+
tokenize(query, respect_word_boundaries, weights) {
|
|
38
|
+
if (!query || !query.length)
|
|
39
|
+
return [];
|
|
40
|
+
const tokens = [];
|
|
41
|
+
const words = query.split(/\s+/);
|
|
42
|
+
var field_regex;
|
|
43
|
+
if (weights) {
|
|
44
|
+
field_regex = new RegExp('^(' + Object.keys(weights).map(escape_regex).join('|') + ')\:(.*)$');
|
|
71
45
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
* @returns {T.ScoreFn}
|
|
99
|
-
*
|
|
100
|
-
*/
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
_getScoreFunction(search) {
|
|
104
|
-
const tokens = search.tokens,
|
|
105
|
-
token_count = tokens.length;
|
|
106
|
-
|
|
107
|
-
if (!token_count) {
|
|
108
|
-
return function () {
|
|
109
|
-
return 0;
|
|
110
|
-
};
|
|
46
|
+
words.forEach((word) => {
|
|
47
|
+
let field_match;
|
|
48
|
+
let field = null;
|
|
49
|
+
let regex = null;
|
|
50
|
+
// look for "field:query" tokens
|
|
51
|
+
if (field_regex && (field_match = word.match(field_regex))) {
|
|
52
|
+
field = field_match[1];
|
|
53
|
+
word = field_match[2];
|
|
54
|
+
}
|
|
55
|
+
if (word.length > 0) {
|
|
56
|
+
if (this.settings.diacritics) {
|
|
57
|
+
regex = getPattern(word) || null;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
regex = escape_regex(word);
|
|
61
|
+
}
|
|
62
|
+
if (regex && respect_word_boundaries)
|
|
63
|
+
regex = "\\b" + regex;
|
|
64
|
+
}
|
|
65
|
+
tokens.push({
|
|
66
|
+
string: word,
|
|
67
|
+
regex: regex ? new RegExp(regex, 'iu') : null,
|
|
68
|
+
field: field,
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
return tokens;
|
|
111
72
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
73
|
+
;
|
|
74
|
+
/**
|
|
75
|
+
* Returns a function to be used to score individual results.
|
|
76
|
+
*
|
|
77
|
+
* Good matches will have a higher score than poor matches.
|
|
78
|
+
* If an item is not a match, 0 will be returned by the function.
|
|
79
|
+
*
|
|
80
|
+
* @returns {T.ScoreFn}
|
|
81
|
+
*/
|
|
82
|
+
getScoreFunction(query, options) {
|
|
83
|
+
var search = this.prepareSearch(query, options);
|
|
84
|
+
return this._getScoreFunction(search);
|
|
122
85
|
}
|
|
123
86
|
/**
|
|
124
|
-
*
|
|
125
|
-
* against the search query.
|
|
87
|
+
* @returns {T.ScoreFn}
|
|
126
88
|
*
|
|
127
89
|
*/
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
return function (token, data) {
|
|
133
|
-
const field = fields[0].field;
|
|
134
|
-
return scoreValue(getAttrFn(data, field), token, weights[field] || 1);
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
return function (token, data) {
|
|
139
|
-
var sum = 0; // is the token specific to a field?
|
|
140
|
-
|
|
141
|
-
if (token.field) {
|
|
142
|
-
const value = getAttrFn(data, token.field);
|
|
143
|
-
|
|
144
|
-
if (!token.regex && value) {
|
|
145
|
-
sum += 1 / field_count;
|
|
146
|
-
} else {
|
|
147
|
-
sum += scoreValue(value, token, 1);
|
|
148
|
-
}
|
|
149
|
-
} else {
|
|
150
|
-
iterate(weights, (weight, field) => {
|
|
151
|
-
sum += scoreValue(getAttrFn(data, field), token, weight);
|
|
152
|
-
});
|
|
90
|
+
_getScoreFunction(search) {
|
|
91
|
+
const tokens = search.tokens, token_count = tokens.length;
|
|
92
|
+
if (!token_count) {
|
|
93
|
+
return function () { return 0; };
|
|
153
94
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
95
|
+
const fields = search.options.fields, weights = search.weights, field_count = fields.length, getAttrFn = search.getAttrFn;
|
|
96
|
+
if (!field_count) {
|
|
97
|
+
return function () { return 1; };
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Calculates the score of an object
|
|
101
|
+
* against the search query.
|
|
102
|
+
*
|
|
103
|
+
*/
|
|
104
|
+
const scoreObject = (function () {
|
|
105
|
+
if (field_count === 1) {
|
|
106
|
+
return function (token, data) {
|
|
107
|
+
const field = fields[0].field;
|
|
108
|
+
return scoreValue(getAttrFn(data, field), token, weights[field] || 1);
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
return function (token, data) {
|
|
112
|
+
var sum = 0;
|
|
113
|
+
// is the token specific to a field?
|
|
114
|
+
if (token.field) {
|
|
115
|
+
const value = getAttrFn(data, token.field);
|
|
116
|
+
if (!token.regex && value) {
|
|
117
|
+
sum += (1 / field_count);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
sum += scoreValue(value, token, 1);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
iterate(weights, (weight, field) => {
|
|
125
|
+
sum += scoreValue(getAttrFn(data, field), token, weight);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return sum / field_count;
|
|
129
|
+
};
|
|
130
|
+
})();
|
|
131
|
+
if (token_count === 1) {
|
|
132
|
+
return function (data) {
|
|
133
|
+
return scoreObject(tokens[0], data);
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
if (search.options.conjunction === 'and') {
|
|
137
|
+
return function (data) {
|
|
138
|
+
var score, sum = 0;
|
|
139
|
+
for (let token of tokens) {
|
|
140
|
+
score = scoreObject(token, data);
|
|
141
|
+
if (score <= 0)
|
|
142
|
+
return 0;
|
|
143
|
+
sum += score;
|
|
144
|
+
}
|
|
145
|
+
return sum / token_count;
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
return function (data) {
|
|
150
|
+
var sum = 0;
|
|
151
|
+
iterate(tokens, (token) => {
|
|
152
|
+
sum += scoreObject(token, data);
|
|
153
|
+
});
|
|
154
|
+
return sum / token_count;
|
|
155
|
+
};
|
|
174
156
|
}
|
|
175
|
-
|
|
176
|
-
return sum / token_count;
|
|
177
|
-
};
|
|
178
|
-
} else {
|
|
179
|
-
return function (data) {
|
|
180
|
-
var sum = 0;
|
|
181
|
-
iterate(tokens, token => {
|
|
182
|
-
sum += scoreObject(token, data);
|
|
183
|
-
});
|
|
184
|
-
return sum / token_count;
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Returns a function that can be used to compare two
|
|
191
|
-
* results, for sorting purposes. If no sorting should
|
|
192
|
-
* be performed, `null` will be returned.
|
|
193
|
-
*
|
|
194
|
-
* @return function(a,b)
|
|
195
|
-
*/
|
|
196
|
-
getSortFunction(query, options) {
|
|
197
|
-
var search = this.prepareSearch(query, options);
|
|
198
|
-
return this._getSortFunction(search);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
_getSortFunction(search) {
|
|
202
|
-
var implicit_score,
|
|
203
|
-
sort_flds = [];
|
|
204
|
-
const self = this,
|
|
205
|
-
options = search.options,
|
|
206
|
-
sort = !search.query && options.sort_empty ? options.sort_empty : options.sort;
|
|
207
|
-
|
|
208
|
-
if (typeof sort == 'function') {
|
|
209
|
-
return sort.bind(this);
|
|
210
157
|
}
|
|
158
|
+
;
|
|
211
159
|
/**
|
|
212
|
-
*
|
|
213
|
-
*
|
|
160
|
+
* Returns a function that can be used to compare two
|
|
161
|
+
* results, for sorting purposes. If no sorting should
|
|
162
|
+
* be performed, `null` will be returned.
|
|
214
163
|
*
|
|
164
|
+
* @return function(a,b)
|
|
215
165
|
*/
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
for (let s of sort) {
|
|
226
|
-
if (search.query || s.field !== '$score') {
|
|
227
|
-
sort_flds.push(s);
|
|
166
|
+
getSortFunction(query, options) {
|
|
167
|
+
var search = this.prepareSearch(query, options);
|
|
168
|
+
return this._getSortFunction(search);
|
|
169
|
+
}
|
|
170
|
+
_getSortFunction(search) {
|
|
171
|
+
var implicit_score, sort_flds = [];
|
|
172
|
+
const self = this, options = search.options, sort = (!search.query && options.sort_empty) ? options.sort_empty : options.sort;
|
|
173
|
+
if (typeof sort == 'function') {
|
|
174
|
+
return sort.bind(this);
|
|
228
175
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
176
|
+
/**
|
|
177
|
+
* Fetches the specified sort field value
|
|
178
|
+
* from a search result item.
|
|
179
|
+
*
|
|
180
|
+
*/
|
|
181
|
+
const get_field = function (name, result) {
|
|
182
|
+
if (name === '$score')
|
|
183
|
+
return result.score;
|
|
184
|
+
return search.getAttrFn(self.items[result.id], name);
|
|
185
|
+
};
|
|
186
|
+
// parse options
|
|
187
|
+
if (sort) {
|
|
188
|
+
for (let s of sort) {
|
|
189
|
+
if (search.query || s.field !== '$score') {
|
|
190
|
+
sort_flds.push(s);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
241
193
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
return null;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
return function (a, b) {
|
|
263
|
-
var result, field;
|
|
264
|
-
|
|
265
|
-
for (let sort_fld of sort_flds) {
|
|
266
|
-
field = sort_fld.field;
|
|
267
|
-
let multiplier = sort_fld.direction === 'desc' ? -1 : 1;
|
|
268
|
-
result = multiplier * cmp(get_field(field, a), get_field(field, b));
|
|
269
|
-
if (result) return result;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
return 0;
|
|
273
|
-
};
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
* Parses a search query and returns an object
|
|
278
|
-
* with tokens and fields ready to be populated
|
|
279
|
-
* with results.
|
|
280
|
-
*
|
|
281
|
-
*/
|
|
282
|
-
prepareSearch(query, optsUser) {
|
|
283
|
-
const weights = {};
|
|
284
|
-
var options = Object.assign({}, optsUser);
|
|
285
|
-
propToArray(options, 'sort');
|
|
286
|
-
propToArray(options, 'sort_empty'); // convert fields to new format
|
|
287
|
-
|
|
288
|
-
if (options.fields) {
|
|
289
|
-
propToArray(options, 'fields');
|
|
290
|
-
const fields = [];
|
|
291
|
-
options.fields.forEach(field => {
|
|
292
|
-
if (typeof field == 'string') {
|
|
293
|
-
field = {
|
|
294
|
-
field: field,
|
|
295
|
-
weight: 1
|
|
296
|
-
};
|
|
194
|
+
// the "$score" field is implied to be the primary
|
|
195
|
+
// sort field, unless it's manually specified
|
|
196
|
+
if (search.query) {
|
|
197
|
+
implicit_score = true;
|
|
198
|
+
for (let fld of sort_flds) {
|
|
199
|
+
if (fld.field === '$score') {
|
|
200
|
+
implicit_score = false;
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (implicit_score) {
|
|
205
|
+
sort_flds.unshift({ field: '$score', direction: 'desc' });
|
|
206
|
+
}
|
|
207
|
+
// without a search.query, all items will have the same score
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
sort_flds = sort_flds.filter((fld) => fld.field !== '$score');
|
|
297
211
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
212
|
+
// build function
|
|
213
|
+
const sort_flds_count = sort_flds.length;
|
|
214
|
+
if (!sort_flds_count) {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
return function (a, b) {
|
|
218
|
+
var result, field;
|
|
219
|
+
for (let sort_fld of sort_flds) {
|
|
220
|
+
field = sort_fld.field;
|
|
221
|
+
let multiplier = sort_fld.direction === 'desc' ? -1 : 1;
|
|
222
|
+
result = multiplier * cmp(get_field(field, a), get_field(field, b));
|
|
223
|
+
if (result)
|
|
224
|
+
return result;
|
|
225
|
+
}
|
|
226
|
+
return 0;
|
|
227
|
+
};
|
|
303
228
|
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
const fn_score = options.score || self._getScoreFunction(search); // perform search and sort
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
if (query.length) {
|
|
332
|
-
iterate(self.items, (item, id) => {
|
|
333
|
-
score = fn_score(item);
|
|
334
|
-
|
|
335
|
-
if (options.filter === false || score > 0) {
|
|
336
|
-
search.items.push({
|
|
337
|
-
'score': score,
|
|
338
|
-
'id': id
|
|
339
|
-
});
|
|
229
|
+
;
|
|
230
|
+
/**
|
|
231
|
+
* Parses a search query and returns an object
|
|
232
|
+
* with tokens and fields ready to be populated
|
|
233
|
+
* with results.
|
|
234
|
+
*
|
|
235
|
+
*/
|
|
236
|
+
prepareSearch(query, optsUser) {
|
|
237
|
+
const weights = {};
|
|
238
|
+
var options = Object.assign({}, optsUser);
|
|
239
|
+
propToArray(options, 'sort');
|
|
240
|
+
propToArray(options, 'sort_empty');
|
|
241
|
+
// convert fields to new format
|
|
242
|
+
if (options.fields) {
|
|
243
|
+
propToArray(options, 'fields');
|
|
244
|
+
const fields = [];
|
|
245
|
+
options.fields.forEach((field) => {
|
|
246
|
+
if (typeof field == 'string') {
|
|
247
|
+
field = { field: field, weight: 1 };
|
|
248
|
+
}
|
|
249
|
+
fields.push(field);
|
|
250
|
+
weights[field.field] = ('weight' in field) ? field.weight : 1;
|
|
251
|
+
});
|
|
252
|
+
options.fields = fields;
|
|
340
253
|
}
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
254
|
+
return {
|
|
255
|
+
options: options,
|
|
256
|
+
query: query.toLowerCase().trim(),
|
|
257
|
+
tokens: this.tokenize(query, options.respect_word_boundaries, weights),
|
|
258
|
+
total: 0,
|
|
259
|
+
items: [],
|
|
260
|
+
weights: weights,
|
|
261
|
+
getAttrFn: (options.nesting) ? getAttrNesting : getAttr,
|
|
262
|
+
};
|
|
349
263
|
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
search
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
264
|
+
;
|
|
265
|
+
/**
|
|
266
|
+
* Searches through all items and returns a sorted array of matches.
|
|
267
|
+
*
|
|
268
|
+
*/
|
|
269
|
+
search(query, options) {
|
|
270
|
+
var self = this, score, search;
|
|
271
|
+
search = this.prepareSearch(query, options);
|
|
272
|
+
options = search.options;
|
|
273
|
+
query = search.query;
|
|
274
|
+
// generate result scoring function
|
|
275
|
+
const fn_score = options.score || self._getScoreFunction(search);
|
|
276
|
+
// perform search and sort
|
|
277
|
+
if (query.length) {
|
|
278
|
+
iterate(self.items, (item, id) => {
|
|
279
|
+
score = fn_score(item);
|
|
280
|
+
if (options.filter === false || score > 0) {
|
|
281
|
+
search.items.push({ 'score': score, 'id': id });
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
iterate(self.items, (_, id) => {
|
|
287
|
+
search.items.push({ 'score': 1, 'id': id });
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
const fn_sort = self._getSortFunction(search);
|
|
291
|
+
if (fn_sort)
|
|
292
|
+
search.items.sort(fn_sort);
|
|
293
|
+
// apply limits
|
|
294
|
+
search.total = search.items.length;
|
|
295
|
+
if (typeof options.limit === 'number') {
|
|
296
|
+
search.items = search.items.slice(0, options.limit);
|
|
297
|
+
}
|
|
298
|
+
return search;
|
|
359
299
|
}
|
|
360
|
-
|
|
361
|
-
return search;
|
|
362
|
-
}
|
|
363
|
-
|
|
300
|
+
;
|
|
364
301
|
}
|
|
365
|
-
|
|
366
|
-
export
|
|
367
|
-
//# sourceMappingURL=sifter.js.map
|
|
302
|
+
export { Sifter, scoreValue, getAttr, getAttrNesting, propToArray, iterate, cmp, getPattern };
|
|
303
|
+
export * from "./types.js";
|
|
304
|
+
//# sourceMappingURL=sifter.js.map
|