@ptolemy2002/rgx 3.0.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 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;
@@ -21,8 +22,11 @@ type ValidVanillaRegexFlags = Branded<string, [ValidVanillaRegexFlagsBrandSymbol
21
22
 
22
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
 
@@ -393,7 +397,7 @@ Converts an `RGXTokenType` to its flat equivalent `RGXTokenTypeFlat`. If the typ
393
397
  function rgxTokenTypeGuardInputToFlat(type: RGXTokenTypeGuardInput): RGXTokenTypeFlat | null
394
398
  ```
395
399
 
396
- 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.
397
401
 
398
402
  #### Parameters
399
403
  - `type` (`RGXTokenTypeGuardInput`): The type guard input to convert.
@@ -408,11 +412,13 @@ function isRGXToken<T extends RGXTokenTypeGuardInput = null>(value: unknown, typ
408
412
 
409
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.
410
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.
416
+
411
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.
412
418
 
413
419
  #### Parameters
414
420
  - `value` (`unknown`): The value to check.
415
- - `type` (`T`, optional): The token type to check against. Defaults to `null`, which checks against all token types.
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`.
416
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`.
417
423
 
418
424
  #### Returns
@@ -423,11 +429,11 @@ When `type` is an array, it checks that every element of the value array is a va
423
429
  function assertRGXToken<T extends RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): asserts value is RGXTokenFromType<T>
424
430
  ```
425
431
 
426
- Asserts that the given value is a valid RGX token, optionally narrowed to a specific token type (including `'class'` for `RGXClassToken` instances). 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.
427
433
 
428
434
  #### Parameters
429
435
  - `value` (`unknown`): The value to assert.
430
- - `type` (`T`, optional): The token type to assert against. Defaults to `null`, which checks against all token types.
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`.
431
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`.
432
438
 
433
439
  #### Returns
@@ -1,3 +1,4 @@
1
1
  export * from "./createConstructFunction";
2
2
  export * from "./createClassGuardFunction";
3
3
  export * from "./taggedTemplateToArray";
4
+ export * from "./isConstructor";
@@ -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
+ }
@@ -60,6 +60,7 @@ exports.assertValidVanillaRegexFlags = assertValidVanillaRegexFlags;
60
60
  const e = __importStar(require("./errors"));
61
61
  const class_1 = require("./class");
62
62
  const is_callable_1 = __importDefault(require("is-callable"));
63
+ const internal_1 = require("./internal");
63
64
  function isRGXNoOpToken(value) {
64
65
  return value === null || value === undefined;
65
66
  }
@@ -149,9 +150,13 @@ function rgxTokenTypeGuardInputToFlat(type) {
149
150
  return null;
150
151
  if (Array.isArray(type))
151
152
  return 'array';
153
+ if ((0, internal_1.isConstructor)(type))
154
+ return 'class';
152
155
  return type;
153
156
  }
154
157
  function isRGXToken(value, type = null, matchLength = true) {
158
+ if ((0, internal_1.isConstructor)(type))
159
+ return value instanceof type;
155
160
  function typeMatches(s) {
156
161
  return type === null || type === s;
157
162
  }
package/dist/types.d.ts CHANGED
@@ -7,12 +7,13 @@ export type RGXConvertibleToken = {
7
7
  toRgx: () => RGXToken;
8
8
  };
9
9
  export type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
10
+ export type RGXClassTokenConstructor = new (...args: unknown[]) => RGXClassToken;
10
11
  export type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | 'class' | RGXTokenType[];
11
12
  export type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
12
- export type RGXTokenTypeGuardInput = RGXTokenTypeFlat | null | RGXTokenTypeGuardInput[];
13
+ export type RGXTokenTypeGuardInput = RGXTokenTypeFlat | null | RGXClassTokenConstructor | RGXTokenTypeGuardInput[];
13
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[] ? {
14
15
  [K in keyof T]: T[K] extends RGXTokenTypeGuardInput ? RGXTokenFromType<T[K]> : never;
15
- } : never;
16
+ } : T extends RGXClassTokenConstructor ? InstanceType<T> : never;
16
17
  export declare const validRegexSymbol: unique symbol;
17
18
  export type ValidRegexBrandSymbol = typeof validRegexSymbol;
18
19
  export type ValidRegexString = Branded<string, [ValidRegexBrandSymbol]>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",