@ptolemy2002/rgx 2.9.0 → 3.1.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 +23 -11
- package/dist/errors/invalidToken.js +1 -0
- package/dist/internal/index.d.ts +1 -0
- package/dist/internal/index.js +1 -0
- package/dist/internal/isConstructor.d.ts +1 -0
- package/dist/internal/isConstructor.js +14 -0
- package/dist/typeGuards.d.ts +2 -2
- package/dist/typeGuards.js +16 -4
- package/dist/types.d.ts +6 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,6 +10,7 @@ type RGXLiteralToken = RegExp;
|
|
|
10
10
|
type RGXNativeToken = string | number | boolean | RGXNoOpToken;
|
|
11
11
|
type RGXConvertibleToken = { toRgx: () => RGXToken };
|
|
12
12
|
type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
|
|
13
|
+
type RGXClassTokenConstructor = new (...args: unknown[]) => RGXClassToken;
|
|
13
14
|
|
|
14
15
|
const validRegexSymbol = Symbol('rgx.ValidRegex');
|
|
15
16
|
type ValidRegexBrandSymbol = typeof validRegexSymbol;
|
|
@@ -19,10 +20,13 @@ const validVanillaRegexFlagsSymbol = Symbol('rgx.ValidVanillaRegexFlags');
|
|
|
19
20
|
type ValidVanillaRegexFlagsBrandSymbol = typeof validVanillaRegexFlagsSymbol;
|
|
20
21
|
type ValidVanillaRegexFlags = Branded<string, [ValidVanillaRegexFlagsBrandSymbol]>;
|
|
21
22
|
|
|
22
|
-
type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | RGXTokenType[];
|
|
23
|
+
type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | 'class' | RGXTokenType[];
|
|
23
24
|
type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
|
|
24
|
-
type RGXTokenTypeGuardInput = RGXTokenTypeFlat | null | RGXTokenTypeGuardInput[];
|
|
25
|
+
type RGXTokenTypeGuardInput = RGXTokenTypeFlat | null | RGXClassTokenConstructor | RGXTokenTypeGuardInput[];
|
|
25
26
|
type RGXTokenFromType<T extends RGXTokenTypeGuardInput> =
|
|
27
|
+
// Maps token type strings to their corresponding types, e.g.:
|
|
28
|
+
// 'no-op' -> RGXNoOpToken, 'literal' -> RGXLiteralToken, etc.
|
|
29
|
+
// Also maps RGXClassTokenConstructor to InstanceType<T>.
|
|
26
30
|
// ... see source for full definition
|
|
27
31
|
;
|
|
28
32
|
|
|
@@ -309,10 +313,12 @@ Asserts that the given value is an array of RGX tokens. When `contentCheck` is `
|
|
|
309
313
|
|
|
310
314
|
### rgxTokenType
|
|
311
315
|
```typescript
|
|
312
|
-
function rgxTokenType(value: unknown): RGXTokenType
|
|
316
|
+
function rgxTokenType(value: unknown, recognizeClass?: boolean): RGXTokenType
|
|
313
317
|
```
|
|
314
318
|
|
|
315
|
-
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.
|
|
319
|
+
Determines the type of a given RGX token value (`no-op`, `literal`, `native`, `convertible`, `class`, or an array of the former) or throws an error if the value is not a valid RGX token.
|
|
320
|
+
|
|
321
|
+
When `recognizeClass` is `true` (the default), `RGXClassToken` instances are identified as `'class'` rather than `'convertible'`. When `recognizeClass` is `false`, class tokens are identified as `'convertible'` instead. The `recognizeClass` preference is passed through to recursive calls for array elements.
|
|
316
322
|
|
|
317
323
|
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.
|
|
318
324
|
|
|
@@ -328,15 +334,18 @@ if (type === 'native') {
|
|
|
328
334
|
|
|
329
335
|
#### Parameters
|
|
330
336
|
- `value` (`unknown`): The value to check.
|
|
337
|
+
- `recognizeClass` (`boolean`, optional): Whether to recognize `RGXClassToken` instances as `'class'` instead of `'convertible'`. Defaults to `true`.
|
|
331
338
|
|
|
332
339
|
#### Returns
|
|
333
340
|
- `RGXTokenType`: The type of the RGX token.
|
|
334
341
|
|
|
335
342
|
### rgxTokenTypeFlat
|
|
336
343
|
```typescript
|
|
337
|
-
function rgxTokenTypeFlat(value: unknown): RGXTokenTypeFlat
|
|
344
|
+
function rgxTokenTypeFlat(value: unknown, recognizeClass?: boolean): RGXTokenTypeFlat
|
|
338
345
|
```
|
|
339
|
-
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.
|
|
346
|
+
Determines the flat type of a given RGX token value (`no-op`, `literal`, `native`, `convertible`, `class`, 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.
|
|
347
|
+
|
|
348
|
+
When `recognizeClass` is `true` (the default), `RGXClassToken` instances are identified as `'class'` rather than `'convertible'`. This distinction is important because class tokens are also convertible tokens, but the `'class'` type is more specific. When `recognizeClass` is `false`, class tokens are identified as `'convertible'` instead.
|
|
340
349
|
|
|
341
350
|
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.
|
|
342
351
|
|
|
@@ -351,6 +360,7 @@ if (type === 'array') {
|
|
|
351
360
|
|
|
352
361
|
#### Parameters
|
|
353
362
|
- `value` (`unknown`): The value to check.
|
|
363
|
+
- `recognizeClass` (`boolean`, optional): Whether to recognize `RGXClassToken` instances as `'class'` instead of `'convertible'`. Defaults to `true`.
|
|
354
364
|
|
|
355
365
|
#### Returns
|
|
356
366
|
- `RGXTokenTypeFlat`: The flat type of the RGX token.
|
|
@@ -387,7 +397,7 @@ Converts an `RGXTokenType` to its flat equivalent `RGXTokenTypeFlat`. If the typ
|
|
|
387
397
|
function rgxTokenTypeGuardInputToFlat(type: RGXTokenTypeGuardInput): RGXTokenTypeFlat | null
|
|
388
398
|
```
|
|
389
399
|
|
|
390
|
-
Converts an `RGXTokenTypeGuardInput` to its flat equivalent. If the type is `null`, it returns `null`; if it is an array, it returns `'array'`; otherwise, it returns the type as-is.
|
|
400
|
+
Converts an `RGXTokenTypeGuardInput` to its flat equivalent. If the type is `null`, it returns `null`; if it is an array, it returns `'array'`; if it is an `RGXClassTokenConstructor` (a constructor for an `RGXClassToken` subclass), it returns `'class'` (making it slightly lossy in that case); otherwise, it returns the type as-is.
|
|
391
401
|
|
|
392
402
|
#### Parameters
|
|
393
403
|
- `type` (`RGXTokenTypeGuardInput`): The type guard input to convert.
|
|
@@ -400,13 +410,15 @@ Converts an `RGXTokenTypeGuardInput` to its flat equivalent. If the type is `nul
|
|
|
400
410
|
function isRGXToken<T extends RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): value is RGXTokenFromType<T>
|
|
401
411
|
```
|
|
402
412
|
|
|
403
|
-
Checks if the given value is a valid RGX token, optionally narrowed to a specific token type. When `type` is `null` (the default), it checks against all token types. When `type` is a specific token type string, it checks only against that type.
|
|
413
|
+
Checks if the given value is a valid RGX token, optionally narrowed to a specific token type. When `type` is `null` (the default), it checks against all token types. When `type` is a specific token type string, it checks only against that type. The `'class'` type matches `RGXClassToken` instances specifically, while `'convertible'` also matches class tokens since they implement the convertible interface.
|
|
414
|
+
|
|
415
|
+
When `type` is an `RGXClassTokenConstructor` (a constructor for an `RGXClassToken` subclass), it performs an `instanceof` check against that specific constructor, allowing you to narrow to a specific class token subclass rather than all class tokens. In this case, `RGXTokenFromType` resolves to `InstanceType<T>`, giving you the specific subclass type.
|
|
404
416
|
|
|
405
417
|
When `type` is an array, it checks that every element of the value array is a valid RGX token matching the corresponding type in the `type` array. If `matchLength` is `true` (the default), it also requires that the value array has the same length as the type array; if `false`, it allows the value array to be longer than the type array, as long as all elements up to the length of the type array match and all elements after that are still valid RGX tokens of any type.
|
|
406
418
|
|
|
407
419
|
#### Parameters
|
|
408
420
|
- `value` (`unknown`): The value to check.
|
|
409
|
-
- `type` (`T`, optional): The token type to check against.
|
|
421
|
+
- `type` (`T`, optional): The token type to check against. Can be a token type string, `null` (checks against all token types), an `RGXClassTokenConstructor` (checks via `instanceof`), or an array of these. Defaults to `null`.
|
|
410
422
|
- `matchLength` (`boolean`, optional): When `type` is an array, whether to require that the value array has the same length as the type array. Defaults to `true`.
|
|
411
423
|
|
|
412
424
|
#### Returns
|
|
@@ -417,11 +429,11 @@ When `type` is an array, it checks that every element of the value array is a va
|
|
|
417
429
|
function assertRGXToken<T extends RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): asserts value is RGXTokenFromType<T>
|
|
418
430
|
```
|
|
419
431
|
|
|
420
|
-
Asserts that the given value is a valid RGX token, optionally narrowed to a specific token type. Uses the same logic as `isRGXToken`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
432
|
+
Asserts that the given value is a valid RGX token, optionally narrowed to a specific token type (including `'class'` for `RGXClassToken` instances, or a specific `RGXClassTokenConstructor` for `instanceof` checks). Uses the same logic as `isRGXToken`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
421
433
|
|
|
422
434
|
#### Parameters
|
|
423
435
|
- `value` (`unknown`): The value to assert.
|
|
424
|
-
- `type` (`T`, optional): The token type to assert against.
|
|
436
|
+
- `type` (`T`, optional): The token type to assert against. Can be a token type string, `null` (checks against all token types), an `RGXClassTokenConstructor` (checks via `instanceof`), or an array of these. Defaults to `null`.
|
|
425
437
|
- `matchLength` (`boolean`, optional): When `type` is an array, whether to require that the value array has the same length as the type array. Defaults to `true`.
|
|
426
438
|
|
|
427
439
|
#### Returns
|
|
@@ -10,6 +10,7 @@ const tokenExpectationMap = {
|
|
|
10
10
|
'native': ['string', 'number', 'boolean', 'null', 'undefined'],
|
|
11
11
|
'convertible': ['object with a toRgx method that returns a valid native/literal token or an array of valid native/literal tokens'],
|
|
12
12
|
'array': ['array of native/literal/convertible tokens'],
|
|
13
|
+
'class': ['instance of RGXClassToken']
|
|
13
14
|
};
|
|
14
15
|
class RGXInvalidTokenError extends errors_1.RGXError {
|
|
15
16
|
setExpected(expected) {
|
package/dist/internal/index.d.ts
CHANGED
package/dist/internal/index.js
CHANGED
|
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
__exportStar(require("./createConstructFunction"), exports);
|
|
18
18
|
__exportStar(require("./createClassGuardFunction"), exports);
|
|
19
19
|
__exportStar(require("./taggedTemplateToArray"), exports);
|
|
20
|
+
__exportStar(require("./isConstructor"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isConstructor(value: unknown): value is new (...args: unknown[]) => unknown;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isConstructor = isConstructor;
|
|
4
|
+
// This should work for our purposes
|
|
5
|
+
const handler = { construct() { return handler; } };
|
|
6
|
+
function isConstructor(value) {
|
|
7
|
+
try {
|
|
8
|
+
//@ts-expect-error If the value isn't constructable, the error will be caught.
|
|
9
|
+
return !!(new (new Proxy(value, handler))());
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
package/dist/typeGuards.d.ts
CHANGED
|
@@ -9,8 +9,8 @@ export declare function isRGXConvertibleToken(value: unknown, returnCheck?: bool
|
|
|
9
9
|
export declare function assertRGXConvertibleToken(value: unknown, returnCheck?: boolean): asserts value is t.RGXConvertibleToken;
|
|
10
10
|
export declare function isRGXArrayToken(value: unknown, contentCheck?: boolean): value is t.RGXToken[];
|
|
11
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;
|
|
12
|
+
export declare function rgxTokenTypeFlat(value: unknown, recognizeClass?: boolean): t.RGXTokenTypeFlat;
|
|
13
|
+
export declare function rgxTokenType(value: unknown, recognizeClass?: boolean): 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
|
@@ -58,7 +58,9 @@ exports.assertValidRegexString = assertValidRegexString;
|
|
|
58
58
|
exports.isValidVanillaRegexFlags = isValidVanillaRegexFlags;
|
|
59
59
|
exports.assertValidVanillaRegexFlags = assertValidVanillaRegexFlags;
|
|
60
60
|
const e = __importStar(require("./errors"));
|
|
61
|
+
const class_1 = require("./class");
|
|
61
62
|
const is_callable_1 = __importDefault(require("is-callable"));
|
|
63
|
+
const internal_1 = require("./internal");
|
|
62
64
|
function isRGXNoOpToken(value) {
|
|
63
65
|
return value === null || value === undefined;
|
|
64
66
|
}
|
|
@@ -112,25 +114,28 @@ function assertRGXArrayToken(value, contentCheck = true) {
|
|
|
112
114
|
throw new e.RGXInvalidTokenError("Invalid array token", { type: "tokenType", values: ['array'] }, value);
|
|
113
115
|
}
|
|
114
116
|
}
|
|
115
|
-
function rgxTokenTypeFlat(value) {
|
|
117
|
+
function rgxTokenTypeFlat(value, recognizeClass = true) {
|
|
116
118
|
if (isRGXNoOpToken(value))
|
|
117
119
|
return 'no-op';
|
|
118
120
|
if (isRGXLiteralToken(value))
|
|
119
121
|
return 'literal';
|
|
120
122
|
if (isRGXNativeToken(value))
|
|
121
123
|
return 'native';
|
|
124
|
+
// We have to check class before convertible because class tokens are also convertible.
|
|
125
|
+
if (recognizeClass && (0, class_1.isRgxClassToken)(value))
|
|
126
|
+
return 'class';
|
|
122
127
|
if (isRGXConvertibleToken(value))
|
|
123
128
|
return 'convertible';
|
|
124
129
|
if (isRGXArrayToken(value))
|
|
125
130
|
return 'array';
|
|
126
131
|
throw new e.RGXInvalidTokenError("Invalid RGX token", null, value);
|
|
127
132
|
}
|
|
128
|
-
function rgxTokenType(value) {
|
|
129
|
-
const flatType = rgxTokenTypeFlat(value);
|
|
133
|
+
function rgxTokenType(value, recognizeClass = true) {
|
|
134
|
+
const flatType = rgxTokenTypeFlat(value, recognizeClass);
|
|
130
135
|
if (flatType !== 'array')
|
|
131
136
|
return flatType;
|
|
132
137
|
else
|
|
133
|
-
return value.map(rgxTokenType);
|
|
138
|
+
return value.map(item => rgxTokenType(item, recognizeClass));
|
|
134
139
|
}
|
|
135
140
|
function rgxTokenFromType(type, value) {
|
|
136
141
|
// Ignoring this line because the function is entirely a TypeScript utility that doesn't need to be tested at runtime.
|
|
@@ -145,9 +150,13 @@ function rgxTokenTypeGuardInputToFlat(type) {
|
|
|
145
150
|
return null;
|
|
146
151
|
if (Array.isArray(type))
|
|
147
152
|
return 'array';
|
|
153
|
+
if ((0, internal_1.isConstructor)(type))
|
|
154
|
+
return 'class';
|
|
148
155
|
return type;
|
|
149
156
|
}
|
|
150
157
|
function isRGXToken(value, type = null, matchLength = true) {
|
|
158
|
+
if ((0, internal_1.isConstructor)(type))
|
|
159
|
+
return value instanceof type;
|
|
151
160
|
function typeMatches(s) {
|
|
152
161
|
return type === null || type === s;
|
|
153
162
|
}
|
|
@@ -157,6 +166,9 @@ function isRGXToken(value, type = null, matchLength = true) {
|
|
|
157
166
|
return true;
|
|
158
167
|
if (typeMatches('native') && isRGXNativeToken(value))
|
|
159
168
|
return true;
|
|
169
|
+
// We have to check class before convertible because class tokens are also convertible.
|
|
170
|
+
if (typeMatches('class') && (0, class_1.isRgxClassToken)(value))
|
|
171
|
+
return true;
|
|
160
172
|
if (typeMatches('convertible') && isRGXConvertibleToken(value))
|
|
161
173
|
return true;
|
|
162
174
|
if (typeMatches('array') && isRGXArrayToken(value))
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Branded } from "@ptolemy2002/ts-brand-utils";
|
|
2
|
+
import type { RGXClassToken } from "./class";
|
|
2
3
|
export type RGXNoOpToken = null | undefined;
|
|
3
4
|
export type RGXLiteralToken = RegExp;
|
|
4
5
|
export type RGXNativeToken = string | number | boolean | RGXNoOpToken;
|
|
@@ -6,12 +7,13 @@ export type RGXConvertibleToken = {
|
|
|
6
7
|
toRgx: () => RGXToken;
|
|
7
8
|
};
|
|
8
9
|
export type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
|
|
9
|
-
export type
|
|
10
|
+
export type RGXClassTokenConstructor = new (...args: unknown[]) => RGXClassToken;
|
|
11
|
+
export type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | 'class' | RGXTokenType[];
|
|
10
12
|
export type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
|
|
11
|
-
export type RGXTokenTypeGuardInput = RGXTokenTypeFlat | null | RGXTokenTypeGuardInput[];
|
|
12
|
-
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 'array' ? RGXToken[] : T extends RGXTokenTypeGuardInput[] ? {
|
|
13
|
+
export type RGXTokenTypeGuardInput = RGXTokenTypeFlat | null | RGXClassTokenConstructor | RGXTokenTypeGuardInput[];
|
|
14
|
+
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 RGXTokenTypeGuardInput[] ? {
|
|
13
15
|
[K in keyof T]: T[K] extends RGXTokenTypeGuardInput ? RGXTokenFromType<T[K]> : never;
|
|
14
|
-
} : never;
|
|
16
|
+
} : T extends RGXClassTokenConstructor ? InstanceType<T> : never;
|
|
15
17
|
export declare const validRegexSymbol: unique symbol;
|
|
16
18
|
export type ValidRegexBrandSymbol = typeof validRegexSymbol;
|
|
17
19
|
export type ValidRegexString = Branded<string, [ValidRegexBrandSymbol]>;
|