@ptolemy2002/rgx 2.7.1 → 2.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.
package/README.md CHANGED
@@ -4,13 +4,11 @@ A library for easy construction and validation of regular expressions in TypeScr
4
4
  ## Type Reference
5
5
  ```typescript
6
6
  import { Branded } from "@ptolemy2002/ts-brand-utils";
7
- import { MaybeArray } from "@ptolemy2002/ts-utils";
8
7
 
9
8
  type RGXNoOpToken = null | undefined;
10
9
  type RGXLiteralToken = RegExp;
11
10
  type RGXNativeToken = string | number | boolean | RGXNoOpToken;
12
- type RGXConvertibleTokenOutput = MaybeArray<RGXNativeToken | RGXLiteralToken>;
13
- type RGXConvertibleToken = { toRgx: () => RGXConvertibleTokenOutput };
11
+ type RGXConvertibleToken = { toRgx: () => RGXToken };
14
12
  type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
15
13
 
16
14
  const validRegexSymbol = Symbol('rgx.ValidRegex');
@@ -140,10 +138,12 @@ constructor(tokens: RGXTokenCollectionInput = [], mode: RGXTokenCollectionMode =
140
138
  Standard array properties and methods like `length`, `push`, `pop`, etc. are implemented to work with the internal `tokens` array, but providing collection instances instead of raw arrays when relevant (e.g., `map` has the third parameter typed as `RGXTokenCollection` instead of `RGXToken[]`).
141
139
 
142
140
  ### RGXClassToken (abstract)
143
- An abstract base class for creating custom RGX token classes. Subclasses must implement the `toRgx()` method, which returns a value compatible with `RGXConvertibleTokenOutput`.
141
+ An abstract base class for creating custom RGX token classes. Subclasses must implement the `toRgx()` method, which returns any valid `RGXToken` (including other convertible tokens, allowing for recursive structures).
142
+
143
+ Two type guards are provided for this class: `isRgxClassToken` to check if a value is an instance of `RGXClassToken`, and `assertRgxClassToken` to assert that a value is an instance of `RGXClassToken` (throwing an `InvalidTokenError` if the assertion fails). Both of these take a single parameter of type `unknown` for the value.
144
144
 
145
145
  #### Abstract Methods
146
- - `toRgx() => RGXConvertibleTokenOutput`: Must be implemented by subclasses to return the token's regex representation as a native/literal token or array of native/literal tokens.
146
+ - `toRgx() => RGXToken`: Must be implemented by subclasses to return the token's regex representation as any valid RGX token (native, literal, convertible, or array of tokens).
147
147
 
148
148
  #### Properties
149
149
  - `isGroup` (`boolean`): Returns `false` by default. Subclasses can override this to indicate whether the token represents a group.
@@ -155,7 +155,7 @@ An abstract base class for creating custom RGX token classes. Subclasses must im
155
155
  ### RGXClassUnionToken extends RGXClassToken
156
156
  A class representing a union (alternation) of RGX tokens. This is typically created via the `or()` method on `RGXClassToken`, but can also be instantiated directly.
157
157
 
158
- A function `rgxClassUnion` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
158
+ A function `rgxClassUnion` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword. Two type guards are provided for this class: `isRgxClassUnionToken` to check if a value is an instance of `RGXClassUnionToken`, and `assertRgxClassUnionToken` to assert that a value is an instance of `RGXClassUnionToken` (throwing an `InvalidTokenError` if the assertion fails). Both of these take a single parameter of type `unknown` for the value.
159
159
 
160
160
  #### Constructor
161
161
  ```typescript
@@ -256,49 +256,53 @@ Asserts that the given value is a native token (string, number, boolean, or no-o
256
256
 
257
257
  ### isRGXConvertibleToken
258
258
  ```typescript
259
- function isRGXConvertibleToken(value: unknown): value is RGXConvertibleToken
259
+ function isRGXConvertibleToken(value: unknown, returnCheck?: boolean): value is RGXConvertibleToken
260
260
  ```
261
261
 
262
- Checks if the given value is a convertible token (an object with a `toRgx` method). Validates that `toRgx` is callable and returns a valid RGX native or literal token, or an array of native/literal tokens.
262
+ Checks if the given value is a convertible token (an object with a `toRgx` method). When `returnCheck` is `true` (the default), also validates that `toRgx` is callable and returns a valid `RGXToken` (which can be any RGX token type, including other convertible tokens, allowing for recursive structures).
263
263
 
264
264
  #### Parameters
265
265
  - `value` (`unknown`): The value to check.
266
+ - `returnCheck` (`boolean`, optional): Whether to validate the return value of the `toRgx` method. Defaults to `true`. When `false`, only checks that `toRgx` exists and is callable. **Note**: Setting this to `false` makes the type guard assertion strictly unsafe, as it doesn't verify that the `toRgx` method actually returns a valid `RGXToken`. However, depending on the type of the value you're checking, you might not need that safety (e.g., when checking values that you know are valid based on other context).
266
267
 
267
268
  #### Returns
268
269
  - `boolean`: `true` if the value is a convertible token, otherwise `false`.
269
270
 
270
271
  ### assertRGXConvertibleToken
271
272
  ```typescript
272
- function assertRGXConvertibleToken(value: unknown): asserts value is RGXConvertibleToken
273
+ function assertRGXConvertibleToken(value: unknown, returnCheck?: boolean): asserts value is RGXConvertibleToken
273
274
  ```
274
- Asserts that the given value is a convertible token (an object with a `toRgx` method). Validates that `toRgx` is callable and returns a valid RGX native or literal token, or an array of native/literal tokens. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
275
+ Asserts that the given value is a convertible token (an object with a `toRgx` method). When `returnCheck` is `true` (the default), also validates that `toRgx` is callable and returns a valid `RGXToken` (which can be any RGX token type, including other convertible tokens, allowing for recursive structures). If the assertion fails, an `RGXInvalidTokenError` will be thrown.
275
276
 
276
277
  #### Parameters
277
278
  - `value` (`unknown`): The value to assert.
279
+ - `returnCheck` (`boolean`, optional): Whether to validate the return value of the `toRgx` method. Defaults to `true`. When `false`, only checks that `toRgx` exists and is callable. **Note**: Setting this to `false` makes the type guard assertion strictly unsafe, as it doesn't verify that the `toRgx` method actually returns a valid `RGXToken`. However, depending on the type of the value you're asserting, you might not need that safety (e.g., when asserting values that you know are valid based on other context).
278
280
 
279
281
  #### Returns
280
282
  - `void`: This function does not return a value, but will throw an error if the assertion fails.
281
283
 
282
284
  ### isRGXArrayToken
283
285
  ```typescript
284
- function isRGXArrayToken(value: unknown): value is RGXToken[]
286
+ function isRGXArrayToken(value: unknown, contentCheck?: boolean): value is RGXToken[]
285
287
  ```
286
- Checks if the given value is an array of RGX tokens. Validates that the value is an array and that every element is a valid RGX token (of any type, including nested arrays).
288
+ Checks if the given value is an array of RGX tokens. When `contentCheck` is `true` (the default), validates that the value is an array and that every element is a valid RGX token (of any type, including nested arrays). When `contentCheck` is `false`, only checks that the value is an array without validating the contents.
287
289
 
288
290
  #### Parameters
289
291
  - `value` (`unknown`): The value to check.
292
+ - `contentCheck` (`boolean`, optional): Whether to validate that every element is a valid RGX token. Defaults to `true`. When `false`, only checks that the value is an array. **Note**: Setting this to `false` makes the type guard assertion strictly unsafe, as it doesn't verify that the array elements are actually valid `RGXToken` values. However, depending on the context, you might not need that safety (e.g., when checking arrays that you know are valid based on other validation).
290
293
 
291
294
  #### Returns
292
- - `boolean`: `true` if the value is an array of RGX tokens, otherwise `false`.
295
+ - `boolean`: `true` if the value is an array of RGX tokens (or just an array when `contentCheck` is `false`), otherwise `false`.
293
296
 
294
297
  ### assertRGXArrayToken
295
298
  ```typescript
296
- function assertRGXArrayToken(value: unknown): asserts value is RGXToken[]
299
+ function assertRGXArrayToken(value: unknown, contentCheck?: boolean): asserts value is RGXToken[]
297
300
  ```
298
- Asserts that the given value is an array of RGX tokens. Validates that the value is an array and that every element is a valid RGX token (of any type, including nested arrays). If the assertion fails, an `RGXInvalidTokenError` will be thrown.
301
+ Asserts that the given value is an array of RGX tokens. When `contentCheck` is `true` (the default), validates that the value is an array and that every element is a valid RGX token (of any type, including nested arrays). When `contentCheck` is `false`, only checks that the value is an array without validating the contents. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
299
302
 
300
303
  #### Parameters
301
304
  - `value` (`unknown`): The value to assert.
305
+ - `contentCheck` (`boolean`, optional): Whether to validate that every element is a valid RGX token. Defaults to `true`. When `false`, only checks that the value is an array. **Note**: Setting this to `false` makes the type guard assertion strictly unsafe, as it doesn't verify that the array elements are actually valid `RGXToken` values. However, depending on the context, you might not need that safety (e.g., when asserting arrays that you know are valid based on other validation).
302
306
 
303
307
  #### Returns
304
308
  - `void`: This function does not return a value, but will throw an error if the assertion fails.
@@ -1,9 +1,11 @@
1
- import { RGXConvertibleTokenOutput, ValidRegexString } from "../types";
1
+ import { RGXToken, ValidRegexString } from "../types";
2
2
  import { RGXTokenCollectionInput } from "../collection";
3
3
  import type { RGXClassUnionToken } from "./union";
4
4
  export declare abstract class RGXClassToken {
5
- abstract toRgx(): RGXConvertibleTokenOutput;
5
+ abstract toRgx(): RGXToken;
6
6
  get isGroup(): boolean;
7
7
  or(...others: RGXTokenCollectionInput[]): RGXClassUnionToken;
8
8
  resolve(): ValidRegexString;
9
9
  }
10
+ export declare const isRgxClassToken: (value: unknown) => value is RGXClassToken;
11
+ export declare const assertRgxClassToken: (value: unknown) => asserts value is RGXClassToken;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RGXClassToken = void 0;
3
+ exports.assertRgxClassToken = exports.isRgxClassToken = exports.RGXClassToken = void 0;
4
4
  const errors_1 = require("../errors");
5
5
  const resolve_1 = require("../resolve");
6
6
  class RGXClassToken {
@@ -15,3 +15,13 @@ class RGXClassToken {
15
15
  }
16
16
  }
17
17
  exports.RGXClassToken = RGXClassToken;
18
+ // The createClassGuard function only accepts non-abstract classes, so we
19
+ // manually define the guard and assertion functions for RGXClassToken here.
20
+ const isRgxClassToken = (value) => value instanceof RGXClassToken;
21
+ exports.isRgxClassToken = isRgxClassToken;
22
+ const assertRgxClassToken = (value) => {
23
+ if (!(value instanceof RGXClassToken)) {
24
+ throw new errors_1.RGXInvalidTokenError("Invalid token type", { type: "custom", values: ["instance of RGXClassToken"] }, value);
25
+ }
26
+ };
27
+ exports.assertRgxClassToken = assertRgxClassToken;
@@ -14,3 +14,5 @@ export declare class RGXClassUnionToken extends RGXClassToken {
14
14
  export declare function expandRgxUnionTokens(...tokens: RGXTokenCollectionInput[]): RGXTokenCollection;
15
15
  export declare function removeRgxUnionDuplicates(...tokens: RGXTokenCollectionInput[]): RGXTokenCollection;
16
16
  export declare const rgxClassUnion: (tokens?: RGXTokenCollectionInput) => RGXClassUnionToken;
17
+ export declare const isRgxClassUnionToken: (value: unknown) => value is RGXClassUnionToken;
18
+ export declare const assertRgxClassUnionToken: (value: unknown) => asserts value is RGXClassUnionToken;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.rgxClassUnion = exports.RGXClassUnionToken = void 0;
3
+ exports.assertRgxClassUnionToken = exports.isRgxClassUnionToken = exports.rgxClassUnion = exports.RGXClassUnionToken = void 0;
4
4
  exports.expandRgxUnionTokens = expandRgxUnionTokens;
5
5
  exports.removeRgxUnionDuplicates = removeRgxUnionDuplicates;
6
6
  const internal_1 = require("../internal");
@@ -87,3 +87,5 @@ function removeRgxUnionDuplicates(...tokens) {
87
87
  return new collection_1.RGXTokenCollection(uniqueTokens, 'union');
88
88
  }
89
89
  exports.rgxClassUnion = (0, internal_1.createConstructFunction)(RGXClassUnionToken);
90
+ exports.isRgxClassUnionToken = (0, internal_1.createClassGuardFunction)(RGXClassUnionToken);
91
+ exports.assertRgxClassUnionToken = (0, internal_1.createAssertClassGuardFunction)(RGXClassUnionToken);
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RGXInvalidTokenError = void 0;
4
4
  const errors_1 = require("./");
5
5
  const js_utils_1 = require("@ptolemy2002/js-utils");
6
+ const class_1 = require("../class");
6
7
  const tokenExpectationMap = {
7
8
  'no-op': ['null', 'undefined'],
8
9
  'literal': ['RegExp'],
@@ -44,7 +45,8 @@ class RGXInvalidTokenError extends errors_1.RGXError {
44
45
  this.setExpected(expected);
45
46
  }
46
47
  toString() {
47
- return `${this.name}: ${this.message}; Expected: ${this.expected}; Got: ${JSON.stringify(this.got)}`;
48
+ const gotString = (0, class_1.isRgxClassToken)(this.got) ? `instance of ${this.got.constructor.name}` : JSON.stringify(this.got);
49
+ return `${this.name}: ${this.message}; Expected: ${this.expected}; Got: [${gotString}]`;
48
50
  }
49
51
  }
50
52
  exports.RGXInvalidTokenError = RGXInvalidTokenError;
@@ -0,0 +1,2 @@
1
+ export declare function createClassGuardFunction<T extends new (...args: unknown[]) => unknown>(constructor: T): (value: unknown) => value is InstanceType<T>;
2
+ export declare function createAssertClassGuardFunction<T extends new (...args: unknown[]) => unknown>(constructor: T): (value: unknown) => asserts value is InstanceType<T>;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createClassGuardFunction = createClassGuardFunction;
4
+ exports.createAssertClassGuardFunction = createAssertClassGuardFunction;
5
+ const errors_1 = require("../errors");
6
+ function createClassGuardFunction(constructor) {
7
+ return (value) => value instanceof constructor;
8
+ }
9
+ function createAssertClassGuardFunction(constructor) {
10
+ return (value) => {
11
+ if (!(value instanceof constructor)) {
12
+ throw new errors_1.RGXInvalidTokenError("Invalid token type", { type: "custom", values: [`instance of ${constructor.name}`] }, value);
13
+ }
14
+ };
15
+ }
@@ -1,2 +1,3 @@
1
1
  export * from "./createConstructFunction";
2
+ export * from "./createClassGuardFunction";
2
3
  export * from "./taggedTemplateToArray";
@@ -15,4 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./createConstructFunction"), exports);
18
+ __exportStar(require("./createClassGuardFunction"), exports);
18
19
  __exportStar(require("./taggedTemplateToArray"), exports);
package/dist/resolve.js CHANGED
@@ -56,7 +56,7 @@ function resolveRGXToken(token, groupWrap = true) {
56
56
  return resolveRGXToken(token.toRgx(), groupWrap);
57
57
  }
58
58
  // Interpret arrays as unions
59
- if (Array.isArray(token)) {
59
+ if (tg.isRGXArrayToken(token, false)) {
60
60
  if (token.length === 0)
61
61
  return '';
62
62
  if (token.length > 1) {
@@ -5,10 +5,10 @@ export declare function isRGXLiteralToken(value: unknown): value is t.RGXLiteral
5
5
  export declare function assertRGXLiteralToken(value: unknown): asserts value is t.RGXLiteralToken;
6
6
  export declare function isRGXNativeToken(value: unknown): value is t.RGXNativeToken;
7
7
  export declare function assertRGXNativeToken(value: unknown): asserts value is t.RGXNativeToken;
8
- export declare function isRGXConvertibleToken(value: unknown): value is t.RGXConvertibleToken;
9
- export declare function assertRGXConvertibleToken(value: unknown): asserts value is t.RGXConvertibleToken;
10
- export declare function isRGXArrayToken(value: unknown): value is t.RGXToken[];
11
- export declare function assertRGXArrayToken(value: unknown): asserts value is t.RGXToken[];
8
+ export declare function isRGXConvertibleToken(value: unknown, returnCheck?: boolean): value is t.RGXConvertibleToken;
9
+ export declare function assertRGXConvertibleToken(value: unknown, returnCheck?: boolean): asserts value is t.RGXConvertibleToken;
10
+ export declare function isRGXArrayToken(value: unknown, contentCheck?: boolean): value is t.RGXToken[];
11
+ export declare function assertRGXArrayToken(value: unknown, contentCheck?: boolean): asserts value is t.RGXToken[];
12
12
  export declare function rgxTokenTypeFlat(value: unknown): t.RGXTokenTypeFlat;
13
13
  export declare function rgxTokenType(value: unknown): t.RGXTokenType;
14
14
  export declare function rgxTokenFromType<T extends t.RGXTokenTypeGuardInput>(type: T, value: t.RGXToken): t.RGXTokenFromType<T>;
@@ -83,31 +83,32 @@ function assertRGXNativeToken(value) {
83
83
  throw new e.RGXInvalidTokenError("Invalid native token", { type: "tokenType", values: ['native'] }, value);
84
84
  }
85
85
  }
86
- function isRGXConvertibleToken(value) {
86
+ function isRGXConvertibleToken(value, returnCheck = true) {
87
87
  if (typeof value === 'object' && value !== null && 'toRgx' in value) {
88
88
  if ((0, is_callable_1.default)(value.toRgx)) {
89
- const returnValue = value.toRgx();
90
- if (Array.isArray(returnValue)) {
91
- return returnValue.every(value => isRGXNativeToken(value) || isRGXLiteralToken(value));
92
- }
93
- return isRGXNativeToken(returnValue) || isRGXLiteralToken(returnValue);
89
+ if (!returnCheck)
90
+ return true;
91
+ const rv = value.toRgx();
92
+ return isRGXNativeToken(rv) || isRGXLiteralToken(rv) ||
93
+ isRGXNoOpToken(rv) || isRGXArrayToken(rv) ||
94
+ isRGXConvertibleToken(rv);
94
95
  }
95
96
  return false;
96
97
  }
97
98
  return false;
98
99
  }
99
- function assertRGXConvertibleToken(value) {
100
- if (!isRGXConvertibleToken(value)) {
100
+ function assertRGXConvertibleToken(value, returnCheck = true) {
101
+ if (!isRGXConvertibleToken(value, returnCheck)) {
101
102
  throw new e.RGXInvalidTokenError(`Invalid convertible token`, { type: "tokenType", values: ['convertible'] }, value);
102
103
  }
103
104
  }
104
- function isRGXArrayToken(value) {
105
- return Array.isArray(value) && value.every(item => isRGXNoOpToken(item) || isRGXLiteralToken(item) ||
105
+ function isRGXArrayToken(value, contentCheck = true) {
106
+ return Array.isArray(value) && (!contentCheck || value.every(item => isRGXNoOpToken(item) || isRGXLiteralToken(item) ||
106
107
  isRGXNativeToken(item) ||
107
- isRGXConvertibleToken(item) || isRGXArrayToken(item));
108
+ isRGXConvertibleToken(item) || isRGXArrayToken(item)));
108
109
  }
109
- function assertRGXArrayToken(value) {
110
- if (!isRGXArrayToken(value)) {
110
+ function assertRGXArrayToken(value, contentCheck = true) {
111
+ if (!isRGXArrayToken(value, contentCheck)) {
111
112
  throw new e.RGXInvalidTokenError("Invalid array token", { type: "tokenType", values: ['array'] }, value);
112
113
  }
113
114
  }
package/dist/types.d.ts CHANGED
@@ -1,11 +1,9 @@
1
1
  import { Branded } from "@ptolemy2002/ts-brand-utils";
2
- import { MaybeArray } from "@ptolemy2002/ts-utils";
3
2
  export type RGXNoOpToken = null | undefined;
4
3
  export type RGXLiteralToken = RegExp;
5
4
  export type RGXNativeToken = string | number | boolean | RGXNoOpToken;
6
- export type RGXConvertibleTokenOutput = MaybeArray<RGXNativeToken | RGXLiteralToken>;
7
5
  export type RGXConvertibleToken = {
8
- toRgx: () => RGXConvertibleTokenOutput;
6
+ toRgx: () => RGXToken;
9
7
  };
10
8
  export type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
11
9
  export type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | RGXTokenType[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "2.7.1",
3
+ "version": "2.9.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",