@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.
Files changed (41) hide show
  1. package/dist/cjs/package.json +1 -0
  2. package/dist/cjs/sifter.d.ts +72 -0
  3. package/dist/cjs/sifter.js +290 -1110
  4. package/dist/cjs/sifter.js.map +1 -1
  5. package/dist/cjs/types.d.ts +57 -0
  6. package/dist/cjs/types.js +3 -0
  7. package/dist/cjs/types.js.map +1 -0
  8. package/dist/cjs/utils.d.ts +48 -0
  9. package/dist/cjs/utils.js +102 -0
  10. package/dist/cjs/utils.js.map +1 -0
  11. package/dist/esm/sifter.d.ts +72 -0
  12. package/dist/esm/sifter.js +267 -330
  13. package/dist/esm/sifter.js.map +1 -1
  14. package/dist/esm/types.d.ts +57 -0
  15. package/dist/esm/types.js +2 -0
  16. package/dist/esm/types.js.map +1 -0
  17. package/dist/esm/utils.d.ts +48 -0
  18. package/dist/esm/utils.js +54 -56
  19. package/dist/esm/utils.js.map +1 -1
  20. package/dist/types/sifter.d.ts +3 -2
  21. package/dist/types/types.d.ts +12 -12
  22. package/dist/types/utils.d.ts +2 -2
  23. package/dist/umd/package.json +1 -0
  24. package/dist/umd/sifter.js +794 -1043
  25. package/dist/umd/sifter.js.map +1 -1
  26. package/dist/umd/sifter.min.js +72 -77
  27. package/dist/umd/sifter.min.js.map +1 -1
  28. package/lib/sifter.ts +3 -2
  29. package/lib/types.ts +1 -1
  30. package/lib/utils.ts +1 -1
  31. package/package.json +31 -21
  32. package/dist/esm/lib/sifter.js +0 -368
  33. package/dist/esm/lib/sifter.js.map +0 -1
  34. package/dist/esm/lib/utils.js +0 -95
  35. package/dist/esm/lib/utils.js.map +0 -1
  36. package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/index.js +0 -556
  37. package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/index.js.map +0 -1
  38. package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/regex.js +0 -113
  39. package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/regex.js.map +0 -1
  40. package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/strings.js +0 -29
  41. package/dist/esm/node_modules/@orchidjs/unicode-variants/dist/esm/strings.js.map +0 -1
@@ -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
- * Textually searches arrays and hashes of objects
28
- * by property (or multiple properties). Designed
29
- * specifically for autocomplete.
30
- *
31
- */
32
- constructor(items, settings) {
33
- this.items = void 0;
34
- this.settings = void 0;
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
- words.forEach(word => {
57
- let field_match;
58
- let field = null;
59
- let regex = null; // look for "field:query" tokens
60
-
61
- if (field_regex && (field_match = word.match(field_regex))) {
62
- field = field_match[1];
63
- word = field_match[2];
64
- }
65
-
66
- if (word.length > 0) {
67
- if (this.settings.diacritics) {
68
- regex = getPattern(word) || null;
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
- if (regex && respect_word_boundaries) regex = "\\b" + regex;
74
- }
75
-
76
- tokens.push({
77
- string: word,
78
- regex: regex ? new RegExp(regex, 'iu') : null,
79
- field: field
80
- });
81
- });
82
- return tokens;
83
- }
84
-
85
- /**
86
- * Returns a function to be used to score individual results.
87
- *
88
- * Good matches will have a higher score than poor matches.
89
- * If an item is not a match, 0 will be returned by the function.
90
- *
91
- * @returns {T.ScoreFn}
92
- */
93
- getScoreFunction(query, options) {
94
- var search = this.prepareSearch(query, options);
95
- return this._getScoreFunction(search);
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
- const fields = search.options.fields,
114
- weights = search.weights,
115
- field_count = fields.length,
116
- getAttrFn = search.getAttrFn;
117
-
118
- if (!field_count) {
119
- return function () {
120
- return 1;
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
- * Calculates the score of an object
125
- * against the search query.
87
+ * @returns {T.ScoreFn}
126
88
  *
127
89
  */
128
-
129
-
130
- const scoreObject = function () {
131
- if (field_count === 1) {
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
- return sum / field_count;
156
- };
157
- }();
158
-
159
- if (token_count === 1) {
160
- return function (data) {
161
- return scoreObject(tokens[0], data);
162
- };
163
- }
164
-
165
- if (search.options.conjunction === 'and') {
166
- return function (data) {
167
- var score,
168
- sum = 0;
169
-
170
- for (let token of tokens) {
171
- score = scoreObject(token, data);
172
- if (score <= 0) return 0;
173
- sum += score;
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
- * Fetches the specified sort field value
213
- * from a search result item.
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
- const get_field = function get_field(name, result) {
219
- if (name === '$score') return result.score;
220
- return search.getAttrFn(self.items[result.id], name);
221
- }; // parse options
222
-
223
-
224
- if (sort) {
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
- } // the "$score" field is implied to be the primary
231
- // sort field, unless it's manually specified
232
-
233
-
234
- if (search.query) {
235
- implicit_score = true;
236
-
237
- for (let fld of sort_flds) {
238
- if (fld.field === '$score') {
239
- implicit_score = false;
240
- break;
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
- if (implicit_score) {
245
- sort_flds.unshift({
246
- field: '$score',
247
- direction: 'desc'
248
- });
249
- } // without a search.query, all items will have the same score
250
-
251
- } else {
252
- sort_flds = sort_flds.filter(fld => fld.field !== '$score');
253
- } // build function
254
-
255
-
256
- const sort_flds_count = sort_flds.length;
257
-
258
- if (!sort_flds_count) {
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
- fields.push(field);
300
- weights[field.field] = 'weight' in field ? field.weight : 1;
301
- });
302
- options.fields = fields;
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
- return {
306
- options: options,
307
- query: query.toLowerCase().trim(),
308
- tokens: this.tokenize(query, options.respect_word_boundaries, weights),
309
- total: 0,
310
- items: [],
311
- weights: weights,
312
- getAttrFn: options.nesting ? getAttrNesting : getAttr
313
- };
314
- }
315
-
316
- /**
317
- * Searches through all items and returns a sorted array of matches.
318
- *
319
- */
320
- search(query, options) {
321
- var self = this,
322
- score,
323
- search;
324
- search = this.prepareSearch(query, options);
325
- options = search.options;
326
- query = search.query; // generate result scoring function
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
- } else {
343
- iterate(self.items, (_, id) => {
344
- search.items.push({
345
- 'score': 1,
346
- 'id': id
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
- const fn_sort = self._getSortFunction(search);
352
-
353
- if (fn_sort) search.items.sort(fn_sort); // apply limits
354
-
355
- search.total = search.items.length;
356
-
357
- if (typeof options.limit === 'number') {
358
- search.items = search.items.slice(0, options.limit);
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 { Sifter };
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