@ptolemy2002/rgx 13.8.0 → 13.9.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 (51) hide show
  1. package/README.md +1 -0
  2. package/dist/class/base.d.ts +4 -1
  3. package/dist/class/base.js +13 -0
  4. package/dist/class/exclusion.d.ts +4 -1
  5. package/dist/class/exclusion.js +27 -0
  6. package/dist/class/group.d.ts +4 -0
  7. package/dist/class/group.js +30 -0
  8. package/dist/class/init.d.ts +3 -0
  9. package/dist/class/init.js +24 -0
  10. package/dist/class/lookahead.d.ts +4 -1
  11. package/dist/class/lookahead.js +14 -2
  12. package/dist/class/lookaround.d.ts +2 -0
  13. package/dist/class/lookaround.js +13 -0
  14. package/dist/class/lookbehind.d.ts +4 -1
  15. package/dist/class/lookbehind.js +14 -2
  16. package/dist/class/repeat.d.ts +4 -1
  17. package/dist/class/repeat.js +23 -0
  18. package/dist/class/subpattern.d.ts +4 -0
  19. package/dist/class/subpattern.js +15 -0
  20. package/dist/class/toRGXClassToken.js +4 -0
  21. package/dist/class/union.d.ts +4 -0
  22. package/dist/class/union.js +29 -1
  23. package/dist/class/wrapper.d.ts +4 -1
  24. package/dist/class/wrapper.js +19 -2
  25. package/dist/errors/base.d.ts +1 -1
  26. package/dist/errors/index.d.ts +5 -0
  27. package/dist/errors/index.js +5 -0
  28. package/dist/errors/invalidJSONClassKey.d.ts +6 -0
  29. package/dist/errors/invalidJSONClassKey.js +15 -0
  30. package/dist/errors/invalidJSONToken.d.ts +4 -0
  31. package/dist/errors/invalidJSONToken.js +12 -0
  32. package/dist/errors/invalidToken.js +15 -2
  33. package/dist/errors/jsonClassArgsValidationFailed.d.ts +8 -0
  34. package/dist/errors/jsonClassArgsValidationFailed.js +20 -0
  35. package/dist/errors/jsonClassConflict.d.ts +6 -0
  36. package/dist/errors/jsonClassConflict.js +15 -0
  37. package/dist/errors/notJSONSerializable.d.ts +7 -0
  38. package/dist/errors/notJSONSerializable.js +26 -0
  39. package/dist/flag-transformer/accentInsensitive.js +5 -5
  40. package/dist/index.d.ts +1 -0
  41. package/dist/index.js +1 -0
  42. package/dist/internal/assureAcceptance.js +4 -1
  43. package/dist/json.d.ts +35 -0
  44. package/dist/json.js +208 -0
  45. package/dist/resolve.js +4 -0
  46. package/dist/typeGuards.d.ts +22 -10
  47. package/dist/typeGuards.js +183 -36
  48. package/dist/types.d.ts +26 -5
  49. package/dist/types.js +3 -1
  50. package/dist/walker/base.d.ts +8 -0
  51. package/package.json +1 -1
@@ -46,6 +46,18 @@ exports.isRGXConvertibleToken = isRGXConvertibleToken;
46
46
  exports.assertRGXConvertibleToken = assertRGXConvertibleToken;
47
47
  exports.isRGXArrayToken = isRGXArrayToken;
48
48
  exports.assertRGXArrayToken = assertRGXArrayToken;
49
+ exports.isRGXJSONValue = isRGXJSONValue;
50
+ exports.assertRGXJSONValue = assertRGXJSONValue;
51
+ exports.isRGXJSONNativeToken = isRGXJSONNativeToken;
52
+ exports.assertRGXJSONNativeToken = assertRGXJSONNativeToken;
53
+ exports.isRGXJSONLiteralToken = isRGXJSONLiteralToken;
54
+ exports.assertRGXJSONLiteralToken = assertRGXJSONLiteralToken;
55
+ exports.isRGXJSONClassToken = isRGXJSONClassToken;
56
+ exports.assertRGXJSONClassToken = assertRGXJSONClassToken;
57
+ exports.isRGXJSONObjectToken = isRGXJSONObjectToken;
58
+ exports.assertRGXJSONObjectToken = assertRGXJSONObjectToken;
59
+ exports.isRGXJSONToken = isRGXJSONToken;
60
+ exports.assertRGXJSONToken = assertRGXJSONToken;
49
61
  exports.rgxTokenTypeFlat = rgxTokenTypeFlat;
50
62
  exports.rgxTokenType = rgxTokenType;
51
63
  exports.rgxTokenFromType = rgxTokenFromType;
@@ -70,12 +82,22 @@ exports.castValidRegexLocalizableFlagDiff = castValidRegexLocalizableFlagDiff;
70
82
  exports.isValidRegexLocalizableFlags = isValidRegexLocalizableFlags;
71
83
  exports.assertValidRegexLocalizableFlags = assertValidRegexLocalizableFlags;
72
84
  exports.castValidRegexLocalizableFlags = castValidRegexLocalizableFlags;
85
+ const t = __importStar(require("./types"));
73
86
  const e = __importStar(require("./errors"));
74
87
  const is_callable_1 = __importDefault(require("is-callable"));
75
88
  const internal_1 = require("./internal");
76
89
  const class_1 = require("./class");
77
90
  const ExtRegExp_1 = require("./ExtRegExp");
78
91
  const collection_1 = require("./collection");
92
+ const json_1 = require("./json");
93
+ const createRegex_1 = require("./utils/createRegex");
94
+ const ExtRegExp_2 = require("./ExtRegExp");
95
+ function isPlainObject(value) {
96
+ if (typeof value !== 'object' || value === null || Array.isArray(value))
97
+ return false;
98
+ const proto = Object.getPrototypeOf(value);
99
+ return proto === Object.prototype || proto === null;
100
+ }
79
101
  function isRGXNoOpToken(value) {
80
102
  return value === null || value === undefined;
81
103
  }
@@ -100,7 +122,7 @@ function assertRGXNativeToken(value) {
100
122
  throw new e.RGXInvalidTokenError("Invalid native token", { type: "tokenType", values: ['native'] }, value);
101
123
  }
102
124
  }
103
- function isRGXConvertibleToken(value, returnCheck = true) {
125
+ function isRGXConvertibleToken(value, returnCheck = true, acceptJSON = true) {
104
126
  if (typeof value === 'object' && value !== null && 'toRgx' in value) {
105
127
  // The rgxGroupWrap, interpolate, rgxIsRepeatable, and rgxIsGroup properties are optional, but if they exist they must be booleans.
106
128
  if ('rgxGroupWrap' in value && typeof value.rgxGroupWrap !== 'boolean')
@@ -126,29 +148,129 @@ function isRGXConvertibleToken(value, returnCheck = true) {
126
148
  return true;
127
149
  const rv = value.toRgx();
128
150
  return isRGXNativeToken(rv) || isRGXLiteralToken(rv) ||
129
- isRGXNoOpToken(rv) || isRGXArrayToken(rv) ||
130
- isRGXConvertibleToken(rv);
151
+ isRGXNoOpToken(rv) || isRGXArrayToken(rv, true, acceptJSON) ||
152
+ (acceptJSON && isRGXJSONObjectToken(rv)) ||
153
+ isRGXConvertibleToken(rv, true, acceptJSON);
131
154
  }
132
155
  return false;
133
156
  }
134
157
  return false;
135
158
  }
136
- function assertRGXConvertibleToken(value, returnCheck = true) {
137
- if (!isRGXConvertibleToken(value, returnCheck)) {
159
+ function assertRGXConvertibleToken(value, returnCheck = true, acceptJSON = true) {
160
+ if (!isRGXConvertibleToken(value, returnCheck, acceptJSON)) {
138
161
  throw new e.RGXInvalidTokenError(`Invalid convertible token`, { type: "tokenType", values: ['convertible'] }, value);
139
162
  }
140
163
  }
141
- function isRGXArrayToken(value, contentCheck = true) {
164
+ function isRGXArrayToken(value, contentCheck = true, acceptJSON = true) {
142
165
  return Array.isArray(value) && (!contentCheck || value.every(item => isRGXNoOpToken(item) || isRGXLiteralToken(item) ||
143
166
  isRGXNativeToken(item) ||
144
- isRGXConvertibleToken(item) || isRGXArrayToken(item)));
167
+ (acceptJSON && isRGXJSONObjectToken(item)) ||
168
+ isRGXConvertibleToken(item, true, acceptJSON) || isRGXArrayToken(item, true, acceptJSON)));
145
169
  }
146
- function assertRGXArrayToken(value, contentCheck = true) {
147
- if (!isRGXArrayToken(value, contentCheck)) {
170
+ function assertRGXArrayToken(value, contentCheck = true, acceptJSON = true) {
171
+ if (!isRGXArrayToken(value, contentCheck, acceptJSON)) {
148
172
  throw new e.RGXInvalidTokenError("Invalid array token", { type: "tokenType", values: ['array'] }, value);
149
173
  }
150
174
  }
151
- function rgxTokenTypeFlat(value, recognizeClass = true) {
175
+ function isRGXJSONValue(value) {
176
+ if (value === null || typeof value === 'string' || typeof value === 'boolean')
177
+ return true;
178
+ if (typeof value === 'number')
179
+ return Number.isFinite(value);
180
+ if (Array.isArray(value))
181
+ return value.every(item => isRGXJSONValue(item));
182
+ // undefined values are dropped by JSON.stringify, so they are allowed inside objects.
183
+ if (isPlainObject(value))
184
+ return Object.values(value).every(item => item === undefined || isRGXJSONValue(item));
185
+ return false;
186
+ }
187
+ function assertRGXJSONValue(value) {
188
+ if (!isRGXJSONValue(value)) {
189
+ throw new e.RGXInvalidJSONTokenError("Invalid JSON value", { type: "custom", values: ["null", "string", "finite number", "boolean", "array of JSON values", "plain object of JSON values"] }, value);
190
+ }
191
+ }
192
+ function isRGXJSONNativeToken(value) {
193
+ return value === null || typeof value === 'string' || typeof value === 'boolean' || (typeof value === 'number' && Number.isFinite(value));
194
+ }
195
+ function assertRGXJSONNativeToken(value) {
196
+ if (!isRGXJSONNativeToken(value)) {
197
+ throw new e.RGXInvalidJSONTokenError("Invalid JSON native token", { type: "custom", values: ["string", "finite number", "boolean", "null"] }, value);
198
+ }
199
+ }
200
+ function hasRGXJSONFlag(value) {
201
+ return isPlainObject(value) && value[t.RGX_JSON_FLAG] === true;
202
+ }
203
+ function isRGXJSONLiteralToken(value, contentCheck = true) {
204
+ if (!hasRGXJSONFlag(value))
205
+ return false;
206
+ if (typeof value.source !== 'string' || 'class' in value)
207
+ return false;
208
+ if ('flags' in value && value.flags !== undefined && typeof value.flags !== 'string')
209
+ return false;
210
+ if (!contentCheck)
211
+ return true;
212
+ const flags = value.flags ?? '';
213
+ if (!(0, ExtRegExp_2.isValidRegexFlags)(flags))
214
+ return false;
215
+ try {
216
+ (0, createRegex_1.createRegex)(value.source, (0, ExtRegExp_2.extractVanillaRegexFlags)(flags));
217
+ return true;
218
+ }
219
+ catch (err) {
220
+ if (err instanceof e.RGXInvalidRegexStringError)
221
+ return false;
222
+ // This is ignored because I don't know what kind of
223
+ // unexpected errors might happen.
224
+ /* istanbul ignore next */
225
+ throw err;
226
+ }
227
+ }
228
+ function assertRGXJSONLiteralToken(value, contentCheck = true) {
229
+ if (!isRGXJSONLiteralToken(value, contentCheck)) {
230
+ throw new e.RGXInvalidJSONTokenError("Invalid JSON literal token", { type: "custom", values: ["object with $rgx set to true, a valid regex source string, and optional valid flags"] }, value);
231
+ }
232
+ }
233
+ function isRGXJSONClassToken(value, registryCheck = true) {
234
+ if (!hasRGXJSONFlag(value))
235
+ return false;
236
+ if (typeof value.class !== 'string' || 'source' in value)
237
+ return false;
238
+ if ('args' in value && value.args !== undefined && !(Array.isArray(value.args) && isRGXJSONValue(value.args)))
239
+ return false;
240
+ if (!registryCheck)
241
+ return true;
242
+ if (!(0, json_1.hasRGXJSONClass)(value.class))
243
+ return false;
244
+ return (0, json_1.validateRGXJSONClassArgs)(value.class, value.args ?? []) === true;
245
+ }
246
+ function assertRGXJSONClassToken(value, registryCheck = true) {
247
+ if (!isRGXJSONClassToken(value, registryCheck)) {
248
+ throw new e.RGXInvalidJSONTokenError("Invalid JSON class token", { type: "custom", values: ["object with $rgx set to true, a registered class name, and optional args that pass the class' validator"] }, value);
249
+ }
250
+ }
251
+ function isRGXJSONObjectToken(value, contentCheck = true) {
252
+ return isRGXJSONLiteralToken(value, contentCheck) || isRGXJSONClassToken(value, contentCheck);
253
+ }
254
+ function assertRGXJSONObjectToken(value, contentCheck = true) {
255
+ if (!isRGXJSONObjectToken(value, contentCheck)) {
256
+ throw new e.RGXInvalidJSONTokenError("Invalid JSON object token", { type: "custom", values: ["JSON literal token", "JSON class token"] }, value);
257
+ }
258
+ }
259
+ function isRGXJSONToken(value, contentCheck = true) {
260
+ if (isRGXJSONNativeToken(value))
261
+ return true;
262
+ if (isRGXJSONObjectToken(value, contentCheck))
263
+ return true;
264
+ if (Array.isArray(value))
265
+ return !contentCheck || value.every(item => isRGXJSONToken(item, contentCheck));
266
+ return false;
267
+ }
268
+ function assertRGXJSONToken(value, contentCheck = true) {
269
+ if (!isRGXJSONToken(value, contentCheck)) {
270
+ throw new e.RGXInvalidJSONTokenError("Invalid JSON token", { type: "tokenType", values: ["json"] }, value);
271
+ }
272
+ }
273
+ function rgxTokenTypeFlat(value, recognizeClass = true, acceptJSON = true) {
152
274
  if (isRGXNoOpToken(value))
153
275
  return 'no-op';
154
276
  if (isRGXLiteralToken(value))
@@ -158,18 +280,21 @@ function rgxTokenTypeFlat(value, recognizeClass = true) {
158
280
  // We have to check class before convertible because class tokens are also convertible.
159
281
  if (recognizeClass && class_1.RGXClassToken.check(value))
160
282
  return 'class';
161
- if (isRGXConvertibleToken(value))
283
+ // JSON object tokens are checked before convertible tokens, since the flag signals explicit intent.
284
+ if (acceptJSON && isRGXJSONObjectToken(value))
285
+ return 'json';
286
+ if (isRGXConvertibleToken(value, true, acceptJSON))
162
287
  return 'convertible';
163
- if (isRGXArrayToken(value))
288
+ if (isRGXArrayToken(value, true, acceptJSON))
164
289
  return 'array';
165
290
  throw new e.RGXInvalidTokenError("Invalid RGX token", null, value);
166
291
  }
167
- function rgxTokenType(value, recognizeClass = true) {
168
- const flatType = rgxTokenTypeFlat(value, recognizeClass);
292
+ function rgxTokenType(value, recognizeClass = true, acceptJSON = true) {
293
+ const flatType = rgxTokenTypeFlat(value, recognizeClass, acceptJSON);
169
294
  if (flatType !== 'array')
170
295
  return flatType;
171
296
  else
172
- return value.map(item => rgxTokenType(item, recognizeClass));
297
+ return value.map(item => rgxTokenType(item, recognizeClass, acceptJSON));
173
298
  }
174
299
  function rgxTokenFromType(type, value) {
175
300
  // Ignoring this line because the function is entirely a TypeScript utility that doesn't need to be tested at runtime.
@@ -194,9 +319,12 @@ function rgxTokenTypeGuardInputToFlat(type) {
194
319
  return null;
195
320
  return type;
196
321
  }
197
- function isRGXToken(value, type = null, matchLength = true) {
198
- if ((0, internal_1.isConstructor)(type))
199
- return value instanceof type;
322
+ function isRGXToken(value, type = null, matchLength = true, acceptJSON = true) {
323
+ if ((0, internal_1.isConstructor)(type)) {
324
+ if (value instanceof type)
325
+ return true;
326
+ return acceptJSON && isRGXJSONClassToken(value) && (0, json_1.rgxTokenFromJSON)(value) instanceof type;
327
+ }
200
328
  function typeMatches(s) {
201
329
  return type === null || type === s;
202
330
  }
@@ -204,50 +332,69 @@ function isRGXToken(value, type = null, matchLength = true) {
204
332
  return true;
205
333
  if (typeMatches('literal') && isRGXLiteralToken(value))
206
334
  return true;
335
+ if (typeMatches('literal') && acceptJSON && isRGXJSONLiteralToken(value))
336
+ return true;
207
337
  if (typeMatches('native') && isRGXNativeToken(value))
208
338
  return true;
209
339
  // We have to check class before convertible because class tokens are also convertible.
210
340
  if (typeMatches('class') && class_1.RGXClassToken.check(value))
211
341
  return true;
212
- if (typeMatches('convertible') && isRGXConvertibleToken(value))
342
+ if (typeMatches('class') && acceptJSON && isRGXJSONClassToken(value))
213
343
  return true;
344
+ if (typeMatches('convertible') && isRGXConvertibleToken(value, true, acceptJSON))
345
+ return true;
346
+ // JSON class tokens become class tokens (which are convertible) when converted.
347
+ if (typeMatches('convertible') && acceptJSON && isRGXJSONClassToken(value))
348
+ return true;
349
+ // The 'json' type is explicit, so it ignores acceptJSON.
350
+ if (type === 'json' && isRGXJSONToken(value))
351
+ return true;
352
+ if (type === 'repeatable' && isRGXJSONObjectToken(value)) {
353
+ if (!acceptJSON)
354
+ return false;
355
+ // Converting a JSON token yields a literal or class token, so we can check that instead.
356
+ return isRGXToken((0, json_1.rgxTokenFromJSON)(value), 'repeatable', true, false);
357
+ }
214
358
  // Non-covertible tokens are repeatable by default. Convertible tokens are only non-repeatable if they have the rgxIsRepeatable property set to false.
215
359
  if (type === 'repeatable' && !isRGXConvertibleToken(value, false))
216
360
  return true;
217
- if (type === 'repeatable' && isRGXConvertibleToken(value))
361
+ if (type === 'repeatable' && isRGXConvertibleToken(value, true, acceptJSON))
218
362
  return value.rgxIsRepeatable ?? true;
219
- if (typeMatches('array') && isRGXArrayToken(value))
363
+ if (typeMatches('array') && isRGXArrayToken(value, true, acceptJSON))
220
364
  return true;
221
365
  if (Array.isArray(type) && Array.isArray(value) && (!matchLength || type.length === value.length)) {
222
366
  // This will always be false.
223
367
  if (value.length < type.length)
224
368
  return false;
225
369
  // @ts-ignore Excessively deep type is not a problem here.
226
- return value.every((item, i) => isRGXToken(item, type[i] ?? null));
370
+ return value.every((item, i) => isRGXToken(item, type[i] ?? null, true, acceptJSON));
227
371
  }
228
372
  return false;
229
373
  }
230
- function assertRGXToken(value, type = null, matchLength = true) {
231
- if (!isRGXToken(value, type, matchLength)) {
374
+ function assertRGXToken(value, type = null, matchLength = true, acceptJSON = true) {
375
+ // Captured before narrowing, since the narrowed type of `value` in the failure branch is excessively deep for TypeScript.
376
+ const got = value;
377
+ if (!isRGXToken(value, type, matchLength, acceptJSON)) {
232
378
  const flatType = rgxTokenTypeGuardInputToFlat(type);
233
- /*
234
- ts-ignore
235
- This line sometimes throws a weird error in VSCode, but it doesn't prevent a build, and I don't know how to fix it.
236
- It's an excessively deep type error on the `value` variable, which is typed as `unknown`.
237
- */
238
- throw new e.RGXInvalidTokenError("Invalid RGX token", flatType === null ? null : { type: "tokenType", values: [flatType] }, value);
379
+ throw new e.RGXInvalidTokenError("Invalid RGX token", flatType === null ? null : { type: "tokenType", values: [flatType] }, got);
239
380
  }
240
381
  }
241
- function isRGXGroupedToken(value, contentCheck = true) {
382
+ function isRGXGroupedToken(value, contentCheck = true, acceptJSON = true) {
383
+ // JSON literal tokens are literals once converted, so they are implicitly groups.
384
+ // JSON class tokens are groups if the class token they convert to is a group.
385
+ if (acceptJSON && isRGXJSONLiteralToken(value, contentCheck))
386
+ return true;
387
+ if (acceptJSON && isRGXJSONClassToken(value))
388
+ return isRGXGroupedToken((0, json_1.rgxTokenFromJSON)(value), contentCheck, false);
242
389
  // Arrays and Literals are implicitly groups.
243
390
  // Classes are only groups if they have the isGroup property set to true.
244
- return (isRGXArrayToken(value, contentCheck) ||
245
- isRGXToken(value, "literal") ||
391
+ return (isRGXArrayToken(value, contentCheck, acceptJSON) ||
392
+ isRGXToken(value, "literal", true, false) ||
246
393
  (isRGXConvertibleToken(value, false) && (value.rgxIsGroup ||
247
- (value.rgxGroupWrap === true && (!contentCheck || isRGXGroupedToken(value.toRgx()))))));
394
+ (value.rgxGroupWrap === true && (!contentCheck || isRGXGroupedToken(value.toRgx(), true, acceptJSON))))));
248
395
  }
249
- function assertRGXGroupedToken(value, contentCheck = true) {
250
- if (!isRGXGroupedToken(value, contentCheck)) {
396
+ function assertRGXGroupedToken(value, contentCheck = true, acceptJSON = true) {
397
+ if (!isRGXGroupedToken(value, contentCheck, acceptJSON)) {
251
398
  throw new e.RGXInvalidTokenError("Invalid group token, class token is not group, or convertible token is not group wrapped.", { type: "custom", values: ['array', 'literal', 'class', 'convertible'] }, value);
252
399
  }
253
400
  }
package/dist/types.d.ts CHANGED
@@ -14,9 +14,30 @@ export type RGXConvertibleToken = {
14
14
  readonly rgxIsRepeatable?: boolean;
15
15
  readonly rgxInterpolate?: boolean;
16
16
  };
17
- export type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
17
+ export declare const RGX_JSON_FLAG = "$rgx";
18
+ export type RGXJSONFlag = typeof RGX_JSON_FLAG;
19
+ export type RGXJSONPrimitive = string | number | boolean | null;
20
+ export type RGXJSONObject = {
21
+ [key: string]: RGXJSONValue | undefined;
22
+ };
23
+ export type RGXJSONValue = RGXJSONPrimitive | RGXJSONValue[] | RGXJSONObject;
24
+ export type RGXJSONNativeToken = Exclude<RGXNativeToken, undefined>;
25
+ export type RGXJSONLiteralToken = {
26
+ $rgx: true;
27
+ source: string;
28
+ flags?: string;
29
+ };
30
+ export type RGXJSONClassToken = {
31
+ $rgx: true;
32
+ class: string;
33
+ args?: RGXJSONValue[];
34
+ };
35
+ export type RGXJSONObjectToken = RGXJSONLiteralToken | RGXJSONClassToken;
36
+ export type RGXJSONToken = RGXJSONNativeToken | RGXJSONObjectToken | RGXJSONToken[];
37
+ export type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXJSONObjectToken | RGXToken[];
38
+ export type RGXNonJSONToken = Exclude<RGXToken, RGXJSONObjectToken>;
18
39
  export type RGXClassTokenConstructor = new (...args: unknown[]) => RGXClassToken;
19
- export type RGXGroupedToken = RGXToken[] | RGXLiteralToken | RGXGroupedConvertibleToken;
40
+ export type RGXGroupedToken = RGXToken[] | RGXLiteralToken | RGXGroupedConvertibleToken | RGXJSONObjectToken;
20
41
  export type RGXGroupedConvertibleToken = (RGXConvertibleToken & {
21
42
  readonly rgxIsGroup: true;
22
43
  }) | (Omit<RGXConvertibleToken, "toRgx"> & {
@@ -26,11 +47,11 @@ export type RGXGroupedConvertibleToken = (RGXConvertibleToken & {
26
47
  export type RGXRepeatableConvertibleToken = RGXConvertibleToken & {
27
48
  readonly rgxIsRepeatable: true | undefined;
28
49
  };
29
- export type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | 'class' | RGXTokenType[];
50
+ export type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | 'class' | 'json' | RGXTokenType[];
30
51
  export type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
31
52
  export type RGXTokenTypeGuardInput = "repeatable" | RGXTokenTypeFlat | null | RGXClassTokenConstructor | typeof RegExp | typeof ExtRegExp | typeof RGXTokenCollection | RGXTokenTypeGuardInput[];
32
- export type RGXTokenFromType<T extends RGXTokenTypeGuardInput> = T extends null ? RGXToken : T extends 'no-op' ? RGXNoOpToken : T extends 'literal' ? RGXLiteralToken : T extends 'native' ? RGXNativeToken : T extends 'convertible' ? RGXConvertibleToken : T extends 'class' ? RGXClassToken : T extends 'array' ? RGXToken[] : T extends 'repeatable' ? Exclude<RGXToken, RGXConvertibleToken> | RGXRepeatableConvertibleToken : T extends new (...args: unknown[]) => infer R ? R : T extends RGXTokenTypeGuardInput[] ? {
33
- [K in keyof T]: T[K] extends RGXTokenTypeGuardInput ? RGXTokenFromType<T[K]> : never;
53
+ export type RGXTokenFromType<T extends RGXTokenTypeGuardInput, J extends boolean = true> = T extends null ? (J extends true ? RGXToken : RGXNonJSONToken) : T extends 'no-op' ? RGXNoOpToken : T extends 'literal' ? (J extends true ? RGXLiteralToken | RGXJSONLiteralToken : RGXLiteralToken) : T extends 'native' ? RGXNativeToken : T extends 'convertible' ? (J extends true ? RGXConvertibleToken | RGXJSONClassToken : RGXConvertibleToken) : T extends 'class' ? (J extends true ? RGXClassToken | RGXJSONClassToken : RGXClassToken) : T extends 'json' ? RGXJSONToken : T extends 'array' ? RGXToken[] : T extends 'repeatable' ? Exclude<RGXToken, RGXConvertibleToken | RGXJSONClassToken> | RGXRepeatableConvertibleToken | (J extends true ? RGXJSONClassToken : never) : T extends new (...args: unknown[]) => infer R ? (J extends true ? R | RGXJSONClassToken : R) : T extends RGXTokenTypeGuardInput[] ? {
54
+ [K in keyof T]: T[K] extends RGXTokenTypeGuardInput ? RGXTokenFromType<T[K], J> : never;
34
55
  } : never;
35
56
  export type RangeObject = {
36
57
  min?: number | null;
package/dist/types.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.validRegexLocalizableFlagDiffSymbol = exports.validRegexLocalizableFlagsSymbol = exports.validIdentifierSymbol = exports.validRegexFlagsSymbol = exports.validVanillaRegexFlagsSymbol = exports.validRegexSymbol = void 0;
3
+ exports.validRegexLocalizableFlagDiffSymbol = exports.validRegexLocalizableFlagsSymbol = exports.validIdentifierSymbol = exports.validRegexFlagsSymbol = exports.validVanillaRegexFlagsSymbol = exports.validRegexSymbol = exports.RGX_JSON_FLAG = void 0;
4
+ // The key that signals that a plain object is intended to be an RGX token in JSON form.
5
+ exports.RGX_JSON_FLAG = "$rgx";
4
6
  exports.validRegexSymbol = Symbol('rgx.ValidRegex');
5
7
  exports.validVanillaRegexFlagsSymbol = Symbol('rgx.ValidVanillaRegexFlags');
6
8
  exports.validRegexFlagsSymbol = Symbol('rgx.ValidRegexFlags');
@@ -15,6 +15,14 @@ export type RGXTryWalkOptions = {
15
15
  revertCaptures?: boolean;
16
16
  };
17
17
  export type RGXWalkerSnapshotKey = "sourcePosition" | "tokenPosition" | "reduced" | "share" | "captures" | "namedCaptures";
18
+ export type RGXWalkerSnapshot<R, S> = Partial<{
19
+ sourcePosition: number;
20
+ tokenPosition: number;
21
+ reduced: R;
22
+ share: S;
23
+ captures: RGXCapture[];
24
+ namedCaptures: Record<string, RGXCapture[]>;
25
+ }>;
18
26
  export type RGXTokenOrPart<R, S = unknown, T = any> = RGXToken | RGXPart<R, S, T>;
19
27
  export type RGXWalkerStepDirective = "stop" | "skip" | "silent";
20
28
  export declare class RGXWalker<R, S = unknown> {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "13.8.0",
3
+ "version": "13.9.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",