@ptolemy2002/rgx 13.7.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 +27 -10
  47. package/dist/typeGuards.js +208 -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;
@@ -57,20 +69,35 @@ exports.isRGXGroupedToken = isRGXGroupedToken;
57
69
  exports.assertRGXGroupedToken = assertRGXGroupedToken;
58
70
  exports.isValidRegexString = isValidRegexString;
59
71
  exports.assertValidRegexString = assertValidRegexString;
72
+ exports.castValidRegexString = castValidRegexString;
60
73
  exports.isValidVanillaRegexFlags = isValidVanillaRegexFlags;
61
74
  exports.assertValidVanillaRegexFlags = assertValidVanillaRegexFlags;
75
+ exports.castValidVanillaRegexFlags = castValidVanillaRegexFlags;
62
76
  exports.isValidIdentifier = isValidIdentifier;
63
77
  exports.assertValidIdentifier = assertValidIdentifier;
78
+ exports.castValidIdentifier = castValidIdentifier;
64
79
  exports.isValidRegexLocalizableFlagDiff = isValidRegexLocalizableFlagDiff;
65
80
  exports.assertValidRegexLocalizableFlagDiff = assertValidRegexLocalizableFlagDiff;
81
+ exports.castValidRegexLocalizableFlagDiff = castValidRegexLocalizableFlagDiff;
66
82
  exports.isValidRegexLocalizableFlags = isValidRegexLocalizableFlags;
67
83
  exports.assertValidRegexLocalizableFlags = assertValidRegexLocalizableFlags;
84
+ exports.castValidRegexLocalizableFlags = castValidRegexLocalizableFlags;
85
+ const t = __importStar(require("./types"));
68
86
  const e = __importStar(require("./errors"));
69
87
  const is_callable_1 = __importDefault(require("is-callable"));
70
88
  const internal_1 = require("./internal");
71
89
  const class_1 = require("./class");
72
90
  const ExtRegExp_1 = require("./ExtRegExp");
73
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
+ }
74
101
  function isRGXNoOpToken(value) {
75
102
  return value === null || value === undefined;
76
103
  }
@@ -95,7 +122,7 @@ function assertRGXNativeToken(value) {
95
122
  throw new e.RGXInvalidTokenError("Invalid native token", { type: "tokenType", values: ['native'] }, value);
96
123
  }
97
124
  }
98
- function isRGXConvertibleToken(value, returnCheck = true) {
125
+ function isRGXConvertibleToken(value, returnCheck = true, acceptJSON = true) {
99
126
  if (typeof value === 'object' && value !== null && 'toRgx' in value) {
100
127
  // The rgxGroupWrap, interpolate, rgxIsRepeatable, and rgxIsGroup properties are optional, but if they exist they must be booleans.
101
128
  if ('rgxGroupWrap' in value && typeof value.rgxGroupWrap !== 'boolean')
@@ -121,29 +148,129 @@ function isRGXConvertibleToken(value, returnCheck = true) {
121
148
  return true;
122
149
  const rv = value.toRgx();
123
150
  return isRGXNativeToken(rv) || isRGXLiteralToken(rv) ||
124
- isRGXNoOpToken(rv) || isRGXArrayToken(rv) ||
125
- isRGXConvertibleToken(rv);
151
+ isRGXNoOpToken(rv) || isRGXArrayToken(rv, true, acceptJSON) ||
152
+ (acceptJSON && isRGXJSONObjectToken(rv)) ||
153
+ isRGXConvertibleToken(rv, true, acceptJSON);
126
154
  }
127
155
  return false;
128
156
  }
129
157
  return false;
130
158
  }
131
- function assertRGXConvertibleToken(value, returnCheck = true) {
132
- if (!isRGXConvertibleToken(value, returnCheck)) {
159
+ function assertRGXConvertibleToken(value, returnCheck = true, acceptJSON = true) {
160
+ if (!isRGXConvertibleToken(value, returnCheck, acceptJSON)) {
133
161
  throw new e.RGXInvalidTokenError(`Invalid convertible token`, { type: "tokenType", values: ['convertible'] }, value);
134
162
  }
135
163
  }
136
- function isRGXArrayToken(value, contentCheck = true) {
164
+ function isRGXArrayToken(value, contentCheck = true, acceptJSON = true) {
137
165
  return Array.isArray(value) && (!contentCheck || value.every(item => isRGXNoOpToken(item) || isRGXLiteralToken(item) ||
138
166
  isRGXNativeToken(item) ||
139
- isRGXConvertibleToken(item) || isRGXArrayToken(item)));
167
+ (acceptJSON && isRGXJSONObjectToken(item)) ||
168
+ isRGXConvertibleToken(item, true, acceptJSON) || isRGXArrayToken(item, true, acceptJSON)));
140
169
  }
141
- function assertRGXArrayToken(value, contentCheck = true) {
142
- if (!isRGXArrayToken(value, contentCheck)) {
170
+ function assertRGXArrayToken(value, contentCheck = true, acceptJSON = true) {
171
+ if (!isRGXArrayToken(value, contentCheck, acceptJSON)) {
143
172
  throw new e.RGXInvalidTokenError("Invalid array token", { type: "tokenType", values: ['array'] }, value);
144
173
  }
145
174
  }
146
- 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) {
147
274
  if (isRGXNoOpToken(value))
148
275
  return 'no-op';
149
276
  if (isRGXLiteralToken(value))
@@ -153,18 +280,21 @@ function rgxTokenTypeFlat(value, recognizeClass = true) {
153
280
  // We have to check class before convertible because class tokens are also convertible.
154
281
  if (recognizeClass && class_1.RGXClassToken.check(value))
155
282
  return 'class';
156
- 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))
157
287
  return 'convertible';
158
- if (isRGXArrayToken(value))
288
+ if (isRGXArrayToken(value, true, acceptJSON))
159
289
  return 'array';
160
290
  throw new e.RGXInvalidTokenError("Invalid RGX token", null, value);
161
291
  }
162
- function rgxTokenType(value, recognizeClass = true) {
163
- const flatType = rgxTokenTypeFlat(value, recognizeClass);
292
+ function rgxTokenType(value, recognizeClass = true, acceptJSON = true) {
293
+ const flatType = rgxTokenTypeFlat(value, recognizeClass, acceptJSON);
164
294
  if (flatType !== 'array')
165
295
  return flatType;
166
296
  else
167
- return value.map(item => rgxTokenType(item, recognizeClass));
297
+ return value.map(item => rgxTokenType(item, recognizeClass, acceptJSON));
168
298
  }
169
299
  function rgxTokenFromType(type, value) {
170
300
  // Ignoring this line because the function is entirely a TypeScript utility that doesn't need to be tested at runtime.
@@ -189,9 +319,12 @@ function rgxTokenTypeGuardInputToFlat(type) {
189
319
  return null;
190
320
  return type;
191
321
  }
192
- function isRGXToken(value, type = null, matchLength = true) {
193
- if ((0, internal_1.isConstructor)(type))
194
- 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
+ }
195
328
  function typeMatches(s) {
196
329
  return type === null || type === s;
197
330
  }
@@ -199,50 +332,69 @@ function isRGXToken(value, type = null, matchLength = true) {
199
332
  return true;
200
333
  if (typeMatches('literal') && isRGXLiteralToken(value))
201
334
  return true;
335
+ if (typeMatches('literal') && acceptJSON && isRGXJSONLiteralToken(value))
336
+ return true;
202
337
  if (typeMatches('native') && isRGXNativeToken(value))
203
338
  return true;
204
339
  // We have to check class before convertible because class tokens are also convertible.
205
340
  if (typeMatches('class') && class_1.RGXClassToken.check(value))
206
341
  return true;
207
- if (typeMatches('convertible') && isRGXConvertibleToken(value))
342
+ if (typeMatches('class') && acceptJSON && isRGXJSONClassToken(value))
343
+ return true;
344
+ if (typeMatches('convertible') && isRGXConvertibleToken(value, true, acceptJSON))
208
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
+ }
209
358
  // Non-covertible tokens are repeatable by default. Convertible tokens are only non-repeatable if they have the rgxIsRepeatable property set to false.
210
359
  if (type === 'repeatable' && !isRGXConvertibleToken(value, false))
211
360
  return true;
212
- if (type === 'repeatable' && isRGXConvertibleToken(value))
361
+ if (type === 'repeatable' && isRGXConvertibleToken(value, true, acceptJSON))
213
362
  return value.rgxIsRepeatable ?? true;
214
- if (typeMatches('array') && isRGXArrayToken(value))
363
+ if (typeMatches('array') && isRGXArrayToken(value, true, acceptJSON))
215
364
  return true;
216
365
  if (Array.isArray(type) && Array.isArray(value) && (!matchLength || type.length === value.length)) {
217
366
  // This will always be false.
218
367
  if (value.length < type.length)
219
368
  return false;
220
369
  // @ts-ignore Excessively deep type is not a problem here.
221
- return value.every((item, i) => isRGXToken(item, type[i] ?? null));
370
+ return value.every((item, i) => isRGXToken(item, type[i] ?? null, true, acceptJSON));
222
371
  }
223
372
  return false;
224
373
  }
225
- function assertRGXToken(value, type = null, matchLength = true) {
226
- 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)) {
227
378
  const flatType = rgxTokenTypeGuardInputToFlat(type);
228
- /*
229
- ts-ignore
230
- 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.
231
- It's an excessively deep type error on the `value` variable, which is typed as `unknown`.
232
- */
233
- 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);
234
380
  }
235
381
  }
236
- 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);
237
389
  // Arrays and Literals are implicitly groups.
238
390
  // Classes are only groups if they have the isGroup property set to true.
239
- return (isRGXArrayToken(value, contentCheck) ||
240
- isRGXToken(value, "literal") ||
391
+ return (isRGXArrayToken(value, contentCheck, acceptJSON) ||
392
+ isRGXToken(value, "literal", true, false) ||
241
393
  (isRGXConvertibleToken(value, false) && (value.rgxIsGroup ||
242
- (value.rgxGroupWrap === true && (!contentCheck || isRGXGroupedToken(value.toRgx()))))));
394
+ (value.rgxGroupWrap === true && (!contentCheck || isRGXGroupedToken(value.toRgx(), true, acceptJSON))))));
243
395
  }
244
- function assertRGXGroupedToken(value, contentCheck = true) {
245
- if (!isRGXGroupedToken(value, contentCheck)) {
396
+ function assertRGXGroupedToken(value, contentCheck = true, acceptJSON = true) {
397
+ if (!isRGXGroupedToken(value, contentCheck, acceptJSON)) {
246
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);
247
399
  }
248
400
  }
@@ -277,6 +429,10 @@ function assertValidRegexString(value) {
277
429
  }
278
430
  }
279
431
  }
432
+ function castValidRegexString(value) {
433
+ assertValidRegexString(value);
434
+ return value;
435
+ }
280
436
  function isValidVanillaRegexFlags(value) {
281
437
  const patternMatch = /^[gimsuydv]*$/.test(value);
282
438
  if (!patternMatch)
@@ -290,6 +446,10 @@ function assertValidVanillaRegexFlags(value) {
290
446
  throw new e.RGXInvalidVanillaRegexFlagsError("Invalid vanilla regex flags", value);
291
447
  }
292
448
  }
449
+ function castValidVanillaRegexFlags(value) {
450
+ assertValidVanillaRegexFlags(value);
451
+ return value;
452
+ }
293
453
  function isValidIdentifier(value) {
294
454
  // This regex checks for valid JavaScript identifiers, which can be used for named capture groups.
295
455
  return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(value);
@@ -299,6 +459,10 @@ function assertValidIdentifier(value) {
299
459
  throw new e.RGXInvalidIdentifierError("Invalid identifier", value);
300
460
  }
301
461
  }
462
+ function castValidIdentifier(value) {
463
+ assertValidIdentifier(value);
464
+ return value;
465
+ }
302
466
  function isValidRegexLocalizableFlagDiff(value) {
303
467
  if (value === '')
304
468
  return true;
@@ -309,6 +473,10 @@ function assertValidRegexLocalizableFlagDiff(value) {
309
473
  throw new e.RGXInvalidRegexLocalizableFlagDiffError("Invalid localizable flag diff", value);
310
474
  }
311
475
  }
476
+ function castValidRegexLocalizableFlagDiff(value) {
477
+ assertValidRegexLocalizableFlagDiff(value);
478
+ return value;
479
+ }
312
480
  function isValidRegexLocalizableFlags(value) {
313
481
  return /^[ims]*$/.test(value);
314
482
  }
@@ -317,3 +485,7 @@ function assertValidRegexLocalizableFlags(value) {
317
485
  throw new e.RGXInvalidRegexLocalizableFlagsError("Invalid localizable flags", value);
318
486
  }
319
487
  }
488
+ function castValidRegexLocalizableFlags(value) {
489
+ assertValidRegexLocalizableFlags(value);
490
+ return value;
491
+ }
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.7.0",
3
+ "version": "13.9.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",