@opensumi/ide-utils 2.16.6

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