@abcagency/hc-ui-components 1.7.5 → 1.7.6

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 (49) hide show
  1. package/dist/components/HireControlMap.js +95 -165
  2. package/dist/components/HireControlMap.js.map +1 -1
  3. package/dist/components/modules/accordions/default.js +1 -1
  4. package/dist/components/modules/buttons/button-group-apply.js +1 -1
  5. package/dist/components/modules/buttons/default.js +1 -1
  6. package/dist/components/modules/cards/default.js +1 -1
  7. package/dist/components/modules/filter/sort.js +1 -1
  8. package/dist/components/modules/grid.js +1 -1
  9. package/dist/components/modules/list/header.js +1 -1
  10. package/dist/components/modules/list/item-expand-card/index.js +1 -1
  11. package/dist/contexts/mapListContext.js +33 -38
  12. package/dist/contexts/mapListContext.js.map +1 -1
  13. package/dist/styles/index.css +1 -1
  14. package/dist/util/algoliaSearchUtil.js +1 -1
  15. package/dist/util/filterUtil.js +1 -1
  16. package/dist/util/twMerge.js +1 -1
  17. package/package.json +1 -1
  18. package/src/apis/hcApi.ts +9 -2
  19. package/src/components/HireControlMap.js +19 -54
  20. package/src/contexts/mapListContext.tsx +34 -37
  21. package/src/index.js +1 -0
  22. package/dist/apis/hcApi.js +0 -91
  23. package/dist/apis/hcApi.js.map +0 -1
  24. package/dist/clientToken.js +0 -10
  25. package/dist/clientToken.js.map +0 -1
  26. package/dist/components/modules/skeleton/map-skeleton.js +0 -50
  27. package/dist/components/modules/skeleton/map-skeleton.js.map +0 -1
  28. package/dist/node_modules/@algolia/client-common/dist/common.js +0 -541
  29. package/dist/node_modules/@algolia/client-common/dist/common.js.map +0 -1
  30. package/dist/node_modules/@algolia/requester-browser-xhr/dist/requester.xhr.js +0 -4
  31. package/dist/node_modules/@algolia/requester-browser-xhr/dist/requester.xhr.js.map +0 -1
  32. package/dist/node_modules/@algolia/requester-node-http/dist/requester.http.js +0 -82
  33. package/dist/node_modules/@algolia/requester-node-http/dist/requester.http.js.map +0 -1
  34. package/dist/node_modules/algoliasearch/dist/lite/builds/browser.js +0 -272
  35. package/dist/node_modules/algoliasearch/dist/lite/builds/browser.js.map +0 -1
  36. package/dist/node_modules/algoliasearch/dist/lite/builds/node.js +0 -269
  37. package/dist/node_modules/algoliasearch/dist/lite/builds/node.js.map +0 -1
  38. package/dist/node_modules/fuse.js/dist/fuse.js +0 -1779
  39. package/dist/node_modules/fuse.js/dist/fuse.js.map +0 -1
  40. package/dist/node_modules/tailwind-merge/dist/bundle-mjs.js +0 -2580
  41. package/dist/node_modules/tailwind-merge/dist/bundle-mjs.js.map +0 -1
  42. package/dist/services/configService.js +0 -15
  43. package/dist/services/configService.js.map +0 -1
  44. package/dist/services/listingAggregatorService.js +0 -41
  45. package/dist/services/listingAggregatorService.js.map +0 -1
  46. package/dist/services/listingEntityService.js +0 -16
  47. package/dist/services/listingEntityService.js.map +0 -1
  48. package/dist/services/listingService.js +0 -15
  49. package/dist/services/listingService.js.map +0 -1
@@ -1,1779 +0,0 @@
1
- /**
2
- * Fuse.js v7.0.0 - Lightweight fuzzy-search (http://fusejs.io)
3
- *
4
- * Copyright (c) 2023 Kiro Risk (http://kiro.me)
5
- * All Rights Reserved. Apache Software License 2.0
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- */
9
-
10
- function isArray(value) {
11
- return !Array.isArray
12
- ? getTag(value) === '[object Array]'
13
- : Array.isArray(value)
14
- }
15
-
16
- // Adapted from: https://github.com/lodash/lodash/blob/master/.internal/baseToString.js
17
- const INFINITY = 1 / 0;
18
- function baseToString(value) {
19
- // Exit early for strings to avoid a performance hit in some environments.
20
- if (typeof value == 'string') {
21
- return value
22
- }
23
- let result = value + '';
24
- return result == '0' && 1 / value == -INFINITY ? '-0' : result
25
- }
26
-
27
- function toString(value) {
28
- return value == null ? '' : baseToString(value)
29
- }
30
-
31
- function isString(value) {
32
- return typeof value === 'string'
33
- }
34
-
35
- function isNumber(value) {
36
- return typeof value === 'number'
37
- }
38
-
39
- // Adapted from: https://github.com/lodash/lodash/blob/master/isBoolean.js
40
- function isBoolean(value) {
41
- return (
42
- value === true ||
43
- value === false ||
44
- (isObjectLike(value) && getTag(value) == '[object Boolean]')
45
- )
46
- }
47
-
48
- function isObject(value) {
49
- return typeof value === 'object'
50
- }
51
-
52
- // Checks if `value` is object-like.
53
- function isObjectLike(value) {
54
- return isObject(value) && value !== null
55
- }
56
-
57
- function isDefined(value) {
58
- return value !== undefined && value !== null
59
- }
60
-
61
- function isBlank(value) {
62
- return !value.trim().length
63
- }
64
-
65
- // Gets the `toStringTag` of `value`.
66
- // Adapted from: https://github.com/lodash/lodash/blob/master/.internal/getTag.js
67
- function getTag(value) {
68
- return value == null
69
- ? value === undefined
70
- ? '[object Undefined]'
71
- : '[object Null]'
72
- : Object.prototype.toString.call(value)
73
- }
74
-
75
- const EXTENDED_SEARCH_UNAVAILABLE = 'Extended search is not available';
76
-
77
- const INCORRECT_INDEX_TYPE = "Incorrect 'index' type";
78
-
79
- const LOGICAL_SEARCH_INVALID_QUERY_FOR_KEY = (key) =>
80
- `Invalid value for key ${key}`;
81
-
82
- const PATTERN_LENGTH_TOO_LARGE = (max) =>
83
- `Pattern length exceeds max of ${max}.`;
84
-
85
- const MISSING_KEY_PROPERTY = (name) => `Missing ${name} property in key`;
86
-
87
- const INVALID_KEY_WEIGHT_VALUE = (key) =>
88
- `Property 'weight' in key '${key}' must be a positive integer`;
89
-
90
- const hasOwn = Object.prototype.hasOwnProperty;
91
-
92
- class KeyStore {
93
- constructor(keys) {
94
- this._keys = [];
95
- this._keyMap = {};
96
-
97
- let totalWeight = 0;
98
-
99
- keys.forEach((key) => {
100
- let obj = createKey(key);
101
-
102
- this._keys.push(obj);
103
- this._keyMap[obj.id] = obj;
104
-
105
- totalWeight += obj.weight;
106
- });
107
-
108
- // Normalize weights so that their sum is equal to 1
109
- this._keys.forEach((key) => {
110
- key.weight /= totalWeight;
111
- });
112
- }
113
- get(keyId) {
114
- return this._keyMap[keyId]
115
- }
116
- keys() {
117
- return this._keys
118
- }
119
- toJSON() {
120
- return JSON.stringify(this._keys)
121
- }
122
- }
123
-
124
- function createKey(key) {
125
- let path = null;
126
- let id = null;
127
- let src = null;
128
- let weight = 1;
129
- let getFn = null;
130
-
131
- if (isString(key) || isArray(key)) {
132
- src = key;
133
- path = createKeyPath(key);
134
- id = createKeyId(key);
135
- } else {
136
- if (!hasOwn.call(key, 'name')) {
137
- throw new Error(MISSING_KEY_PROPERTY('name'))
138
- }
139
-
140
- const name = key.name;
141
- src = name;
142
-
143
- if (hasOwn.call(key, 'weight')) {
144
- weight = key.weight;
145
-
146
- if (weight <= 0) {
147
- throw new Error(INVALID_KEY_WEIGHT_VALUE(name))
148
- }
149
- }
150
-
151
- path = createKeyPath(name);
152
- id = createKeyId(name);
153
- getFn = key.getFn;
154
- }
155
-
156
- return { path, id, weight, src, getFn }
157
- }
158
-
159
- function createKeyPath(key) {
160
- return isArray(key) ? key : key.split('.')
161
- }
162
-
163
- function createKeyId(key) {
164
- return isArray(key) ? key.join('.') : key
165
- }
166
-
167
- function get(obj, path) {
168
- let list = [];
169
- let arr = false;
170
-
171
- const deepGet = (obj, path, index) => {
172
- if (!isDefined(obj)) {
173
- return
174
- }
175
- if (!path[index]) {
176
- // If there's no path left, we've arrived at the object we care about.
177
- list.push(obj);
178
- } else {
179
- let key = path[index];
180
-
181
- const value = obj[key];
182
-
183
- if (!isDefined(value)) {
184
- return
185
- }
186
-
187
- // If we're at the last value in the path, and if it's a string/number/bool,
188
- // add it to the list
189
- if (
190
- index === path.length - 1 &&
191
- (isString(value) || isNumber(value) || isBoolean(value))
192
- ) {
193
- list.push(toString(value));
194
- } else if (isArray(value)) {
195
- arr = true;
196
- // Search each item in the array.
197
- for (let i = 0, len = value.length; i < len; i += 1) {
198
- deepGet(value[i], path, index + 1);
199
- }
200
- } else if (path.length) {
201
- // An object. Recurse further.
202
- deepGet(value, path, index + 1);
203
- }
204
- }
205
- };
206
-
207
- // Backwards compatibility (since path used to be a string)
208
- deepGet(obj, isString(path) ? path.split('.') : path, 0);
209
-
210
- return arr ? list : list[0]
211
- }
212
-
213
- const MatchOptions = {
214
- // Whether the matches should be included in the result set. When `true`, each record in the result
215
- // set will include the indices of the matched characters.
216
- // These can consequently be used for highlighting purposes.
217
- includeMatches: false,
218
- // When `true`, the matching function will continue to the end of a search pattern even if
219
- // a perfect match has already been located in the string.
220
- findAllMatches: false,
221
- // Minimum number of characters that must be matched before a result is considered a match
222
- minMatchCharLength: 1
223
- };
224
-
225
- const BasicOptions = {
226
- // When `true`, the algorithm continues searching to the end of the input even if a perfect
227
- // match is found before the end of the same input.
228
- isCaseSensitive: false,
229
- // When true, the matching function will continue to the end of a search pattern even if
230
- includeScore: false,
231
- // List of properties that will be searched. This also supports nested properties.
232
- keys: [],
233
- // Whether to sort the result list, by score
234
- shouldSort: true,
235
- // Default sort function: sort by ascending score, ascending index
236
- sortFn: (a, b) =>
237
- a.score === b.score ? (a.idx < b.idx ? -1 : 1) : a.score < b.score ? -1 : 1
238
- };
239
-
240
- const FuzzyOptions = {
241
- // Approximately where in the text is the pattern expected to be found?
242
- location: 0,
243
- // At what point does the match algorithm give up. A threshold of '0.0' requires a perfect match
244
- // (of both letters and location), a threshold of '1.0' would match anything.
245
- threshold: 0.6,
246
- // Determines how close the match must be to the fuzzy location (specified above).
247
- // An exact letter match which is 'distance' characters away from the fuzzy location
248
- // would score as a complete mismatch. A distance of '0' requires the match be at
249
- // the exact location specified, a threshold of '1000' would require a perfect match
250
- // to be within 800 characters of the fuzzy location to be found using a 0.8 threshold.
251
- distance: 100
252
- };
253
-
254
- const AdvancedOptions = {
255
- // When `true`, it enables the use of unix-like search commands
256
- useExtendedSearch: false,
257
- // The get function to use when fetching an object's properties.
258
- // The default will search nested paths *ie foo.bar.baz*
259
- getFn: get,
260
- // When `true`, search will ignore `location` and `distance`, so it won't matter
261
- // where in the string the pattern appears.
262
- // More info: https://fusejs.io/concepts/scoring-theory.html#fuzziness-score
263
- ignoreLocation: false,
264
- // When `true`, the calculation for the relevance score (used for sorting) will
265
- // ignore the field-length norm.
266
- // More info: https://fusejs.io/concepts/scoring-theory.html#field-length-norm
267
- ignoreFieldNorm: false,
268
- // The weight to determine how much field length norm effects scoring.
269
- fieldNormWeight: 1
270
- };
271
-
272
- var Config = {
273
- ...BasicOptions,
274
- ...MatchOptions,
275
- ...FuzzyOptions,
276
- ...AdvancedOptions
277
- };
278
-
279
- const SPACE = /[^ ]+/g;
280
-
281
- // Field-length norm: the shorter the field, the higher the weight.
282
- // Set to 3 decimals to reduce index size.
283
- function norm(weight = 1, mantissa = 3) {
284
- const cache = new Map();
285
- const m = Math.pow(10, mantissa);
286
-
287
- return {
288
- get(value) {
289
- const numTokens = value.match(SPACE).length;
290
-
291
- if (cache.has(numTokens)) {
292
- return cache.get(numTokens)
293
- }
294
-
295
- // Default function is 1/sqrt(x), weight makes that variable
296
- const norm = 1 / Math.pow(numTokens, 0.5 * weight);
297
-
298
- // In place of `toFixed(mantissa)`, for faster computation
299
- const n = parseFloat(Math.round(norm * m) / m);
300
-
301
- cache.set(numTokens, n);
302
-
303
- return n
304
- },
305
- clear() {
306
- cache.clear();
307
- }
308
- }
309
- }
310
-
311
- class FuseIndex {
312
- constructor({
313
- getFn = Config.getFn,
314
- fieldNormWeight = Config.fieldNormWeight
315
- } = {}) {
316
- this.norm = norm(fieldNormWeight, 3);
317
- this.getFn = getFn;
318
- this.isCreated = false;
319
-
320
- this.setIndexRecords();
321
- }
322
- setSources(docs = []) {
323
- this.docs = docs;
324
- }
325
- setIndexRecords(records = []) {
326
- this.records = records;
327
- }
328
- setKeys(keys = []) {
329
- this.keys = keys;
330
- this._keysMap = {};
331
- keys.forEach((key, idx) => {
332
- this._keysMap[key.id] = idx;
333
- });
334
- }
335
- create() {
336
- if (this.isCreated || !this.docs.length) {
337
- return
338
- }
339
-
340
- this.isCreated = true;
341
-
342
- // List is Array<String>
343
- if (isString(this.docs[0])) {
344
- this.docs.forEach((doc, docIndex) => {
345
- this._addString(doc, docIndex);
346
- });
347
- } else {
348
- // List is Array<Object>
349
- this.docs.forEach((doc, docIndex) => {
350
- this._addObject(doc, docIndex);
351
- });
352
- }
353
-
354
- this.norm.clear();
355
- }
356
- // Adds a doc to the end of the index
357
- add(doc) {
358
- const idx = this.size();
359
-
360
- if (isString(doc)) {
361
- this._addString(doc, idx);
362
- } else {
363
- this._addObject(doc, idx);
364
- }
365
- }
366
- // Removes the doc at the specified index of the index
367
- removeAt(idx) {
368
- this.records.splice(idx, 1);
369
-
370
- // Change ref index of every subsquent doc
371
- for (let i = idx, len = this.size(); i < len; i += 1) {
372
- this.records[i].i -= 1;
373
- }
374
- }
375
- getValueForItemAtKeyId(item, keyId) {
376
- return item[this._keysMap[keyId]]
377
- }
378
- size() {
379
- return this.records.length
380
- }
381
- _addString(doc, docIndex) {
382
- if (!isDefined(doc) || isBlank(doc)) {
383
- return
384
- }
385
-
386
- let record = {
387
- v: doc,
388
- i: docIndex,
389
- n: this.norm.get(doc)
390
- };
391
-
392
- this.records.push(record);
393
- }
394
- _addObject(doc, docIndex) {
395
- let record = { i: docIndex, $: {} };
396
-
397
- // Iterate over every key (i.e, path), and fetch the value at that key
398
- this.keys.forEach((key, keyIndex) => {
399
- let value = key.getFn ? key.getFn(doc) : this.getFn(doc, key.path);
400
-
401
- if (!isDefined(value)) {
402
- return
403
- }
404
-
405
- if (isArray(value)) {
406
- let subRecords = [];
407
- const stack = [{ nestedArrIndex: -1, value }];
408
-
409
- while (stack.length) {
410
- const { nestedArrIndex, value } = stack.pop();
411
-
412
- if (!isDefined(value)) {
413
- continue
414
- }
415
-
416
- if (isString(value) && !isBlank(value)) {
417
- let subRecord = {
418
- v: value,
419
- i: nestedArrIndex,
420
- n: this.norm.get(value)
421
- };
422
-
423
- subRecords.push(subRecord);
424
- } else if (isArray(value)) {
425
- value.forEach((item, k) => {
426
- stack.push({
427
- nestedArrIndex: k,
428
- value: item
429
- });
430
- });
431
- } else ;
432
- }
433
- record.$[keyIndex] = subRecords;
434
- } else if (isString(value) && !isBlank(value)) {
435
- let subRecord = {
436
- v: value,
437
- n: this.norm.get(value)
438
- };
439
-
440
- record.$[keyIndex] = subRecord;
441
- }
442
- });
443
-
444
- this.records.push(record);
445
- }
446
- toJSON() {
447
- return {
448
- keys: this.keys,
449
- records: this.records
450
- }
451
- }
452
- }
453
-
454
- function createIndex(
455
- keys,
456
- docs,
457
- { getFn = Config.getFn, fieldNormWeight = Config.fieldNormWeight } = {}
458
- ) {
459
- const myIndex = new FuseIndex({ getFn, fieldNormWeight });
460
- myIndex.setKeys(keys.map(createKey));
461
- myIndex.setSources(docs);
462
- myIndex.create();
463
- return myIndex
464
- }
465
-
466
- function parseIndex(
467
- data,
468
- { getFn = Config.getFn, fieldNormWeight = Config.fieldNormWeight } = {}
469
- ) {
470
- const { keys, records } = data;
471
- const myIndex = new FuseIndex({ getFn, fieldNormWeight });
472
- myIndex.setKeys(keys);
473
- myIndex.setIndexRecords(records);
474
- return myIndex
475
- }
476
-
477
- function computeScore$1(
478
- pattern,
479
- {
480
- errors = 0,
481
- currentLocation = 0,
482
- expectedLocation = 0,
483
- distance = Config.distance,
484
- ignoreLocation = Config.ignoreLocation
485
- } = {}
486
- ) {
487
- const accuracy = errors / pattern.length;
488
-
489
- if (ignoreLocation) {
490
- return accuracy
491
- }
492
-
493
- const proximity = Math.abs(expectedLocation - currentLocation);
494
-
495
- if (!distance) {
496
- // Dodge divide by zero error.
497
- return proximity ? 1.0 : accuracy
498
- }
499
-
500
- return accuracy + proximity / distance
501
- }
502
-
503
- function convertMaskToIndices(
504
- matchmask = [],
505
- minMatchCharLength = Config.minMatchCharLength
506
- ) {
507
- let indices = [];
508
- let start = -1;
509
- let end = -1;
510
- let i = 0;
511
-
512
- for (let len = matchmask.length; i < len; i += 1) {
513
- let match = matchmask[i];
514
- if (match && start === -1) {
515
- start = i;
516
- } else if (!match && start !== -1) {
517
- end = i - 1;
518
- if (end - start + 1 >= minMatchCharLength) {
519
- indices.push([start, end]);
520
- }
521
- start = -1;
522
- }
523
- }
524
-
525
- // (i-1 - start) + 1 => i - start
526
- if (matchmask[i - 1] && i - start >= minMatchCharLength) {
527
- indices.push([start, i - 1]);
528
- }
529
-
530
- return indices
531
- }
532
-
533
- // Machine word size
534
- const MAX_BITS = 32;
535
-
536
- function search(
537
- text,
538
- pattern,
539
- patternAlphabet,
540
- {
541
- location = Config.location,
542
- distance = Config.distance,
543
- threshold = Config.threshold,
544
- findAllMatches = Config.findAllMatches,
545
- minMatchCharLength = Config.minMatchCharLength,
546
- includeMatches = Config.includeMatches,
547
- ignoreLocation = Config.ignoreLocation
548
- } = {}
549
- ) {
550
- if (pattern.length > MAX_BITS) {
551
- throw new Error(PATTERN_LENGTH_TOO_LARGE(MAX_BITS))
552
- }
553
-
554
- const patternLen = pattern.length;
555
- // Set starting location at beginning text and initialize the alphabet.
556
- const textLen = text.length;
557
- // Handle the case when location > text.length
558
- const expectedLocation = Math.max(0, Math.min(location, textLen));
559
- // Highest score beyond which we give up.
560
- let currentThreshold = threshold;
561
- // Is there a nearby exact match? (speedup)
562
- let bestLocation = expectedLocation;
563
-
564
- // Performance: only computer matches when the minMatchCharLength > 1
565
- // OR if `includeMatches` is true.
566
- const computeMatches = minMatchCharLength > 1 || includeMatches;
567
- // A mask of the matches, used for building the indices
568
- const matchMask = computeMatches ? Array(textLen) : [];
569
-
570
- let index;
571
-
572
- // Get all exact matches, here for speed up
573
- while ((index = text.indexOf(pattern, bestLocation)) > -1) {
574
- let score = computeScore$1(pattern, {
575
- currentLocation: index,
576
- expectedLocation,
577
- distance,
578
- ignoreLocation
579
- });
580
-
581
- currentThreshold = Math.min(score, currentThreshold);
582
- bestLocation = index + patternLen;
583
-
584
- if (computeMatches) {
585
- let i = 0;
586
- while (i < patternLen) {
587
- matchMask[index + i] = 1;
588
- i += 1;
589
- }
590
- }
591
- }
592
-
593
- // Reset the best location
594
- bestLocation = -1;
595
-
596
- let lastBitArr = [];
597
- let finalScore = 1;
598
- let binMax = patternLen + textLen;
599
-
600
- const mask = 1 << (patternLen - 1);
601
-
602
- for (let i = 0; i < patternLen; i += 1) {
603
- // Scan for the best match; each iteration allows for one more error.
604
- // Run a binary search to determine how far from the match location we can stray
605
- // at this error level.
606
- let binMin = 0;
607
- let binMid = binMax;
608
-
609
- while (binMin < binMid) {
610
- const score = computeScore$1(pattern, {
611
- errors: i,
612
- currentLocation: expectedLocation + binMid,
613
- expectedLocation,
614
- distance,
615
- ignoreLocation
616
- });
617
-
618
- if (score <= currentThreshold) {
619
- binMin = binMid;
620
- } else {
621
- binMax = binMid;
622
- }
623
-
624
- binMid = Math.floor((binMax - binMin) / 2 + binMin);
625
- }
626
-
627
- // Use the result from this iteration as the maximum for the next.
628
- binMax = binMid;
629
-
630
- let start = Math.max(1, expectedLocation - binMid + 1);
631
- let finish = findAllMatches
632
- ? textLen
633
- : Math.min(expectedLocation + binMid, textLen) + patternLen;
634
-
635
- // Initialize the bit array
636
- let bitArr = Array(finish + 2);
637
-
638
- bitArr[finish + 1] = (1 << i) - 1;
639
-
640
- for (let j = finish; j >= start; j -= 1) {
641
- let currentLocation = j - 1;
642
- let charMatch = patternAlphabet[text.charAt(currentLocation)];
643
-
644
- if (computeMatches) {
645
- // Speed up: quick bool to int conversion (i.e, `charMatch ? 1 : 0`)
646
- matchMask[currentLocation] = +!!charMatch;
647
- }
648
-
649
- // First pass: exact match
650
- bitArr[j] = ((bitArr[j + 1] << 1) | 1) & charMatch;
651
-
652
- // Subsequent passes: fuzzy match
653
- if (i) {
654
- bitArr[j] |=
655
- ((lastBitArr[j + 1] | lastBitArr[j]) << 1) | 1 | lastBitArr[j + 1];
656
- }
657
-
658
- if (bitArr[j] & mask) {
659
- finalScore = computeScore$1(pattern, {
660
- errors: i,
661
- currentLocation,
662
- expectedLocation,
663
- distance,
664
- ignoreLocation
665
- });
666
-
667
- // This match will almost certainly be better than any existing match.
668
- // But check anyway.
669
- if (finalScore <= currentThreshold) {
670
- // Indeed it is
671
- currentThreshold = finalScore;
672
- bestLocation = currentLocation;
673
-
674
- // Already passed `loc`, downhill from here on in.
675
- if (bestLocation <= expectedLocation) {
676
- break
677
- }
678
-
679
- // When passing `bestLocation`, don't exceed our current distance from `expectedLocation`.
680
- start = Math.max(1, 2 * expectedLocation - bestLocation);
681
- }
682
- }
683
- }
684
-
685
- // No hope for a (better) match at greater error levels.
686
- const score = computeScore$1(pattern, {
687
- errors: i + 1,
688
- currentLocation: expectedLocation,
689
- expectedLocation,
690
- distance,
691
- ignoreLocation
692
- });
693
-
694
- if (score > currentThreshold) {
695
- break
696
- }
697
-
698
- lastBitArr = bitArr;
699
- }
700
-
701
- const result = {
702
- isMatch: bestLocation >= 0,
703
- // Count exact matches (those with a score of 0) to be "almost" exact
704
- score: Math.max(0.001, finalScore)
705
- };
706
-
707
- if (computeMatches) {
708
- const indices = convertMaskToIndices(matchMask, minMatchCharLength);
709
- if (!indices.length) {
710
- result.isMatch = false;
711
- } else if (includeMatches) {
712
- result.indices = indices;
713
- }
714
- }
715
-
716
- return result
717
- }
718
-
719
- function createPatternAlphabet(pattern) {
720
- let mask = {};
721
-
722
- for (let i = 0, len = pattern.length; i < len; i += 1) {
723
- const char = pattern.charAt(i);
724
- mask[char] = (mask[char] || 0) | (1 << (len - i - 1));
725
- }
726
-
727
- return mask
728
- }
729
-
730
- class BitapSearch {
731
- constructor(
732
- pattern,
733
- {
734
- location = Config.location,
735
- threshold = Config.threshold,
736
- distance = Config.distance,
737
- includeMatches = Config.includeMatches,
738
- findAllMatches = Config.findAllMatches,
739
- minMatchCharLength = Config.minMatchCharLength,
740
- isCaseSensitive = Config.isCaseSensitive,
741
- ignoreLocation = Config.ignoreLocation
742
- } = {}
743
- ) {
744
- this.options = {
745
- location,
746
- threshold,
747
- distance,
748
- includeMatches,
749
- findAllMatches,
750
- minMatchCharLength,
751
- isCaseSensitive,
752
- ignoreLocation
753
- };
754
-
755
- this.pattern = isCaseSensitive ? pattern : pattern.toLowerCase();
756
-
757
- this.chunks = [];
758
-
759
- if (!this.pattern.length) {
760
- return
761
- }
762
-
763
- const addChunk = (pattern, startIndex) => {
764
- this.chunks.push({
765
- pattern,
766
- alphabet: createPatternAlphabet(pattern),
767
- startIndex
768
- });
769
- };
770
-
771
- const len = this.pattern.length;
772
-
773
- if (len > MAX_BITS) {
774
- let i = 0;
775
- const remainder = len % MAX_BITS;
776
- const end = len - remainder;
777
-
778
- while (i < end) {
779
- addChunk(this.pattern.substr(i, MAX_BITS), i);
780
- i += MAX_BITS;
781
- }
782
-
783
- if (remainder) {
784
- const startIndex = len - MAX_BITS;
785
- addChunk(this.pattern.substr(startIndex), startIndex);
786
- }
787
- } else {
788
- addChunk(this.pattern, 0);
789
- }
790
- }
791
-
792
- searchIn(text) {
793
- const { isCaseSensitive, includeMatches } = this.options;
794
-
795
- if (!isCaseSensitive) {
796
- text = text.toLowerCase();
797
- }
798
-
799
- // Exact match
800
- if (this.pattern === text) {
801
- let result = {
802
- isMatch: true,
803
- score: 0
804
- };
805
-
806
- if (includeMatches) {
807
- result.indices = [[0, text.length - 1]];
808
- }
809
-
810
- return result
811
- }
812
-
813
- // Otherwise, use Bitap algorithm
814
- const {
815
- location,
816
- distance,
817
- threshold,
818
- findAllMatches,
819
- minMatchCharLength,
820
- ignoreLocation
821
- } = this.options;
822
-
823
- let allIndices = [];
824
- let totalScore = 0;
825
- let hasMatches = false;
826
-
827
- this.chunks.forEach(({ pattern, alphabet, startIndex }) => {
828
- const { isMatch, score, indices } = search(text, pattern, alphabet, {
829
- location: location + startIndex,
830
- distance,
831
- threshold,
832
- findAllMatches,
833
- minMatchCharLength,
834
- includeMatches,
835
- ignoreLocation
836
- });
837
-
838
- if (isMatch) {
839
- hasMatches = true;
840
- }
841
-
842
- totalScore += score;
843
-
844
- if (isMatch && indices) {
845
- allIndices = [...allIndices, ...indices];
846
- }
847
- });
848
-
849
- let result = {
850
- isMatch: hasMatches,
851
- score: hasMatches ? totalScore / this.chunks.length : 1
852
- };
853
-
854
- if (hasMatches && includeMatches) {
855
- result.indices = allIndices;
856
- }
857
-
858
- return result
859
- }
860
- }
861
-
862
- class BaseMatch {
863
- constructor(pattern) {
864
- this.pattern = pattern;
865
- }
866
- static isMultiMatch(pattern) {
867
- return getMatch(pattern, this.multiRegex)
868
- }
869
- static isSingleMatch(pattern) {
870
- return getMatch(pattern, this.singleRegex)
871
- }
872
- search(/*text*/) {}
873
- }
874
-
875
- function getMatch(pattern, exp) {
876
- const matches = pattern.match(exp);
877
- return matches ? matches[1] : null
878
- }
879
-
880
- // Token: 'file
881
-
882
- class ExactMatch extends BaseMatch {
883
- constructor(pattern) {
884
- super(pattern);
885
- }
886
- static get type() {
887
- return 'exact'
888
- }
889
- static get multiRegex() {
890
- return /^="(.*)"$/
891
- }
892
- static get singleRegex() {
893
- return /^=(.*)$/
894
- }
895
- search(text) {
896
- const isMatch = text === this.pattern;
897
-
898
- return {
899
- isMatch,
900
- score: isMatch ? 0 : 1,
901
- indices: [0, this.pattern.length - 1]
902
- }
903
- }
904
- }
905
-
906
- // Token: !fire
907
-
908
- class InverseExactMatch extends BaseMatch {
909
- constructor(pattern) {
910
- super(pattern);
911
- }
912
- static get type() {
913
- return 'inverse-exact'
914
- }
915
- static get multiRegex() {
916
- return /^!"(.*)"$/
917
- }
918
- static get singleRegex() {
919
- return /^!(.*)$/
920
- }
921
- search(text) {
922
- const index = text.indexOf(this.pattern);
923
- const isMatch = index === -1;
924
-
925
- return {
926
- isMatch,
927
- score: isMatch ? 0 : 1,
928
- indices: [0, text.length - 1]
929
- }
930
- }
931
- }
932
-
933
- // Token: ^file
934
-
935
- class PrefixExactMatch extends BaseMatch {
936
- constructor(pattern) {
937
- super(pattern);
938
- }
939
- static get type() {
940
- return 'prefix-exact'
941
- }
942
- static get multiRegex() {
943
- return /^\^"(.*)"$/
944
- }
945
- static get singleRegex() {
946
- return /^\^(.*)$/
947
- }
948
- search(text) {
949
- const isMatch = text.startsWith(this.pattern);
950
-
951
- return {
952
- isMatch,
953
- score: isMatch ? 0 : 1,
954
- indices: [0, this.pattern.length - 1]
955
- }
956
- }
957
- }
958
-
959
- // Token: !^fire
960
-
961
- class InversePrefixExactMatch extends BaseMatch {
962
- constructor(pattern) {
963
- super(pattern);
964
- }
965
- static get type() {
966
- return 'inverse-prefix-exact'
967
- }
968
- static get multiRegex() {
969
- return /^!\^"(.*)"$/
970
- }
971
- static get singleRegex() {
972
- return /^!\^(.*)$/
973
- }
974
- search(text) {
975
- const isMatch = !text.startsWith(this.pattern);
976
-
977
- return {
978
- isMatch,
979
- score: isMatch ? 0 : 1,
980
- indices: [0, text.length - 1]
981
- }
982
- }
983
- }
984
-
985
- // Token: .file$
986
-
987
- class SuffixExactMatch extends BaseMatch {
988
- constructor(pattern) {
989
- super(pattern);
990
- }
991
- static get type() {
992
- return 'suffix-exact'
993
- }
994
- static get multiRegex() {
995
- return /^"(.*)"\$$/
996
- }
997
- static get singleRegex() {
998
- return /^(.*)\$$/
999
- }
1000
- search(text) {
1001
- const isMatch = text.endsWith(this.pattern);
1002
-
1003
- return {
1004
- isMatch,
1005
- score: isMatch ? 0 : 1,
1006
- indices: [text.length - this.pattern.length, text.length - 1]
1007
- }
1008
- }
1009
- }
1010
-
1011
- // Token: !.file$
1012
-
1013
- class InverseSuffixExactMatch extends BaseMatch {
1014
- constructor(pattern) {
1015
- super(pattern);
1016
- }
1017
- static get type() {
1018
- return 'inverse-suffix-exact'
1019
- }
1020
- static get multiRegex() {
1021
- return /^!"(.*)"\$$/
1022
- }
1023
- static get singleRegex() {
1024
- return /^!(.*)\$$/
1025
- }
1026
- search(text) {
1027
- const isMatch = !text.endsWith(this.pattern);
1028
- return {
1029
- isMatch,
1030
- score: isMatch ? 0 : 1,
1031
- indices: [0, text.length - 1]
1032
- }
1033
- }
1034
- }
1035
-
1036
- class FuzzyMatch extends BaseMatch {
1037
- constructor(
1038
- pattern,
1039
- {
1040
- location = Config.location,
1041
- threshold = Config.threshold,
1042
- distance = Config.distance,
1043
- includeMatches = Config.includeMatches,
1044
- findAllMatches = Config.findAllMatches,
1045
- minMatchCharLength = Config.minMatchCharLength,
1046
- isCaseSensitive = Config.isCaseSensitive,
1047
- ignoreLocation = Config.ignoreLocation
1048
- } = {}
1049
- ) {
1050
- super(pattern);
1051
- this._bitapSearch = new BitapSearch(pattern, {
1052
- location,
1053
- threshold,
1054
- distance,
1055
- includeMatches,
1056
- findAllMatches,
1057
- minMatchCharLength,
1058
- isCaseSensitive,
1059
- ignoreLocation
1060
- });
1061
- }
1062
- static get type() {
1063
- return 'fuzzy'
1064
- }
1065
- static get multiRegex() {
1066
- return /^"(.*)"$/
1067
- }
1068
- static get singleRegex() {
1069
- return /^(.*)$/
1070
- }
1071
- search(text) {
1072
- return this._bitapSearch.searchIn(text)
1073
- }
1074
- }
1075
-
1076
- // Token: 'file
1077
-
1078
- class IncludeMatch extends BaseMatch {
1079
- constructor(pattern) {
1080
- super(pattern);
1081
- }
1082
- static get type() {
1083
- return 'include'
1084
- }
1085
- static get multiRegex() {
1086
- return /^'"(.*)"$/
1087
- }
1088
- static get singleRegex() {
1089
- return /^'(.*)$/
1090
- }
1091
- search(text) {
1092
- let location = 0;
1093
- let index;
1094
-
1095
- const indices = [];
1096
- const patternLen = this.pattern.length;
1097
-
1098
- // Get all exact matches
1099
- while ((index = text.indexOf(this.pattern, location)) > -1) {
1100
- location = index + patternLen;
1101
- indices.push([index, location - 1]);
1102
- }
1103
-
1104
- const isMatch = !!indices.length;
1105
-
1106
- return {
1107
- isMatch,
1108
- score: isMatch ? 0 : 1,
1109
- indices
1110
- }
1111
- }
1112
- }
1113
-
1114
- // ❗Order is important. DO NOT CHANGE.
1115
- const searchers = [
1116
- ExactMatch,
1117
- IncludeMatch,
1118
- PrefixExactMatch,
1119
- InversePrefixExactMatch,
1120
- InverseSuffixExactMatch,
1121
- SuffixExactMatch,
1122
- InverseExactMatch,
1123
- FuzzyMatch
1124
- ];
1125
-
1126
- const searchersLen = searchers.length;
1127
-
1128
- // Regex to split by spaces, but keep anything in quotes together
1129
- const SPACE_RE = / +(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/;
1130
- const OR_TOKEN = '|';
1131
-
1132
- // Return a 2D array representation of the query, for simpler parsing.
1133
- // Example:
1134
- // "^core go$ | rb$ | py$ xy$" => [["^core", "go$"], ["rb$"], ["py$", "xy$"]]
1135
- function parseQuery(pattern, options = {}) {
1136
- return pattern.split(OR_TOKEN).map((item) => {
1137
- let query = item
1138
- .trim()
1139
- .split(SPACE_RE)
1140
- .filter((item) => item && !!item.trim());
1141
-
1142
- let results = [];
1143
- for (let i = 0, len = query.length; i < len; i += 1) {
1144
- const queryItem = query[i];
1145
-
1146
- // 1. Handle multiple query match (i.e, once that are quoted, like `"hello world"`)
1147
- let found = false;
1148
- let idx = -1;
1149
- while (!found && ++idx < searchersLen) {
1150
- const searcher = searchers[idx];
1151
- let token = searcher.isMultiMatch(queryItem);
1152
- if (token) {
1153
- results.push(new searcher(token, options));
1154
- found = true;
1155
- }
1156
- }
1157
-
1158
- if (found) {
1159
- continue
1160
- }
1161
-
1162
- // 2. Handle single query matches (i.e, once that are *not* quoted)
1163
- idx = -1;
1164
- while (++idx < searchersLen) {
1165
- const searcher = searchers[idx];
1166
- let token = searcher.isSingleMatch(queryItem);
1167
- if (token) {
1168
- results.push(new searcher(token, options));
1169
- break
1170
- }
1171
- }
1172
- }
1173
-
1174
- return results
1175
- })
1176
- }
1177
-
1178
- // These extended matchers can return an array of matches, as opposed
1179
- // to a singl match
1180
- const MultiMatchSet = new Set([FuzzyMatch.type, IncludeMatch.type]);
1181
-
1182
- /**
1183
- * Command-like searching
1184
- * ======================
1185
- *
1186
- * Given multiple search terms delimited by spaces.e.g. `^jscript .python$ ruby !java`,
1187
- * search in a given text.
1188
- *
1189
- * Search syntax:
1190
- *
1191
- * | Token | Match type | Description |
1192
- * | ----------- | -------------------------- | -------------------------------------- |
1193
- * | `jscript` | fuzzy-match | Items that fuzzy match `jscript` |
1194
- * | `=scheme` | exact-match | Items that are `scheme` |
1195
- * | `'python` | include-match | Items that include `python` |
1196
- * | `!ruby` | inverse-exact-match | Items that do not include `ruby` |
1197
- * | `^java` | prefix-exact-match | Items that start with `java` |
1198
- * | `!^earlang` | inverse-prefix-exact-match | Items that do not start with `earlang` |
1199
- * | `.js$` | suffix-exact-match | Items that end with `.js` |
1200
- * | `!.go$` | inverse-suffix-exact-match | Items that do not end with `.go` |
1201
- *
1202
- * A single pipe character acts as an OR operator. For example, the following
1203
- * query matches entries that start with `core` and end with either`go`, `rb`,
1204
- * or`py`.
1205
- *
1206
- * ```
1207
- * ^core go$ | rb$ | py$
1208
- * ```
1209
- */
1210
- class ExtendedSearch {
1211
- constructor(
1212
- pattern,
1213
- {
1214
- isCaseSensitive = Config.isCaseSensitive,
1215
- includeMatches = Config.includeMatches,
1216
- minMatchCharLength = Config.minMatchCharLength,
1217
- ignoreLocation = Config.ignoreLocation,
1218
- findAllMatches = Config.findAllMatches,
1219
- location = Config.location,
1220
- threshold = Config.threshold,
1221
- distance = Config.distance
1222
- } = {}
1223
- ) {
1224
- this.query = null;
1225
- this.options = {
1226
- isCaseSensitive,
1227
- includeMatches,
1228
- minMatchCharLength,
1229
- findAllMatches,
1230
- ignoreLocation,
1231
- location,
1232
- threshold,
1233
- distance
1234
- };
1235
-
1236
- this.pattern = isCaseSensitive ? pattern : pattern.toLowerCase();
1237
- this.query = parseQuery(this.pattern, this.options);
1238
- }
1239
-
1240
- static condition(_, options) {
1241
- return options.useExtendedSearch
1242
- }
1243
-
1244
- searchIn(text) {
1245
- const query = this.query;
1246
-
1247
- if (!query) {
1248
- return {
1249
- isMatch: false,
1250
- score: 1
1251
- }
1252
- }
1253
-
1254
- const { includeMatches, isCaseSensitive } = this.options;
1255
-
1256
- text = isCaseSensitive ? text : text.toLowerCase();
1257
-
1258
- let numMatches = 0;
1259
- let allIndices = [];
1260
- let totalScore = 0;
1261
-
1262
- // ORs
1263
- for (let i = 0, qLen = query.length; i < qLen; i += 1) {
1264
- const searchers = query[i];
1265
-
1266
- // Reset indices
1267
- allIndices.length = 0;
1268
- numMatches = 0;
1269
-
1270
- // ANDs
1271
- for (let j = 0, pLen = searchers.length; j < pLen; j += 1) {
1272
- const searcher = searchers[j];
1273
- const { isMatch, indices, score } = searcher.search(text);
1274
-
1275
- if (isMatch) {
1276
- numMatches += 1;
1277
- totalScore += score;
1278
- if (includeMatches) {
1279
- const type = searcher.constructor.type;
1280
- if (MultiMatchSet.has(type)) {
1281
- allIndices = [...allIndices, ...indices];
1282
- } else {
1283
- allIndices.push(indices);
1284
- }
1285
- }
1286
- } else {
1287
- totalScore = 0;
1288
- numMatches = 0;
1289
- allIndices.length = 0;
1290
- break
1291
- }
1292
- }
1293
-
1294
- // OR condition, so if TRUE, return
1295
- if (numMatches) {
1296
- let result = {
1297
- isMatch: true,
1298
- score: totalScore / numMatches
1299
- };
1300
-
1301
- if (includeMatches) {
1302
- result.indices = allIndices;
1303
- }
1304
-
1305
- return result
1306
- }
1307
- }
1308
-
1309
- // Nothing was matched
1310
- return {
1311
- isMatch: false,
1312
- score: 1
1313
- }
1314
- }
1315
- }
1316
-
1317
- const registeredSearchers = [];
1318
-
1319
- function register(...args) {
1320
- registeredSearchers.push(...args);
1321
- }
1322
-
1323
- function createSearcher(pattern, options) {
1324
- for (let i = 0, len = registeredSearchers.length; i < len; i += 1) {
1325
- let searcherClass = registeredSearchers[i];
1326
- if (searcherClass.condition(pattern, options)) {
1327
- return new searcherClass(pattern, options)
1328
- }
1329
- }
1330
-
1331
- return new BitapSearch(pattern, options)
1332
- }
1333
-
1334
- const LogicalOperator = {
1335
- AND: '$and',
1336
- OR: '$or'
1337
- };
1338
-
1339
- const KeyType = {
1340
- PATH: '$path',
1341
- PATTERN: '$val'
1342
- };
1343
-
1344
- const isExpression = (query) =>
1345
- !!(query[LogicalOperator.AND] || query[LogicalOperator.OR]);
1346
-
1347
- const isPath = (query) => !!query[KeyType.PATH];
1348
-
1349
- const isLeaf = (query) =>
1350
- !isArray(query) && isObject(query) && !isExpression(query);
1351
-
1352
- const convertToExplicit = (query) => ({
1353
- [LogicalOperator.AND]: Object.keys(query).map((key) => ({
1354
- [key]: query[key]
1355
- }))
1356
- });
1357
-
1358
- // When `auto` is `true`, the parse function will infer and initialize and add
1359
- // the appropriate `Searcher` instance
1360
- function parse(query, options, { auto = true } = {}) {
1361
- const next = (query) => {
1362
- let keys = Object.keys(query);
1363
-
1364
- const isQueryPath = isPath(query);
1365
-
1366
- if (!isQueryPath && keys.length > 1 && !isExpression(query)) {
1367
- return next(convertToExplicit(query))
1368
- }
1369
-
1370
- if (isLeaf(query)) {
1371
- const key = isQueryPath ? query[KeyType.PATH] : keys[0];
1372
-
1373
- const pattern = isQueryPath ? query[KeyType.PATTERN] : query[key];
1374
-
1375
- if (!isString(pattern)) {
1376
- throw new Error(LOGICAL_SEARCH_INVALID_QUERY_FOR_KEY(key))
1377
- }
1378
-
1379
- const obj = {
1380
- keyId: createKeyId(key),
1381
- pattern
1382
- };
1383
-
1384
- if (auto) {
1385
- obj.searcher = createSearcher(pattern, options);
1386
- }
1387
-
1388
- return obj
1389
- }
1390
-
1391
- let node = {
1392
- children: [],
1393
- operator: keys[0]
1394
- };
1395
-
1396
- keys.forEach((key) => {
1397
- const value = query[key];
1398
-
1399
- if (isArray(value)) {
1400
- value.forEach((item) => {
1401
- node.children.push(next(item));
1402
- });
1403
- }
1404
- });
1405
-
1406
- return node
1407
- };
1408
-
1409
- if (!isExpression(query)) {
1410
- query = convertToExplicit(query);
1411
- }
1412
-
1413
- return next(query)
1414
- }
1415
-
1416
- // Practical scoring function
1417
- function computeScore(
1418
- results,
1419
- { ignoreFieldNorm = Config.ignoreFieldNorm }
1420
- ) {
1421
- results.forEach((result) => {
1422
- let totalScore = 1;
1423
-
1424
- result.matches.forEach(({ key, norm, score }) => {
1425
- const weight = key ? key.weight : null;
1426
-
1427
- totalScore *= Math.pow(
1428
- score === 0 && weight ? Number.EPSILON : score,
1429
- (weight || 1) * (ignoreFieldNorm ? 1 : norm)
1430
- );
1431
- });
1432
-
1433
- result.score = totalScore;
1434
- });
1435
- }
1436
-
1437
- function transformMatches(result, data) {
1438
- const matches = result.matches;
1439
- data.matches = [];
1440
-
1441
- if (!isDefined(matches)) {
1442
- return
1443
- }
1444
-
1445
- matches.forEach((match) => {
1446
- if (!isDefined(match.indices) || !match.indices.length) {
1447
- return
1448
- }
1449
-
1450
- const { indices, value } = match;
1451
-
1452
- let obj = {
1453
- indices,
1454
- value
1455
- };
1456
-
1457
- if (match.key) {
1458
- obj.key = match.key.src;
1459
- }
1460
-
1461
- if (match.idx > -1) {
1462
- obj.refIndex = match.idx;
1463
- }
1464
-
1465
- data.matches.push(obj);
1466
- });
1467
- }
1468
-
1469
- function transformScore(result, data) {
1470
- data.score = result.score;
1471
- }
1472
-
1473
- function format(
1474
- results,
1475
- docs,
1476
- {
1477
- includeMatches = Config.includeMatches,
1478
- includeScore = Config.includeScore
1479
- } = {}
1480
- ) {
1481
- const transformers = [];
1482
-
1483
- if (includeMatches) transformers.push(transformMatches);
1484
- if (includeScore) transformers.push(transformScore);
1485
-
1486
- return results.map((result) => {
1487
- const { idx } = result;
1488
-
1489
- const data = {
1490
- item: docs[idx],
1491
- refIndex: idx
1492
- };
1493
-
1494
- if (transformers.length) {
1495
- transformers.forEach((transformer) => {
1496
- transformer(result, data);
1497
- });
1498
- }
1499
-
1500
- return data
1501
- })
1502
- }
1503
-
1504
- class Fuse {
1505
- constructor(docs, options = {}, index) {
1506
- this.options = { ...Config, ...options };
1507
-
1508
- if (
1509
- this.options.useExtendedSearch &&
1510
- !true
1511
- ) {
1512
- throw new Error(EXTENDED_SEARCH_UNAVAILABLE)
1513
- }
1514
-
1515
- this._keyStore = new KeyStore(this.options.keys);
1516
-
1517
- this.setCollection(docs, index);
1518
- }
1519
-
1520
- setCollection(docs, index) {
1521
- this._docs = docs;
1522
-
1523
- if (index && !(index instanceof FuseIndex)) {
1524
- throw new Error(INCORRECT_INDEX_TYPE)
1525
- }
1526
-
1527
- this._myIndex =
1528
- index ||
1529
- createIndex(this.options.keys, this._docs, {
1530
- getFn: this.options.getFn,
1531
- fieldNormWeight: this.options.fieldNormWeight
1532
- });
1533
- }
1534
-
1535
- add(doc) {
1536
- if (!isDefined(doc)) {
1537
- return
1538
- }
1539
-
1540
- this._docs.push(doc);
1541
- this._myIndex.add(doc);
1542
- }
1543
-
1544
- remove(predicate = (/* doc, idx */) => false) {
1545
- const results = [];
1546
-
1547
- for (let i = 0, len = this._docs.length; i < len; i += 1) {
1548
- const doc = this._docs[i];
1549
- if (predicate(doc, i)) {
1550
- this.removeAt(i);
1551
- i -= 1;
1552
- len -= 1;
1553
-
1554
- results.push(doc);
1555
- }
1556
- }
1557
-
1558
- return results
1559
- }
1560
-
1561
- removeAt(idx) {
1562
- this._docs.splice(idx, 1);
1563
- this._myIndex.removeAt(idx);
1564
- }
1565
-
1566
- getIndex() {
1567
- return this._myIndex
1568
- }
1569
-
1570
- search(query, { limit = -1 } = {}) {
1571
- const {
1572
- includeMatches,
1573
- includeScore,
1574
- shouldSort,
1575
- sortFn,
1576
- ignoreFieldNorm
1577
- } = this.options;
1578
-
1579
- let results = isString(query)
1580
- ? isString(this._docs[0])
1581
- ? this._searchStringList(query)
1582
- : this._searchObjectList(query)
1583
- : this._searchLogical(query);
1584
-
1585
- computeScore(results, { ignoreFieldNorm });
1586
-
1587
- if (shouldSort) {
1588
- results.sort(sortFn);
1589
- }
1590
-
1591
- if (isNumber(limit) && limit > -1) {
1592
- results = results.slice(0, limit);
1593
- }
1594
-
1595
- return format(results, this._docs, {
1596
- includeMatches,
1597
- includeScore
1598
- })
1599
- }
1600
-
1601
- _searchStringList(query) {
1602
- const searcher = createSearcher(query, this.options);
1603
- const { records } = this._myIndex;
1604
- const results = [];
1605
-
1606
- // Iterate over every string in the index
1607
- records.forEach(({ v: text, i: idx, n: norm }) => {
1608
- if (!isDefined(text)) {
1609
- return
1610
- }
1611
-
1612
- const { isMatch, score, indices } = searcher.searchIn(text);
1613
-
1614
- if (isMatch) {
1615
- results.push({
1616
- item: text,
1617
- idx,
1618
- matches: [{ score, value: text, norm, indices }]
1619
- });
1620
- }
1621
- });
1622
-
1623
- return results
1624
- }
1625
-
1626
- _searchLogical(query) {
1627
-
1628
- const expression = parse(query, this.options);
1629
-
1630
- const evaluate = (node, item, idx) => {
1631
- if (!node.children) {
1632
- const { keyId, searcher } = node;
1633
-
1634
- const matches = this._findMatches({
1635
- key: this._keyStore.get(keyId),
1636
- value: this._myIndex.getValueForItemAtKeyId(item, keyId),
1637
- searcher
1638
- });
1639
-
1640
- if (matches && matches.length) {
1641
- return [
1642
- {
1643
- idx,
1644
- item,
1645
- matches
1646
- }
1647
- ]
1648
- }
1649
-
1650
- return []
1651
- }
1652
-
1653
- const res = [];
1654
- for (let i = 0, len = node.children.length; i < len; i += 1) {
1655
- const child = node.children[i];
1656
- const result = evaluate(child, item, idx);
1657
- if (result.length) {
1658
- res.push(...result);
1659
- } else if (node.operator === LogicalOperator.AND) {
1660
- return []
1661
- }
1662
- }
1663
- return res
1664
- };
1665
-
1666
- const records = this._myIndex.records;
1667
- const resultMap = {};
1668
- const results = [];
1669
-
1670
- records.forEach(({ $: item, i: idx }) => {
1671
- if (isDefined(item)) {
1672
- let expResults = evaluate(expression, item, idx);
1673
-
1674
- if (expResults.length) {
1675
- // Dedupe when adding
1676
- if (!resultMap[idx]) {
1677
- resultMap[idx] = { idx, item, matches: [] };
1678
- results.push(resultMap[idx]);
1679
- }
1680
- expResults.forEach(({ matches }) => {
1681
- resultMap[idx].matches.push(...matches);
1682
- });
1683
- }
1684
- }
1685
- });
1686
-
1687
- return results
1688
- }
1689
-
1690
- _searchObjectList(query) {
1691
- const searcher = createSearcher(query, this.options);
1692
- const { keys, records } = this._myIndex;
1693
- const results = [];
1694
-
1695
- // List is Array<Object>
1696
- records.forEach(({ $: item, i: idx }) => {
1697
- if (!isDefined(item)) {
1698
- return
1699
- }
1700
-
1701
- let matches = [];
1702
-
1703
- // Iterate over every key (i.e, path), and fetch the value at that key
1704
- keys.forEach((key, keyIndex) => {
1705
- matches.push(
1706
- ...this._findMatches({
1707
- key,
1708
- value: item[keyIndex],
1709
- searcher
1710
- })
1711
- );
1712
- });
1713
-
1714
- if (matches.length) {
1715
- results.push({
1716
- idx,
1717
- item,
1718
- matches
1719
- });
1720
- }
1721
- });
1722
-
1723
- return results
1724
- }
1725
- _findMatches({ key, value, searcher }) {
1726
- if (!isDefined(value)) {
1727
- return []
1728
- }
1729
-
1730
- let matches = [];
1731
-
1732
- if (isArray(value)) {
1733
- value.forEach(({ v: text, i: idx, n: norm }) => {
1734
- if (!isDefined(text)) {
1735
- return
1736
- }
1737
-
1738
- const { isMatch, score, indices } = searcher.searchIn(text);
1739
-
1740
- if (isMatch) {
1741
- matches.push({
1742
- score,
1743
- key,
1744
- value: text,
1745
- idx,
1746
- norm,
1747
- indices
1748
- });
1749
- }
1750
- });
1751
- } else {
1752
- const { v: text, n: norm } = value;
1753
-
1754
- const { isMatch, score, indices } = searcher.searchIn(text);
1755
-
1756
- if (isMatch) {
1757
- matches.push({ score, key, value: text, norm, indices });
1758
- }
1759
- }
1760
-
1761
- return matches
1762
- }
1763
- }
1764
-
1765
- Fuse.version = '7.0.0';
1766
- Fuse.createIndex = createIndex;
1767
- Fuse.parseIndex = parseIndex;
1768
- Fuse.config = Config;
1769
-
1770
- {
1771
- Fuse.parseQuery = parse;
1772
- }
1773
-
1774
- {
1775
- register(ExtendedSearch);
1776
- }
1777
-
1778
- export { Fuse as default };
1779
- //# sourceMappingURL=fuse.js.map