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