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