@ptolemy2002/rgx 2.7.0 → 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 +22 -20
- package/dist/class/base.d.ts +2 -2
- package/dist/resolve.js +1 -1
- package/dist/typeGuards.d.ts +6 -6
- package/dist/typeGuards.js +16 -20
- package/dist/types.d.ts +1 -3
- package/package.json +1 -1
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
|
|
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
|
|
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() =>
|
|
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,59 +254,63 @@ 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).
|
|
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).
|
|
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.
|
|
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.
|
|
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.
|
|
305
307
|
|
|
306
308
|
### rgxTokenType
|
|
307
309
|
```typescript
|
|
308
|
-
function rgxTokenType(value:
|
|
310
|
+
function rgxTokenType(value: unknown): RGXTokenType
|
|
309
311
|
```
|
|
310
312
|
|
|
311
|
-
Determines the type of a given RGX token (`no-op`, `literal`, `native`, `convertible`, or an array of the former).
|
|
313
|
+
Determines the type of a given RGX token value (`no-op`, `literal`, `native`, `convertible`, or an array of the former) or throws an error if the value is not a valid RGX token.
|
|
312
314
|
|
|
313
315
|
If you narrow the result of this function to something more specific, you can then convert these string or array literals into their corresponding token types using the `RGXTokenFromType` utility type or `rgxTokenFromType` function.
|
|
314
316
|
|
|
@@ -323,16 +325,16 @@ if (type === 'native') {
|
|
|
323
325
|
```
|
|
324
326
|
|
|
325
327
|
#### Parameters
|
|
326
|
-
- `value` (`
|
|
328
|
+
- `value` (`unknown`): The value to check.
|
|
327
329
|
|
|
328
330
|
#### Returns
|
|
329
331
|
- `RGXTokenType`: The type of the RGX token.
|
|
330
332
|
|
|
331
333
|
### rgxTokenTypeFlat
|
|
332
334
|
```typescript
|
|
333
|
-
function rgxTokenTypeFlat(value:
|
|
335
|
+
function rgxTokenTypeFlat(value: unknown): RGXTokenTypeFlat
|
|
334
336
|
```
|
|
335
|
-
Determines the flat type of a given RGX token (`no-op`, `literal`, `native`, `convertible`, or `array`). The `array` type represents any array of RGX tokens, regardless of the types of the individual tokens within the array.
|
|
337
|
+
Determines the flat type of a given RGX token value (`no-op`, `literal`, `native`, `convertible`, or `array`) or throws an error if the value is not a valid RGX token. The `array` type represents any array of RGX tokens, regardless of the types of the individual tokens within the array.
|
|
336
338
|
|
|
337
339
|
If you narrow the result of this function to something more specific, you can then convert these string literals into their corresponding token types using the `RGXTokenFromType` utility type or `rgxTokenFromType` function.
|
|
338
340
|
|
|
@@ -346,7 +348,7 @@ if (type === 'array') {
|
|
|
346
348
|
```
|
|
347
349
|
|
|
348
350
|
#### Parameters
|
|
349
|
-
- `value` (`
|
|
351
|
+
- `value` (`unknown`): The value to check.
|
|
350
352
|
|
|
351
353
|
#### Returns
|
|
352
354
|
- `RGXTokenTypeFlat`: The flat type of the RGX token.
|
package/dist/class/base.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
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():
|
|
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 (
|
|
59
|
+
if (tg.isRGXArrayToken(token, false)) {
|
|
60
60
|
if (token.length === 0)
|
|
61
61
|
return '';
|
|
62
62
|
if (token.length > 1) {
|
package/dist/typeGuards.d.ts
CHANGED
|
@@ -5,12 +5,12 @@ 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[];
|
|
12
|
-
export declare function rgxTokenTypeFlat(value:
|
|
13
|
-
export declare function rgxTokenType(value:
|
|
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
|
+
export declare function rgxTokenTypeFlat(value: unknown): t.RGXTokenTypeFlat;
|
|
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>;
|
|
15
15
|
export declare function rgxTokenTypeToFlat(type: t.RGXTokenType): t.RGXTokenTypeFlat;
|
|
16
16
|
export declare function rgxTokenTypeGuardInputToFlat(type: t.RGXTokenTypeGuardInput): t.RGXTokenTypeFlat | null;
|
package/dist/typeGuards.js
CHANGED
|
@@ -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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
}
|
|
@@ -120,21 +121,16 @@ function rgxTokenTypeFlat(value) {
|
|
|
120
121
|
return 'native';
|
|
121
122
|
if (isRGXConvertibleToken(value))
|
|
122
123
|
return 'convertible';
|
|
123
|
-
if (
|
|
124
|
+
if (isRGXArrayToken(value))
|
|
124
125
|
return 'array';
|
|
125
|
-
// Ignoring this line since it should be impossible to reach if the types are correct, but we need it to satisfy the return type
|
|
126
|
-
/* istanbul ignore next */
|
|
127
126
|
throw new e.RGXInvalidTokenError("Invalid RGX token", null, value);
|
|
128
127
|
}
|
|
129
128
|
function rgxTokenType(value) {
|
|
130
129
|
const flatType = rgxTokenTypeFlat(value);
|
|
131
130
|
if (flatType !== 'array')
|
|
132
131
|
return flatType;
|
|
133
|
-
|
|
132
|
+
else
|
|
134
133
|
return value.map(rgxTokenType);
|
|
135
|
-
// Ignoring this line since it should be impossible to reach if the types are correct, but we need it to satisfy the return type
|
|
136
|
-
/* istanbul ignore next */
|
|
137
|
-
throw new e.RGXInvalidTokenError("Invalid RGX token", null, value);
|
|
138
134
|
}
|
|
139
135
|
function rgxTokenFromType(type, value) {
|
|
140
136
|
// Ignoring this line because the function is entirely a TypeScript utility that doesn't need to be tested at runtime.
|
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: () =>
|
|
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[];
|