@ptolemy2002/rgx 13.6.0 → 13.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.
- package/dist/typeGuards.d.ts +5 -0
- package/dist/typeGuards.js +25 -0
- package/dist/walker/base.d.ts +8 -1
- package/dist/walker/base.js +48 -6
- package/package.json +1 -1
package/dist/typeGuards.d.ts
CHANGED
|
@@ -20,11 +20,16 @@ export declare function isRGXGroupedToken(value: unknown, contentCheck?: boolean
|
|
|
20
20
|
export declare function assertRGXGroupedToken(value: unknown, contentCheck?: boolean): asserts value is t.RGXGroupedToken;
|
|
21
21
|
export declare function isValidRegexString(value: string): value is t.ValidRegexString;
|
|
22
22
|
export declare function assertValidRegexString(value: string): asserts value is t.ValidRegexString;
|
|
23
|
+
export declare function castValidRegexString(value: string): t.ValidRegexString;
|
|
23
24
|
export declare function isValidVanillaRegexFlags(value: string): value is t.ValidVanillaRegexFlags;
|
|
24
25
|
export declare function assertValidVanillaRegexFlags(value: string): asserts value is t.ValidVanillaRegexFlags;
|
|
26
|
+
export declare function castValidVanillaRegexFlags(value: string): t.ValidVanillaRegexFlags;
|
|
25
27
|
export declare function isValidIdentifier(value: string): value is t.ValidIdentifier;
|
|
26
28
|
export declare function assertValidIdentifier(value: string): asserts value is t.ValidIdentifier;
|
|
29
|
+
export declare function castValidIdentifier(value: string): t.ValidIdentifier;
|
|
27
30
|
export declare function isValidRegexLocalizableFlagDiff(value: string): value is t.ValidRegexLocalizableFlagDiff;
|
|
28
31
|
export declare function assertValidRegexLocalizableFlagDiff(value: string): asserts value is t.ValidRegexLocalizableFlagDiff;
|
|
32
|
+
export declare function castValidRegexLocalizableFlagDiff(value: string): t.ValidRegexLocalizableFlagDiff;
|
|
29
33
|
export declare function isValidRegexLocalizableFlags(value: string): value is t.ValidRegexLocalizableFlags;
|
|
30
34
|
export declare function assertValidRegexLocalizableFlags(value: string): asserts value is t.ValidRegexLocalizableFlags;
|
|
35
|
+
export declare function castValidRegexLocalizableFlags(value: string): t.ValidRegexLocalizableFlags;
|
package/dist/typeGuards.js
CHANGED
|
@@ -57,14 +57,19 @@ exports.isRGXGroupedToken = isRGXGroupedToken;
|
|
|
57
57
|
exports.assertRGXGroupedToken = assertRGXGroupedToken;
|
|
58
58
|
exports.isValidRegexString = isValidRegexString;
|
|
59
59
|
exports.assertValidRegexString = assertValidRegexString;
|
|
60
|
+
exports.castValidRegexString = castValidRegexString;
|
|
60
61
|
exports.isValidVanillaRegexFlags = isValidVanillaRegexFlags;
|
|
61
62
|
exports.assertValidVanillaRegexFlags = assertValidVanillaRegexFlags;
|
|
63
|
+
exports.castValidVanillaRegexFlags = castValidVanillaRegexFlags;
|
|
62
64
|
exports.isValidIdentifier = isValidIdentifier;
|
|
63
65
|
exports.assertValidIdentifier = assertValidIdentifier;
|
|
66
|
+
exports.castValidIdentifier = castValidIdentifier;
|
|
64
67
|
exports.isValidRegexLocalizableFlagDiff = isValidRegexLocalizableFlagDiff;
|
|
65
68
|
exports.assertValidRegexLocalizableFlagDiff = assertValidRegexLocalizableFlagDiff;
|
|
69
|
+
exports.castValidRegexLocalizableFlagDiff = castValidRegexLocalizableFlagDiff;
|
|
66
70
|
exports.isValidRegexLocalizableFlags = isValidRegexLocalizableFlags;
|
|
67
71
|
exports.assertValidRegexLocalizableFlags = assertValidRegexLocalizableFlags;
|
|
72
|
+
exports.castValidRegexLocalizableFlags = castValidRegexLocalizableFlags;
|
|
68
73
|
const e = __importStar(require("./errors"));
|
|
69
74
|
const is_callable_1 = __importDefault(require("is-callable"));
|
|
70
75
|
const internal_1 = require("./internal");
|
|
@@ -277,6 +282,10 @@ function assertValidRegexString(value) {
|
|
|
277
282
|
}
|
|
278
283
|
}
|
|
279
284
|
}
|
|
285
|
+
function castValidRegexString(value) {
|
|
286
|
+
assertValidRegexString(value);
|
|
287
|
+
return value;
|
|
288
|
+
}
|
|
280
289
|
function isValidVanillaRegexFlags(value) {
|
|
281
290
|
const patternMatch = /^[gimsuydv]*$/.test(value);
|
|
282
291
|
if (!patternMatch)
|
|
@@ -290,6 +299,10 @@ function assertValidVanillaRegexFlags(value) {
|
|
|
290
299
|
throw new e.RGXInvalidVanillaRegexFlagsError("Invalid vanilla regex flags", value);
|
|
291
300
|
}
|
|
292
301
|
}
|
|
302
|
+
function castValidVanillaRegexFlags(value) {
|
|
303
|
+
assertValidVanillaRegexFlags(value);
|
|
304
|
+
return value;
|
|
305
|
+
}
|
|
293
306
|
function isValidIdentifier(value) {
|
|
294
307
|
// This regex checks for valid JavaScript identifiers, which can be used for named capture groups.
|
|
295
308
|
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(value);
|
|
@@ -299,6 +312,10 @@ function assertValidIdentifier(value) {
|
|
|
299
312
|
throw new e.RGXInvalidIdentifierError("Invalid identifier", value);
|
|
300
313
|
}
|
|
301
314
|
}
|
|
315
|
+
function castValidIdentifier(value) {
|
|
316
|
+
assertValidIdentifier(value);
|
|
317
|
+
return value;
|
|
318
|
+
}
|
|
302
319
|
function isValidRegexLocalizableFlagDiff(value) {
|
|
303
320
|
if (value === '')
|
|
304
321
|
return true;
|
|
@@ -309,6 +326,10 @@ function assertValidRegexLocalizableFlagDiff(value) {
|
|
|
309
326
|
throw new e.RGXInvalidRegexLocalizableFlagDiffError("Invalid localizable flag diff", value);
|
|
310
327
|
}
|
|
311
328
|
}
|
|
329
|
+
function castValidRegexLocalizableFlagDiff(value) {
|
|
330
|
+
assertValidRegexLocalizableFlagDiff(value);
|
|
331
|
+
return value;
|
|
332
|
+
}
|
|
312
333
|
function isValidRegexLocalizableFlags(value) {
|
|
313
334
|
return /^[ims]*$/.test(value);
|
|
314
335
|
}
|
|
@@ -317,3 +338,7 @@ function assertValidRegexLocalizableFlags(value) {
|
|
|
317
338
|
throw new e.RGXInvalidRegexLocalizableFlagsError("Invalid localizable flags", value);
|
|
318
339
|
}
|
|
319
340
|
}
|
|
341
|
+
function castValidRegexLocalizableFlags(value) {
|
|
342
|
+
assertValidRegexLocalizableFlags(value);
|
|
343
|
+
return value;
|
|
344
|
+
}
|
package/dist/walker/base.d.ts
CHANGED
|
@@ -60,12 +60,19 @@ export declare class RGXWalker<R, S = unknown> {
|
|
|
60
60
|
private validateCapture;
|
|
61
61
|
private handleAfterCapture;
|
|
62
62
|
step(): RGXCapture | null;
|
|
63
|
-
|
|
63
|
+
private _stepToTokenIter;
|
|
64
|
+
stepToToken(predicate?: (token: RGXTokenOrPart<R>) => boolean): this;
|
|
65
|
+
stepToTokenAsync(predicate?: (token: RGXTokenOrPart<R, S, unknown>) => boolean, yieldFn?: () => Promise<void>): Promise<this>;
|
|
66
|
+
private _stepPastCurrentPartIfNeeded;
|
|
64
67
|
stepToPart(predicate?: (part: RGXPart<R, S, unknown>) => boolean): this;
|
|
68
|
+
stepToPartAsync(predicate?: (part: RGXPart<R, S, unknown>) => boolean, yieldFn?: () => Promise<void>): Promise<this>;
|
|
65
69
|
walk(): R;
|
|
70
|
+
walkAsync(yieldFn?: () => Promise<void>): Promise<R>;
|
|
66
71
|
snapshot(name: string, ...keys: Array<RGXWalkerSnapshotKey | false>): this;
|
|
67
72
|
restore(name: string): this;
|
|
73
|
+
private _walkerSnapshot;
|
|
68
74
|
tryWalk({ revertReduced, revertShare, revertCaptures }?: RGXTryWalkOptions): boolean;
|
|
75
|
+
tryWalkAsync({ revertReduced, revertShare, revertCaptures }?: RGXTryWalkOptions, yieldFn?: () => Promise<void>): Promise<boolean>;
|
|
69
76
|
clone(depth?: CloneDepth): RGXWalker<R, S>;
|
|
70
77
|
}
|
|
71
78
|
export declare function rgxWalker<R, S = unknown>(...args: ConstructorParameters<typeof RGXWalker<R, S>>): RGXWalker<R, S>;
|
package/dist/walker/base.js
CHANGED
|
@@ -315,7 +315,7 @@ class RGXWalker {
|
|
|
315
315
|
this.advanceToken();
|
|
316
316
|
return captureResult;
|
|
317
317
|
}
|
|
318
|
-
|
|
318
|
+
*_stepToTokenIter(predicate) {
|
|
319
319
|
while (this.hasNextToken()) {
|
|
320
320
|
this._stopped = false;
|
|
321
321
|
if (predicate(this.currentToken()))
|
|
@@ -323,25 +323,51 @@ class RGXWalker {
|
|
|
323
323
|
this.step();
|
|
324
324
|
if (this._stopped)
|
|
325
325
|
break;
|
|
326
|
+
yield;
|
|
326
327
|
}
|
|
328
|
+
}
|
|
329
|
+
stepToToken(predicate = () => true) {
|
|
330
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
331
|
+
for (const _ of this._stepToTokenIter(predicate)) { /* sync — no yield needed */ }
|
|
327
332
|
return this;
|
|
328
333
|
}
|
|
329
|
-
|
|
330
|
-
//
|
|
331
|
-
|
|
334
|
+
async stepToTokenAsync(predicate = () => true, yieldFn = () => new Promise(resolve => setImmediate(resolve))) {
|
|
335
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
336
|
+
for (const _ of this._stepToTokenIter(predicate))
|
|
337
|
+
await yieldFn();
|
|
338
|
+
return this;
|
|
339
|
+
}
|
|
340
|
+
// If currently at a Part, steps past it (so repeated stepToPart calls advance
|
|
341
|
+
// rather than getting stuck). Returns true when the walker stopped mid-step.
|
|
342
|
+
_stepPastCurrentPartIfNeeded() {
|
|
332
343
|
if (this.hasNextToken() && this.currentToken() instanceof part_1.RGXPart) {
|
|
333
344
|
this._stopped = false;
|
|
334
345
|
this.step();
|
|
335
346
|
if (this._stopped)
|
|
336
|
-
return
|
|
347
|
+
return true;
|
|
337
348
|
}
|
|
349
|
+
return false;
|
|
350
|
+
}
|
|
351
|
+
stepToPart(predicate = () => true) {
|
|
352
|
+
if (this._stepPastCurrentPartIfNeeded())
|
|
353
|
+
return this;
|
|
338
354
|
this.stepToToken(token => token instanceof part_1.RGXPart && predicate(token));
|
|
339
355
|
return this;
|
|
340
356
|
}
|
|
357
|
+
async stepToPartAsync(predicate = () => true, yieldFn = () => new Promise(resolve => setImmediate(resolve))) {
|
|
358
|
+
if (this._stepPastCurrentPartIfNeeded())
|
|
359
|
+
return this;
|
|
360
|
+
await this.stepToTokenAsync(token => token instanceof part_1.RGXPart && predicate(token), yieldFn);
|
|
361
|
+
return this;
|
|
362
|
+
}
|
|
341
363
|
walk() {
|
|
342
364
|
this.stepToToken(() => false);
|
|
343
365
|
return this.reduced;
|
|
344
366
|
}
|
|
367
|
+
async walkAsync(yieldFn) {
|
|
368
|
+
await this.stepToTokenAsync(() => false, yieldFn);
|
|
369
|
+
return this.reduced;
|
|
370
|
+
}
|
|
345
371
|
snapshot(name, ...keys) {
|
|
346
372
|
const filteredKeys = keys.filter((k) => k !== false);
|
|
347
373
|
const snap = {};
|
|
@@ -374,8 +400,11 @@ class RGXWalker {
|
|
|
374
400
|
this.namedCaptures = snap.namedCaptures;
|
|
375
401
|
return this;
|
|
376
402
|
}
|
|
403
|
+
_walkerSnapshot(name, { revertReduced, revertShare, revertCaptures }) {
|
|
404
|
+
return this.snapshot(name, "sourcePosition", "tokenPosition", revertReduced && "reduced", revertShare && "share", revertCaptures && "captures", revertCaptures && "namedCaptures");
|
|
405
|
+
}
|
|
377
406
|
tryWalk({ revertReduced = false, revertShare = false, revertCaptures = false } = {}) {
|
|
378
|
-
this.
|
|
407
|
+
this._walkerSnapshot("tryWalk", { revertReduced, revertShare, revertCaptures });
|
|
379
408
|
try {
|
|
380
409
|
this.walk();
|
|
381
410
|
return true;
|
|
@@ -387,6 +416,19 @@ class RGXWalker {
|
|
|
387
416
|
throw e;
|
|
388
417
|
}
|
|
389
418
|
}
|
|
419
|
+
async tryWalkAsync({ revertReduced = false, revertShare = false, revertCaptures = false } = {}, yieldFn) {
|
|
420
|
+
this._walkerSnapshot("tryWalk", { revertReduced, revertShare, revertCaptures });
|
|
421
|
+
try {
|
|
422
|
+
await this.walkAsync(yieldFn);
|
|
423
|
+
return true;
|
|
424
|
+
}
|
|
425
|
+
catch (e) {
|
|
426
|
+
this.restore("tryWalk");
|
|
427
|
+
if (isMatchError(e))
|
|
428
|
+
return false;
|
|
429
|
+
throw e;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
390
432
|
// Clone method
|
|
391
433
|
clone(depth = "max") {
|
|
392
434
|
if (depth === 0)
|