@ptolemy2002/rgx 2.7.1 → 2.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/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,10 @@ 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).
144
142
 
145
143
  #### 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.
144
+ - `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
145
 
148
146
  #### Properties
149
147
  - `isGroup` (`boolean`): Returns `false` by default. Subclasses can override this to indicate whether the token represents a group.
@@ -256,49 +254,53 @@ Asserts that the given value is a native token (string, number, boolean, or no-o
256
254
 
257
255
  ### isRGXConvertibleToken
258
256
  ```typescript
259
- function isRGXConvertibleToken(value: unknown): value is RGXConvertibleToken
257
+ function isRGXConvertibleToken(value: unknown, returnCheck?: boolean): value is RGXConvertibleToken
260
258
  ```
261
259
 
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.
260
+ 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
261
 
264
262
  #### Parameters
265
263
  - `value` (`unknown`): The value to check.
264
+ - `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
265
 
267
266
  #### Returns
268
267
  - `boolean`: `true` if the value is a convertible token, otherwise `false`.
269
268
 
270
269
  ### assertRGXConvertibleToken
271
270
  ```typescript
272
- function assertRGXConvertibleToken(value: unknown): asserts value is RGXConvertibleToken
271
+ function assertRGXConvertibleToken(value: unknown, returnCheck?: boolean): asserts value is RGXConvertibleToken
273
272
  ```
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.
273
+ 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
274
 
276
275
  #### Parameters
277
276
  - `value` (`unknown`): The value to assert.
277
+ - `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
278
 
279
279
  #### Returns
280
280
  - `void`: This function does not return a value, but will throw an error if the assertion fails.
281
281
 
282
282
  ### isRGXArrayToken
283
283
  ```typescript
284
- function isRGXArrayToken(value: unknown): value is RGXToken[]
284
+ function isRGXArrayToken(value: unknown, contentCheck?: boolean): value is RGXToken[]
285
285
  ```
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).
286
+ 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
287
 
288
288
  #### Parameters
289
289
  - `value` (`unknown`): The value to check.
290
+ - `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
291
 
291
292
  #### Returns
292
- - `boolean`: `true` if the value is an array of RGX tokens, otherwise `false`.
293
+ - `boolean`: `true` if the value is an array of RGX tokens (or just an array when `contentCheck` is `false`), otherwise `false`.
293
294
 
294
295
  ### assertRGXArrayToken
295
296
  ```typescript
296
- function assertRGXArrayToken(value: unknown): asserts value is RGXToken[]
297
+ function assertRGXArrayToken(value: unknown, contentCheck?: boolean): asserts value is RGXToken[]
297
298
  ```
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.
299
+ 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
300
 
300
301
  #### Parameters
301
302
  - `value` (`unknown`): The value to assert.
303
+ - `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
304
 
303
305
  #### Returns
304
306
  - `void`: This function does not return a value, but will throw an error if the assertion fails.
@@ -1,8 +1,8 @@
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;
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.8.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",