@itwin/appui-abstract 4.3.0-dev.9 → 4.3.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 (84) hide show
  1. package/CHANGELOG.md +40 -1
  2. package/lib/cjs/appui-abstract/UiAdmin.d.ts.map +1 -1
  3. package/lib/cjs/appui-abstract/UiAdmin.js +5 -4
  4. package/lib/cjs/appui-abstract/UiAdmin.js.map +1 -1
  5. package/lib/cjs/appui-abstract/UiItemsManager.d.ts.map +1 -1
  6. package/lib/cjs/appui-abstract/UiItemsManager.js +4 -4
  7. package/lib/cjs/appui-abstract/UiItemsManager.js.map +1 -1
  8. package/lib/cjs/appui-abstract/common/KeyboardKey.d.ts +3 -3
  9. package/lib/cjs/appui-abstract/common/KeyboardKey.d.ts.map +1 -1
  10. package/lib/cjs/appui-abstract/common/KeyboardKey.js +4 -3
  11. package/lib/cjs/appui-abstract/common/KeyboardKey.js.map +1 -1
  12. package/lib/cjs/appui-abstract/utils/IconSpecUtilities.d.ts +3 -3
  13. package/lib/cjs/appui-abstract/utils/IconSpecUtilities.d.ts.map +1 -1
  14. package/lib/cjs/appui-abstract/utils/IconSpecUtilities.js +4 -3
  15. package/lib/cjs/appui-abstract/utils/IconSpecUtilities.js.map +1 -1
  16. package/lib/cjs/appui-abstract/utils/UiError.d.ts +1 -1
  17. package/lib/cjs/appui-abstract/utils/UiError.js +1 -1
  18. package/lib/cjs/appui-abstract/utils/UiError.js.map +1 -1
  19. package/lib/cjs/appui-abstract/utils/filter/filters.d.ts +2 -80
  20. package/lib/cjs/appui-abstract/utils/filter/filters.d.ts.map +1 -1
  21. package/lib/cjs/appui-abstract/utils/filter/filters.js +13 -621
  22. package/lib/cjs/appui-abstract/utils/filter/filters.js.map +1 -1
  23. package/lib/cjs/appui-abstract/utils/misc.d.ts +1 -4
  24. package/lib/cjs/appui-abstract/utils/misc.d.ts.map +1 -1
  25. package/lib/cjs/appui-abstract/utils/misc.js +2 -10
  26. package/lib/cjs/appui-abstract/utils/misc.js.map +1 -1
  27. package/lib/cjs/appui-abstract.d.ts +0 -2
  28. package/lib/cjs/appui-abstract.d.ts.map +1 -1
  29. package/lib/cjs/appui-abstract.js +0 -2
  30. package/lib/cjs/appui-abstract.js.map +1 -1
  31. package/lib/esm/appui-abstract/UiAdmin.d.ts.map +1 -1
  32. package/lib/esm/appui-abstract/UiAdmin.js +6 -5
  33. package/lib/esm/appui-abstract/UiAdmin.js.map +1 -1
  34. package/lib/esm/appui-abstract/UiItemsManager.d.ts.map +1 -1
  35. package/lib/esm/appui-abstract/UiItemsManager.js +4 -4
  36. package/lib/esm/appui-abstract/UiItemsManager.js.map +1 -1
  37. package/lib/esm/appui-abstract/common/KeyboardKey.d.ts +3 -3
  38. package/lib/esm/appui-abstract/common/KeyboardKey.d.ts.map +1 -1
  39. package/lib/esm/appui-abstract/common/KeyboardKey.js +4 -3
  40. package/lib/esm/appui-abstract/common/KeyboardKey.js.map +1 -1
  41. package/lib/esm/appui-abstract/utils/IconSpecUtilities.d.ts +3 -3
  42. package/lib/esm/appui-abstract/utils/IconSpecUtilities.d.ts.map +1 -1
  43. package/lib/esm/appui-abstract/utils/IconSpecUtilities.js +4 -3
  44. package/lib/esm/appui-abstract/utils/IconSpecUtilities.js.map +1 -1
  45. package/lib/esm/appui-abstract/utils/UiError.d.ts +1 -1
  46. package/lib/esm/appui-abstract/utils/UiError.js +1 -1
  47. package/lib/esm/appui-abstract/utils/UiError.js.map +1 -1
  48. package/lib/esm/appui-abstract/utils/filter/filters.d.ts +2 -80
  49. package/lib/esm/appui-abstract/utils/filter/filters.d.ts.map +1 -1
  50. package/lib/esm/appui-abstract/utils/filter/filters.js +12 -609
  51. package/lib/esm/appui-abstract/utils/filter/filters.js.map +1 -1
  52. package/lib/esm/appui-abstract/utils/misc.d.ts +1 -4
  53. package/lib/esm/appui-abstract/utils/misc.d.ts.map +1 -1
  54. package/lib/esm/appui-abstract/utils/misc.js +1 -8
  55. package/lib/esm/appui-abstract/utils/misc.js.map +1 -1
  56. package/lib/esm/appui-abstract.d.ts +0 -2
  57. package/lib/esm/appui-abstract.d.ts.map +1 -1
  58. package/lib/esm/appui-abstract.js +0 -2
  59. package/lib/esm/appui-abstract.js.map +1 -1
  60. package/package.json +5 -6
  61. package/lib/cjs/appui-abstract/utils/filter/charCode.d.ts +0 -417
  62. package/lib/cjs/appui-abstract/utils/filter/charCode.d.ts.map +0 -1
  63. package/lib/cjs/appui-abstract/utils/filter/charCode.js +0 -7
  64. package/lib/cjs/appui-abstract/utils/filter/charCode.js.map +0 -1
  65. package/lib/cjs/appui-abstract/utils/filter/map.d.ts +0 -60
  66. package/lib/cjs/appui-abstract/utils/filter/map.d.ts.map +0 -1
  67. package/lib/cjs/appui-abstract/utils/filter/map.js +0 -403
  68. package/lib/cjs/appui-abstract/utils/filter/map.js.map +0 -1
  69. package/lib/cjs/appui-abstract/utils/filter/strings.d.ts +0 -21
  70. package/lib/cjs/appui-abstract/utils/filter/strings.d.ts.map +0 -1
  71. package/lib/cjs/appui-abstract/utils/filter/strings.js +0 -79
  72. package/lib/cjs/appui-abstract/utils/filter/strings.js.map +0 -1
  73. package/lib/esm/appui-abstract/utils/filter/charCode.d.ts +0 -417
  74. package/lib/esm/appui-abstract/utils/filter/charCode.d.ts.map +0 -1
  75. package/lib/esm/appui-abstract/utils/filter/charCode.js +0 -6
  76. package/lib/esm/appui-abstract/utils/filter/charCode.js.map +0 -1
  77. package/lib/esm/appui-abstract/utils/filter/map.d.ts +0 -60
  78. package/lib/esm/appui-abstract/utils/filter/map.d.ts.map +0 -1
  79. package/lib/esm/appui-abstract/utils/filter/map.js +0 -398
  80. package/lib/esm/appui-abstract/utils/filter/map.js.map +0 -1
  81. package/lib/esm/appui-abstract/utils/filter/strings.d.ts +0 -21
  82. package/lib/esm/appui-abstract/utils/filter/strings.d.ts.map +0 -1
  83. package/lib/esm/appui-abstract/utils/filter/strings.js +0 -71
  84. package/lib/esm/appui-abstract/utils/filter/strings.js.map +0 -1
@@ -5,100 +5,18 @@
5
5
  /** @packageDocumentation
6
6
  * @module Utilities
7
7
  */
8
- import { LRUCache } from "./map";
9
- import * as strings from "./strings";
10
- // Combined filters
11
- /**
12
- * @returns A filter which combines the provided set
13
- * of filters with an or. The *first* filters that
14
- * matches defined the return value of the returned
15
- * filter.
16
- * @internal
17
- */
18
- export function or(...filter) {
19
- return function (word, wordToMatchAgainst) {
20
- for (let i = 0, len = filter.length; i < len; i++) {
21
- const match = filter[i](word, wordToMatchAgainst);
22
- if (match) {
23
- return match;
24
- }
25
- }
26
- return null;
27
- };
28
- }
29
- // Prefix
30
- /** @internal */
31
- export const matchesStrictPrefix = _matchesPrefix.bind(undefined, false);
32
- /** @internal */
33
- export const matchesPrefix = _matchesPrefix.bind(undefined, true);
34
- function _matchesPrefix(ignoreCase, word, wordToMatchAgainst) {
35
- if (!wordToMatchAgainst || wordToMatchAgainst.length < word.length) {
36
- return null;
37
- }
38
- let matches;
39
- if (ignoreCase) {
40
- matches = strings.startsWithIgnoreCase(wordToMatchAgainst, word);
41
- }
42
- else {
43
- matches = wordToMatchAgainst.indexOf(word) === 0;
44
- }
45
- if (!matches) {
46
- return null;
47
- }
48
- return word.length > 0 ? [{ start: 0, end: word.length }] : [];
49
- }
50
- // Contiguous Substring
51
- /** @internal */
52
- export function matchesContiguousSubString(word, wordToMatchAgainst) {
53
- const index = wordToMatchAgainst.toLowerCase().indexOf(word.toLowerCase());
54
- if (index === -1) {
55
- return null;
56
- }
57
- return [{ start: index, end: index + word.length }];
58
- }
59
- // Substring
60
- /**
61
- * Return case insensitive substring matches
62
- * @param word filter string
63
- * @param wordToMatchAgainst string to test
64
- * @internal
65
- */
66
- export function matchesSubString(word, wordToMatchAgainst) {
67
- return _matchesSubString(word.toLowerCase(), wordToMatchAgainst.toLowerCase(), 0, 0);
68
- }
69
- function _matchesSubString(word, wordToMatchAgainst, i, j) {
70
- if (i === word.length) {
71
- return [];
72
- }
73
- else if (j === wordToMatchAgainst.length) {
74
- return null;
75
- }
76
- else {
77
- if (word[i] === wordToMatchAgainst[j]) {
78
- let result = null;
79
- if (result = _matchesSubString(word, wordToMatchAgainst, i + 1, j + 1)) {
80
- return join({ start: j, end: j + 1 }, result);
81
- }
82
- return null;
83
- }
84
- return _matchesSubString(word, wordToMatchAgainst, i, j + 1);
85
- }
86
- }
87
- // CamelCase
88
- function isLower(code) {
89
- return 97 /* CharCode.a */ <= code && code <= 122 /* CharCode.z */;
90
- }
91
- function isUpper(code) {
92
- return 65 /* CharCode.A */ <= code && code <= 90 /* CharCode.Z */;
93
- }
94
- function isNumber(code) {
95
- return 48 /* CharCode.Digit0 */ <= code && code <= 57 /* CharCode.Digit9 */;
96
- }
8
+ var CharCode;
9
+ (function (CharCode) {
10
+ CharCode[CharCode["Space"] = 32] = "Space";
11
+ CharCode[CharCode["Tab"] = 9] = "Tab";
12
+ CharCode[CharCode["LineFeed"] = 10] = "LineFeed";
13
+ CharCode[CharCode["CarriageReturn"] = 13] = "CarriageReturn";
14
+ })(CharCode || (CharCode = {}));
97
15
  function isWhitespace(code) {
98
- return (code === 32 /* CharCode.Space */
99
- || code === 9 /* CharCode.Tab */
100
- || code === 10 /* CharCode.LineFeed */
101
- || code === 13 /* CharCode.CarriageReturn */);
16
+ return (code === CharCode.Space
17
+ || code === CharCode.Tab
18
+ || code === CharCode.LineFeed
19
+ || code === CharCode.CarriageReturn);
102
20
  }
103
21
  const wordSeparators = new Set();
104
22
  '`~!@#$%^&*()-=+[{]}\\|;:\'",.<>/?'
@@ -110,9 +28,6 @@ function isWordSeparator(code) {
110
28
  function charactersMatch(codeA, codeB) {
111
29
  return (codeA === codeB) || (isWordSeparator(codeA) && isWordSeparator(codeB));
112
30
  }
113
- function isAlphanumeric(code) {
114
- return isLower(code) || isUpper(code) || isNumber(code);
115
- }
116
31
  function join(head, tail) {
117
32
  if (tail.length === 0) {
118
33
  tail = [head];
@@ -125,128 +40,6 @@ function join(head, tail) {
125
40
  }
126
41
  return tail;
127
42
  }
128
- function nextAnchor(camelCaseWord, start) {
129
- for (let i = start; i < camelCaseWord.length; i++) {
130
- const c = camelCaseWord.charCodeAt(i);
131
- if (isUpper(c) || isNumber(c) || (i > 0 && !isAlphanumeric(camelCaseWord.charCodeAt(i - 1)))) {
132
- return i;
133
- }
134
- }
135
- return camelCaseWord.length;
136
- }
137
- function _matchesCamelCase(word, camelCaseWord, i, j) {
138
- if (i === word.length) {
139
- return [];
140
- }
141
- else if (j === camelCaseWord.length) {
142
- return null;
143
- }
144
- else if (word[i] !== camelCaseWord[j].toLowerCase()) {
145
- return null;
146
- }
147
- else {
148
- let result = null;
149
- let nextUpperIndex = j + 1;
150
- result = _matchesCamelCase(word, camelCaseWord, i + 1, j + 1);
151
- while (!result && (nextUpperIndex = nextAnchor(camelCaseWord, nextUpperIndex)) < camelCaseWord.length) {
152
- result = _matchesCamelCase(word, camelCaseWord, i + 1, nextUpperIndex);
153
- nextUpperIndex++;
154
- }
155
- return result === null ? null : join({ start: j, end: j + 1 }, result);
156
- }
157
- }
158
- // Heuristic to avoid computing camel case matcher for words that don't
159
- // look like camelCaseWords.
160
- function analyzeCamelCaseWord(word) {
161
- let upper = 0, lower = 0, alpha = 0, numeric = 0, code = 0;
162
- for (let i = 0; i < word.length; i++) {
163
- code = word.charCodeAt(i);
164
- if (isUpper(code)) {
165
- upper++;
166
- }
167
- if (isLower(code)) {
168
- lower++;
169
- }
170
- if (isAlphanumeric(code)) {
171
- alpha++;
172
- }
173
- if (isNumber(code)) {
174
- numeric++;
175
- }
176
- }
177
- const upperPercent = upper / word.length;
178
- const lowerPercent = lower / word.length;
179
- const alphaPercent = alpha / word.length;
180
- const numericPercent = numeric / word.length;
181
- return { upperPercent, lowerPercent, alphaPercent, numericPercent };
182
- }
183
- function isUpperCaseWord(analysis) {
184
- const { upperPercent, lowerPercent } = analysis;
185
- return lowerPercent === 0 && upperPercent > 0.6;
186
- }
187
- function isCamelCaseWord(analysis) {
188
- const { upperPercent, lowerPercent, alphaPercent, numericPercent } = analysis;
189
- return lowerPercent > 0.2 && upperPercent < 0.8 && alphaPercent > 0.6 && numericPercent < 0.2;
190
- }
191
- // Heuristic to avoid computing camel case matcher for words that don't
192
- // look like camel case patterns.
193
- function isCamelCasePattern(word) {
194
- let upper = 0, lower = 0, code = 0, whitespace = 0;
195
- for (let i = 0; i < word.length; i++) {
196
- code = word.charCodeAt(i);
197
- if (isUpper(code)) {
198
- upper++;
199
- }
200
- if (isLower(code)) {
201
- lower++;
202
- }
203
- if (isWhitespace(code)) {
204
- whitespace++;
205
- }
206
- }
207
- if ((upper === 0 || lower === 0) && whitespace === 0) {
208
- return word.length <= 30;
209
- }
210
- else {
211
- return upper <= 5;
212
- }
213
- }
214
- /**
215
- * Return matches treating "camelCase" words separately. For example the filter string "gp" would return two matches in string "Git Pull".
216
- * @param word filter string
217
- * @param wordToMatchAgainst string to test
218
- * @internal
219
- */
220
- export function matchesCamelCase(word, camelCaseWord) {
221
- if (!camelCaseWord) {
222
- return null;
223
- }
224
- camelCaseWord = camelCaseWord.trim();
225
- if (camelCaseWord.length === 0) {
226
- return null;
227
- }
228
- if (!isCamelCasePattern(camelCaseWord)) {
229
- return null;
230
- }
231
- if (camelCaseWord.length > 60) {
232
- return null;
233
- }
234
- const analysis = analyzeCamelCaseWord(camelCaseWord);
235
- if (!isCamelCaseWord(analysis)) {
236
- // istanbul ignore next
237
- if (!isUpperCaseWord(analysis)) {
238
- return null;
239
- }
240
- camelCaseWord = camelCaseWord.toLowerCase();
241
- }
242
- let result = null;
243
- let i = 0;
244
- word = word.toLowerCase();
245
- while (i < camelCaseWord.length && (result = _matchesCamelCase(word, camelCaseWord, 0, i)) === null) {
246
- i = nextAnchor(camelCaseWord, i + 1);
247
- }
248
- return result;
249
- }
250
43
  /**
251
44
  * Matches beginning of words supporting non-ASCII languages.
252
45
  * @param word Filter string
@@ -254,7 +47,7 @@ export function matchesCamelCase(word, camelCaseWord) {
254
47
  * @param contiguous - If true the filter string must be found "contiguous" in the searched string (E.g. "pul" will match "Git: Pull").
255
48
  * Otherwise also matches sub string of the word with beginnings of the words in the target (e.g. "gp" or "g p" will match "Git: Pull").
256
49
  * Useful in cases where the target is words (e.g. command labels)
257
- * @internal
50
+ * @internal @deprecated in 4.3. Use `matchesWords` from @itwin/core-react instead. Though internal, this requires deprecation due to known public usage in @itwin/core-react.
258
51
  */
259
52
  export function matchesWords(word, target, contiguous = false) {
260
53
  if (!target || target.length === 0) {
@@ -302,394 +95,4 @@ function nextWord(word, start) {
302
95
  }
303
96
  return word.length;
304
97
  }
305
- // Fuzzy
306
- const fuzzyContiguousFilter = or(matchesPrefix, matchesCamelCase, matchesContiguousSubString);
307
- const fuzzySeparateFilter = or(matchesPrefix, matchesCamelCase, matchesSubString);
308
- const fuzzyRegExpCache = new LRUCache(10000); // bounded to 10000 elements
309
- /**
310
- * Match pattern against word in a fuzzy way. This will only return a single match.
311
- * @internal
312
- */
313
- export function matchesFuzzy(word, wordToMatchAgainst, enableSeparateSubstringMatching = false) {
314
- // istanbul ignore next
315
- if (typeof word !== "string" || typeof wordToMatchAgainst !== "string") {
316
- return null; // return early for invalid input
317
- }
318
- // Form RegExp for wildcard matches
319
- let regexp = fuzzyRegExpCache.get(word);
320
- if (!regexp) {
321
- regexp = new RegExp(strings.convertSimple2RegExpPattern(word), "i");
322
- fuzzyRegExpCache.set(word, regexp);
323
- }
324
- // RegExp Filter
325
- const match = regexp.exec(wordToMatchAgainst);
326
- if (match) {
327
- return [{ start: match.index, end: match.index + match[0].length }];
328
- }
329
- // Default Filter
330
- return enableSeparateSubstringMatching ? /* istanbul ignore next */ fuzzySeparateFilter(word, wordToMatchAgainst) : fuzzyContiguousFilter(word, wordToMatchAgainst);
331
- }
332
- /**
333
- * Match pattern against word in a fuzzy way. As in IntelliSense and faster and more
334
- * powerful than `matchesFuzzy`. This will only return a single match.
335
- * @internal
336
- */
337
- export function matchesFuzzy2(pattern, word) {
338
- const score = fuzzyScore(pattern, pattern.toLowerCase(), 0, word, word.toLowerCase(), 0, true);
339
- return score ? createMatches(score) : null;
340
- }
341
- // #region --- fuzzyScore ---
342
- /**
343
- * @internal
344
- */
345
- export function createMatches(score) {
346
- // istanbul ignore next
347
- if (typeof score === "undefined") {
348
- return [];
349
- }
350
- const matches = score[1].toString(2);
351
- const wordStart = score[2];
352
- const res = [];
353
- for (let pos = wordStart; pos < _maxLen; pos++) {
354
- if (matches[matches.length - (pos + 1)] === "1") {
355
- const last = res[res.length - 1];
356
- if (last && last.end === pos) {
357
- last.end = pos + 1;
358
- }
359
- else {
360
- res.push({ start: pos, end: pos + 1 });
361
- }
362
- }
363
- }
364
- return res;
365
- }
366
- const _maxLen = 128;
367
- function initTable() {
368
- const table = [];
369
- const row = [0];
370
- for (let i = 1; i <= _maxLen; i++) {
371
- row.push(-i);
372
- }
373
- for (let i = 0; i <= _maxLen; i++) {
374
- const thisRow = row.slice(0);
375
- thisRow[0] = -i;
376
- table.push(thisRow);
377
- }
378
- return table;
379
- }
380
- const _table = initTable();
381
- const _scores = initTable();
382
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
383
- const _arrows = initTable();
384
- const _debug = false;
385
- function isSeparatorAtPos(value, index) {
386
- if (index < 0 || index >= value.length) {
387
- return false;
388
- }
389
- const code = value.charCodeAt(index);
390
- switch (code) {
391
- case 95 /* CharCode.Underline */:
392
- case 45 /* CharCode.Dash */:
393
- case 46 /* CharCode.Period */:
394
- case 32 /* CharCode.Space */:
395
- case 47 /* CharCode.Slash */:
396
- case 92 /* CharCode.Backslash */:
397
- case 39 /* CharCode.SingleQuote */:
398
- case 34 /* CharCode.DoubleQuote */:
399
- case 58 /* CharCode.Colon */:
400
- case 36 /* CharCode.DollarSign */:
401
- return true;
402
- default:
403
- return false;
404
- }
405
- }
406
- function isWhitespaceAtPos(value, index) {
407
- if (index < 0 || index >= value.length) {
408
- return false;
409
- }
410
- const code = value.charCodeAt(index);
411
- switch (code) {
412
- // istanbul ignore next
413
- case 32 /* CharCode.Space */:
414
- case 9 /* CharCode.Tab */:
415
- return true;
416
- default:
417
- return false;
418
- }
419
- }
420
- function isUpperCaseAtPos(pos, word, wordLow) {
421
- return word[pos] !== wordLow[pos];
422
- }
423
- /** @internal */
424
- export function isPatternInWord(patternLow, patternPos, patternLen, wordLow, wordPos, wordLen) {
425
- while (patternPos < patternLen && wordPos < wordLen) {
426
- if (patternLow[patternPos] === wordLow[wordPos]) {
427
- patternPos += 1;
428
- }
429
- wordPos += 1;
430
- }
431
- return patternPos === patternLen; // pattern must be exhausted
432
- }
433
- /** @internal */
434
- export var FuzzyScore;
435
- (function (FuzzyScore) {
436
- /**
437
- * No matches and value `-100`
438
- * @internal
439
- */
440
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
441
- FuzzyScore.Default = Object.freeze([-100, 0, 0]);
442
- // istanbul ignore next
443
- /** @internal */
444
- function isDefault(score) {
445
- return !score || (score[0] === -100 && score[1] === 0 && score[2] === 0);
446
- }
447
- FuzzyScore.isDefault = isDefault;
448
- })(FuzzyScore || (FuzzyScore = {}));
449
- /** @internal */
450
- export function fuzzyScore(pattern, patternLow, patternStart, word, wordLow, wordStart, firstMatchCanBeWeak) {
451
- // istanbul ignore next
452
- const patternLen = pattern.length > _maxLen ? _maxLen : pattern.length;
453
- const wordLen = word.length > _maxLen ? _maxLen : word.length;
454
- if (patternStart >= patternLen || wordStart >= wordLen || (patternLen - patternStart) > (wordLen - wordStart)) {
455
- return undefined;
456
- }
457
- // Run a simple check if the characters of pattern occur
458
- // (in order) at all in word. If that isn't the case we
459
- // stop because no match will be possible
460
- if (!isPatternInWord(patternLow, patternStart, patternLen, wordLow, wordStart, wordLen)) {
461
- return undefined;
462
- }
463
- let row = 1;
464
- let column = 1;
465
- let patternPos = patternStart;
466
- let wordPos = wordStart;
467
- let hasStrongFirstMatch = false;
468
- // There will be a match, fill in tables
469
- for (row = 1, patternPos = patternStart; patternPos < patternLen; row++, patternPos++) {
470
- for (column = 1, wordPos = wordStart; wordPos < wordLen; column++, wordPos++) {
471
- const score = _doScore(pattern, patternLow, patternPos, patternStart, word, wordLow, wordPos);
472
- if (patternPos === patternStart && score > 1) {
473
- hasStrongFirstMatch = true;
474
- }
475
- _scores[row][column] = score;
476
- const diag = _table[row - 1][column - 1] + (score > 1 ? 1 : score);
477
- const top = _table[row - 1][column] + -1;
478
- const left = _table[row][column - 1] + -1;
479
- if (left >= top) {
480
- // left or diag
481
- if (left > diag) {
482
- _table[row][column] = left;
483
- _arrows[row][column] = 4 /* Arrow.Left */;
484
- }
485
- else if (left === diag) {
486
- _table[row][column] = left;
487
- _arrows[row][column] = 4 /* Arrow.Left */ | 2 /* Arrow.Diag */;
488
- }
489
- else {
490
- _table[row][column] = diag;
491
- _arrows[row][column] = 2 /* Arrow.Diag */;
492
- }
493
- }
494
- else {
495
- // top or diag
496
- if (top > diag) {
497
- _table[row][column] = top;
498
- _arrows[row][column] = 1 /* Arrow.Top */;
499
- }
500
- else if (top === diag) {
501
- _table[row][column] = top;
502
- _arrows[row][column] = 1 /* Arrow.Top */ | 2 /* Arrow.Diag */;
503
- }
504
- else {
505
- _table[row][column] = diag;
506
- _arrows[row][column] = 2 /* Arrow.Diag */;
507
- }
508
- }
509
- }
510
- }
511
- if (!hasStrongFirstMatch && !firstMatchCanBeWeak) {
512
- return undefined;
513
- }
514
- _matchesCount = 0;
515
- _topScore = -100;
516
- _wordStart = wordStart;
517
- _firstMatchCanBeWeak = firstMatchCanBeWeak;
518
- _findAllMatches2(row - 1, column - 1, patternLen === wordLen ? 1 : 0, 0, false);
519
- if (_matchesCount === 0) {
520
- return undefined;
521
- }
522
- return [_topScore, _topMatch2, wordStart];
523
- }
524
- function _doScore(pattern, patternLow, patternPos, patternStart, word, wordLow, wordPos) {
525
- if (patternLow[patternPos] !== wordLow[wordPos]) {
526
- return -1;
527
- }
528
- if (wordPos === (patternPos - patternStart)) {
529
- // common prefix: `foobar <-> foobaz`
530
- // ^^^^^
531
- if (pattern[patternPos] === word[wordPos]) {
532
- return 7;
533
- }
534
- else {
535
- return 5;
536
- }
537
- }
538
- else if (isUpperCaseAtPos(wordPos, word, wordLow) && (wordPos === 0 || !isUpperCaseAtPos(wordPos - 1, word, wordLow))) {
539
- // hitting upper-case: `foo <-> forOthers`
540
- // ^^ ^
541
- if (pattern[patternPos] === word[wordPos]) {
542
- return 7;
543
- }
544
- else {
545
- return 5;
546
- }
547
- }
548
- else if (isSeparatorAtPos(wordLow, wordPos) && (wordPos === 0 || !isSeparatorAtPos(wordLow, wordPos - 1))) {
549
- // hitting a separator: `. <-> foo.bar`
550
- // ^
551
- return 5;
552
- }
553
- else if (isSeparatorAtPos(wordLow, wordPos - 1) || isWhitespaceAtPos(wordLow, wordPos - 1)) {
554
- // post separator: `foo <-> bar_foo`
555
- // ^^^
556
- return 5;
557
- }
558
- else {
559
- return 1;
560
- }
561
- }
562
- let _matchesCount = 0;
563
- let _topMatch2 = 0;
564
- let _topScore = 0;
565
- let _wordStart = 0;
566
- let _firstMatchCanBeWeak = false;
567
- function _findAllMatches2(row, column, total, matches, lastMatched) {
568
- if (_matchesCount >= 10 || total < -25) {
569
- // stop when having already 10 results, or
570
- // when a potential alignment as already 5 gaps
571
- return;
572
- }
573
- let simpleMatchCount = 0;
574
- while (row > 0 && column > 0) {
575
- const score = _scores[row][column];
576
- const arrow = _arrows[row][column];
577
- if (arrow === 4 /* Arrow.Left */) {
578
- // left -> no match, skip a word character
579
- column -= 1;
580
- if (lastMatched) {
581
- total -= 5; // new gap penalty
582
- }
583
- else if (matches !== 0) {
584
- total -= 1; // gap penalty after first match
585
- }
586
- lastMatched = false;
587
- simpleMatchCount = 0;
588
- }
589
- else {
590
- /* istanbul ignore else */
591
- if (arrow & 2 /* Arrow.Diag */) {
592
- if (arrow & 4 /* Arrow.Left */) {
593
- // left
594
- _findAllMatches2(row, column - 1, matches !== 0 ? total - 1 : total, // gap penalty after first match
595
- matches, lastMatched);
596
- }
597
- // diag
598
- total += score;
599
- row -= 1;
600
- column -= 1;
601
- lastMatched = true;
602
- // match -> set a 1 at the word pos
603
- matches += 2 ** (column + _wordStart);
604
- // count simple matches and boost a row of
605
- // simple matches when they yield in a
606
- // strong match.
607
- if (score === 1) {
608
- simpleMatchCount += 1;
609
- if (row === 0 && !_firstMatchCanBeWeak) {
610
- // when the first match is a weak
611
- // match we discard it
612
- return undefined;
613
- }
614
- }
615
- else {
616
- // boost
617
- total += 1 + (simpleMatchCount * (score - 1));
618
- simpleMatchCount = 0;
619
- }
620
- }
621
- else {
622
- // istanbul ignore next
623
- return undefined;
624
- }
625
- }
626
- }
627
- total -= column >= 3 ? 9 : column * 3; // late start penalty
628
- // dynamically keep track of the current top score
629
- // and insert the current best score at head, the rest at tail
630
- _matchesCount += 1;
631
- if (total > _topScore) {
632
- _topScore = total;
633
- _topMatch2 = matches;
634
- }
635
- }
636
- // #endregion
637
- // #region --- graceful ---
638
- /** @internal */
639
- export function fuzzyScoreGracefulAggressive(pattern, lowPattern, patternPos, word, lowWord, wordPos, firstMatchCanBeWeak) {
640
- return fuzzyScoreWithPermutations(pattern, lowPattern, patternPos, word, lowWord, wordPos, true, firstMatchCanBeWeak);
641
- }
642
- /** @internal */
643
- export function fuzzyScoreGraceful(pattern, lowPattern, patternPos, word, lowWord, wordPos, firstMatchCanBeWeak) {
644
- return fuzzyScoreWithPermutations(pattern, lowPattern, patternPos, word, lowWord, wordPos, false, firstMatchCanBeWeak);
645
- }
646
- function fuzzyScoreWithPermutations(pattern, lowPattern, patternPos, word, lowWord, wordPos, aggressive, firstMatchCanBeWeak) {
647
- let top = fuzzyScore(pattern, lowPattern, patternPos, word, lowWord, wordPos, firstMatchCanBeWeak);
648
- if (top && !aggressive) {
649
- // when using the original pattern yield a result we`
650
- // return it unless we are aggressive and try to find
651
- // a better alignment, e.g. `cno` -> `^co^ns^ole` or `^c^o^nsole`.
652
- return top;
653
- }
654
- // istanbul ignore else
655
- if (pattern.length >= 3) {
656
- // When the pattern is long enough then try a few (max 7)
657
- // permutations of the pattern to find a better match. The
658
- // permutations only swap neighbouring characters, e.g
659
- // `cnoso` becomes `conso`, `cnsoo`, `cnoos`.
660
- const tries = Math.min(7, pattern.length - 1);
661
- for (let movingPatternPos = patternPos + 1; movingPatternPos < tries; movingPatternPos++) {
662
- const newPattern = nextTypoPermutation(pattern, movingPatternPos);
663
- // istanbul ignore else
664
- if (newPattern) {
665
- const candidate = fuzzyScore(newPattern, newPattern.toLowerCase(), patternPos, word, lowWord, wordPos, firstMatchCanBeWeak);
666
- if (candidate) {
667
- candidate[0] -= 3; // permutation penalty
668
- // istanbul ignore else
669
- if (!top || candidate[0] > top[0]) {
670
- top = candidate;
671
- }
672
- }
673
- }
674
- }
675
- }
676
- return top;
677
- }
678
- function nextTypoPermutation(pattern, patternPos) {
679
- // istanbul ignore next
680
- if (patternPos + 1 >= pattern.length) {
681
- return undefined;
682
- }
683
- const swap1 = pattern[patternPos];
684
- const swap2 = pattern[patternPos + 1];
685
- // istanbul ignore next
686
- if (swap1 === swap2) {
687
- return undefined;
688
- }
689
- return pattern.slice(0, patternPos)
690
- + swap2
691
- + swap1
692
- + pattern.slice(patternPos + 2);
693
- }
694
- // #endregion
695
98
  //# sourceMappingURL=filters.js.map