@ptolemy2002/rgx 12.7.4 → 12.8.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.
@@ -9,6 +9,11 @@ export type RGXWalkerOptions<R, S = unknown> = {
9
9
  looping?: boolean;
10
10
  contiguous?: boolean;
11
11
  };
12
+ export type RGXTryWalkOptions = {
13
+ revertReduced?: boolean;
14
+ revertShare?: boolean;
15
+ revertCaptures?: boolean;
16
+ };
12
17
  export type RGXTokenOrPart<R, S = unknown, T = unknown> = RGXToken | RGXPart<R, S, T>;
13
18
  export type RGXWalkerStepDirective = "stop" | "skip" | "silent";
14
19
  export declare class RGXWalker<R, S = unknown> {
@@ -56,7 +61,7 @@ export declare class RGXWalker<R, S = unknown> {
56
61
  stepToToken(predicate: (token: RGXTokenOrPart<R>) => boolean): this;
57
62
  stepToPart(predicate?: (part: RGXPart<R, S, unknown>) => boolean): this;
58
63
  walk(): R;
59
- tryWalk(): boolean;
64
+ tryWalk({ revertReduced, revertShare, revertCaptures }?: RGXTryWalkOptions): boolean;
60
65
  clone(depth?: CloneDepth): RGXWalker<R, S>;
61
66
  }
62
67
  export declare function rgxWalker<R, S = unknown>(...args: ConstructorParameters<typeof RGXWalker<R, S>>): RGXWalker<R, S>;
@@ -104,7 +104,7 @@ class RGXWalker {
104
104
  }
105
105
  capture(token, includeMatch = false) {
106
106
  const regex = (0, utils_1.createRegex)((0, resolve_1.resolveRGXToken)(part_1.RGXPart.check(token) ? token.token : token));
107
- const args = [regex, this.source, this.sourcePosition, 10, true];
107
+ const args = [regex, this.source, Math.min(this.sourcePosition, this.source.length - 1), 10, true];
108
108
  let match;
109
109
  let endPosition;
110
110
  if (this.contiguous) {
@@ -341,9 +341,13 @@ class RGXWalker {
341
341
  this.stepToToken(() => false);
342
342
  return this.reduced;
343
343
  }
344
- tryWalk() {
344
+ tryWalk({ revertReduced = false, revertShare = false, revertCaptures = false } = {}) {
345
345
  const prevSourcePosition = this.sourcePosition;
346
346
  const prevTokenPosition = this.tokenPosition;
347
+ const prevReduced = revertReduced ? (0, immutability_utils_1.extClone)(this.reduced, "max") : this.reduced;
348
+ const prevShare = revertShare ? (0, immutability_utils_1.extClone)(this.share, "max") : this.share;
349
+ const prevCaptures = revertCaptures ? (0, immutability_utils_1.extClone)(this.captures, "max") : this.captures;
350
+ const prevNamedCaptures = revertCaptures ? (0, immutability_utils_1.extClone)(this.namedCaptures, "max") : this.namedCaptures;
347
351
  try {
348
352
  this.walk();
349
353
  return true;
@@ -351,6 +355,14 @@ class RGXWalker {
351
355
  catch (e) {
352
356
  this.sourcePosition = prevSourcePosition;
353
357
  this.tokenPosition = prevTokenPosition;
358
+ if (revertReduced)
359
+ this.reduced = prevReduced;
360
+ if (revertShare)
361
+ this.share = prevShare;
362
+ if (revertCaptures)
363
+ this.captures = prevCaptures;
364
+ if (revertCaptures)
365
+ this.namedCaptures = prevNamedCaptures;
354
366
  if (isMatchError(e)) {
355
367
  return false;
356
368
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "12.7.4",
3
+ "version": "12.8.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",