@jsenv/test 3.6.3 → 3.6.5

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.
@@ -41,6 +41,13 @@ const applyPattern = ({ url, pattern }) => {
41
41
  };
42
42
  };
43
43
 
44
+ /**
45
+ * Core pattern matching function that processes patterns against strings
46
+ * @param {string} pattern - The pattern with special syntax (*,**,/) to match against
47
+ * @param {string} string - The string to test against the pattern
48
+ * @returns {Object} Result containing match status and capture groups
49
+ * @private
50
+ */
44
51
  const applyMatching = (pattern, string) => {
45
52
  const groups = [];
46
53
  let patternIndex = 0;
@@ -65,22 +72,6 @@ const applyMatching = (pattern, string) => {
65
72
  return consumeString(remainingString.length);
66
73
  };
67
74
 
68
- let matched;
69
- const iterate = () => {
70
- const patternIndexBefore = patternIndex;
71
- const indexBefore = index;
72
- matched = matchOne();
73
- if (matched === undefined) {
74
- consumePattern(1);
75
- consumeString(1);
76
- iterate();
77
- return;
78
- }
79
- if (matched === false && restoreIndexes) {
80
- patternIndex = patternIndexBefore;
81
- index = indexBefore;
82
- }
83
- };
84
75
  const matchOne = () => {
85
76
  // pattern consumed
86
77
  if (remainingPattern === "") {
@@ -90,12 +81,11 @@ const applyMatching = (pattern, string) => {
90
81
  if (remainingString[0] === "?") {
91
82
  // match search params
92
83
  consumeRemainingString();
93
-
94
84
  return true;
95
85
  }
96
- // if remainingString
97
86
  return false; // fails because string longer than expect
98
87
  }
88
+
99
89
  // -- from this point pattern is not consumed --
100
90
  // string consumed, pattern not consumed
101
91
  if (remainingString === "") {
@@ -109,6 +99,7 @@ const applyMatching = (pattern, string) => {
109
99
  }
110
100
  return false; // fail because string shorter than expect
111
101
  }
102
+
112
103
  // -- from this point pattern and string are not consumed --
113
104
  // fast path trailing slash
114
105
  if (remainingPattern === "/") {
@@ -120,25 +111,30 @@ const applyMatching = (pattern, string) => {
120
111
  }
121
112
  return false;
122
113
  }
114
+
123
115
  // fast path trailing '**'
124
116
  if (remainingPattern === "**") {
125
117
  consumePattern(2);
126
118
  consumeRemainingString();
127
119
  return true;
128
120
  }
121
+
129
122
  if (remainingPattern.slice(0, 4) === "/**/") {
130
123
  consumePattern(3); // consumes "/**/"
131
- const skipResult = skipUntilMatch({
124
+ const skipResult = skipUntilMatchIterative({
132
125
  pattern: remainingPattern,
133
126
  string: remainingString,
134
127
  canSkipSlash: true,
135
128
  });
136
- groups.push(...skipResult.groups);
129
+ for (let i = 0; i < skipResult.groups.length; i++) {
130
+ groups.push(skipResult.groups[i]);
131
+ }
137
132
  consumePattern(skipResult.patternIndex);
138
133
  consumeRemainingString();
139
134
  restoreIndexes = false;
140
135
  return skipResult.matched;
141
136
  }
137
+
142
138
  // pattern leading **
143
139
  if (remainingPattern.slice(0, 2) === "**") {
144
140
  consumePattern(2); // consumes "**"
@@ -148,7 +144,7 @@ const applyMatching = (pattern, string) => {
148
144
  // when remainingPattern was preceeded by "**/"
149
145
  // and remainingString have no "/"
150
146
  // then skip is not allowed, a regular match will be performed
151
- if (!remainingString.includes("/")) {
147
+ if (remainingString.includes("/")) {
152
148
  skipAllowed = false;
153
149
  }
154
150
  }
@@ -158,18 +154,21 @@ const applyMatching = (pattern, string) => {
158
154
  return true;
159
155
  }
160
156
  if (skipAllowed) {
161
- const skipResult = skipUntilMatch({
157
+ const skipResult = skipUntilMatchIterative({
162
158
  pattern: remainingPattern,
163
159
  string: remainingString,
164
160
  canSkipSlash: true,
165
161
  });
166
- groups.push(...skipResult.groups);
162
+ for (let i = 0; i < skipResult.groups.length; i++) {
163
+ groups.push(skipResult.groups[i]);
164
+ }
167
165
  consumePattern(skipResult.patternIndex);
168
166
  consumeRemainingString();
169
167
  restoreIndexes = false;
170
168
  return skipResult.matched;
171
169
  }
172
170
  }
171
+
173
172
  if (remainingPattern[0] === "*") {
174
173
  consumePattern(1); // consumes "*"
175
174
  if (remainingPattern === "") {
@@ -189,23 +188,53 @@ const applyMatching = (pattern, string) => {
189
188
  patternIndex = patternIndex - 1;
190
189
  return false;
191
190
  }
192
- const skipResult = skipUntilMatch({
191
+ const skipResult = skipUntilMatchIterative({
193
192
  pattern: remainingPattern,
194
193
  string: remainingString,
195
194
  canSkipSlash: false,
196
195
  });
197
- groups.push(skipResult.group, ...skipResult.groups);
196
+ groups.push(skipResult.group);
197
+ for (let i = 0; i < skipResult.groups.length; i++) {
198
+ groups.push(skipResult.groups[i]);
199
+ }
198
200
  consumePattern(skipResult.patternIndex);
199
201
  consumeString(skipResult.index);
200
202
  restoreIndexes = false;
201
203
  return skipResult.matched;
202
204
  }
205
+
203
206
  if (remainingPattern[0] !== remainingString[0]) {
204
207
  return false;
205
208
  }
209
+
206
210
  return undefined;
207
211
  };
208
- iterate();
212
+
213
+ // Replace recursive iterate() with iterative approach
214
+ let matched;
215
+ let continueIteration = true;
216
+
217
+ while (continueIteration) {
218
+ const patternIndexBefore = patternIndex;
219
+ const indexBefore = index;
220
+
221
+ matched = matchOne();
222
+
223
+ if (matched === undefined) {
224
+ consumePattern(1);
225
+ consumeString(1);
226
+ // Continue the loop instead of recursion
227
+ continue;
228
+ }
229
+
230
+ if (matched === false && restoreIndexes) {
231
+ patternIndex = patternIndexBefore;
232
+ index = indexBefore;
233
+ }
234
+
235
+ // End the loop
236
+ continueIteration = false;
237
+ }
209
238
 
210
239
  return {
211
240
  matched,
@@ -215,7 +244,15 @@ const applyMatching = (pattern, string) => {
215
244
  };
216
245
  };
217
246
 
218
- const skipUntilMatch = ({ pattern, string, canSkipSlash }) => {
247
+ /**
248
+ * Iterative version of skipUntilMatch that avoids recursion
249
+ * @param {Object} params
250
+ * @param {string} params.pattern - The pattern to match
251
+ * @param {string} params.string - The string to test against
252
+ * @param {boolean} params.canSkipSlash - Whether slash characters can be skipped
253
+ * @returns {Object} Result of the matching attempt
254
+ */
255
+ const skipUntilMatchIterative = ({ pattern, string, canSkipSlash }) => {
219
256
  let index = 0;
220
257
  let remainingString = string;
221
258
  let longestAttemptRange = null;
@@ -233,8 +270,10 @@ const skipUntilMatch = ({ pattern, string, canSkipSlash }) => {
233
270
  };
234
271
  };
235
272
 
236
- const tryToMatch = () => {
273
+ // Loop until a match is found or all attempts fail
274
+ while (true) {
237
275
  const matchAttempt = applyMatching(pattern, remainingString);
276
+
238
277
  if (matchAttempt.matched) {
239
278
  return {
240
279
  matched: true,
@@ -249,6 +288,7 @@ const skipUntilMatch = ({ pattern, string, canSkipSlash }) => {
249
288
  },
250
289
  };
251
290
  }
291
+
252
292
  const attemptIndex = matchAttempt.index;
253
293
  const attemptRange = {
254
294
  patternIndex: matchAttempt.patternIndex,
@@ -256,19 +296,23 @@ const skipUntilMatch = ({ pattern, string, canSkipSlash }) => {
256
296
  length: attemptIndex,
257
297
  groups: matchAttempt.groups,
258
298
  };
299
+
259
300
  if (
260
301
  !longestAttemptRange ||
261
302
  longestAttemptRange.length < attemptRange.length
262
303
  ) {
263
304
  longestAttemptRange = attemptRange;
264
305
  }
306
+
265
307
  if (isLastAttempt) {
266
308
  return failure();
267
309
  }
310
+
268
311
  const nextIndex = attemptIndex + 1;
269
312
  if (nextIndex >= remainingString.length) {
270
313
  return failure();
271
314
  }
315
+
272
316
  if (remainingString[0] === "/") {
273
317
  if (!canSkipSlash) {
274
318
  return failure();
@@ -278,12 +322,11 @@ const skipUntilMatch = ({ pattern, string, canSkipSlash }) => {
278
322
  isLastAttempt = true;
279
323
  }
280
324
  }
325
+
281
326
  // search against the next unattempted string
282
327
  index += nextIndex;
283
328
  remainingString = remainingString.slice(nextIndex);
284
- return tryToMatch();
285
- };
286
- return tryToMatch();
329
+ }
287
330
  };
288
331
 
289
332
  const applyPatternMatching = ({ url, pattern }) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/test",
3
- "version": "3.6.3",
3
+ "version": "3.6.5",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -41,15 +41,15 @@
41
41
  "dependencies": {
42
42
  "@c88/v8-coverage": "0.1.1",
43
43
  "@jsenv/abort": "4.3.1",
44
- "@jsenv/ast": "6.6.2",
45
- "@jsenv/exception": "1.1.5",
46
- "@jsenv/filesystem": "4.14.2",
47
- "@jsenv/github-check-run": "0.2.16",
44
+ "@jsenv/ast": "6.6.5",
45
+ "@jsenv/exception": "1.1.6",
46
+ "@jsenv/filesystem": "4.14.3",
47
+ "@jsenv/github-check-run": "0.2.18",
48
48
  "@jsenv/node-esm-resolution": "1.1.0",
49
49
  "@jsenv/humanize": "1.3.1",
50
- "@jsenv/plugin-supervisor": "1.6.9",
50
+ "@jsenv/plugin-supervisor": "1.6.10",
51
51
  "@jsenv/sourcemap": "1.3.2",
52
- "@jsenv/url-meta": "8.5.4",
52
+ "@jsenv/url-meta": "8.5.5",
53
53
  "@jsenv/urls": "2.6.3",
54
54
  "@jsenv/utils": "2.2.1",
55
55
  "he": "1.2.0",