@orchidjs/sifter 0.8.2 → 0.9.2

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