@ptolemy2002/rgx 2.8.0 → 3.0.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
@@ -19,7 +19,7 @@ const validVanillaRegexFlagsSymbol = Symbol('rgx.ValidVanillaRegexFlags');
19
19
  type ValidVanillaRegexFlagsBrandSymbol = typeof validVanillaRegexFlagsSymbol;
20
20
  type ValidVanillaRegexFlags = Branded<string, [ValidVanillaRegexFlagsBrandSymbol]>;
21
21
 
22
- type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | RGXTokenType[];
22
+ type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | 'class' | RGXTokenType[];
23
23
  type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
24
24
  type RGXTokenTypeGuardInput = RGXTokenTypeFlat | null | RGXTokenTypeGuardInput[];
25
25
  type RGXTokenFromType<T extends RGXTokenTypeGuardInput> =
@@ -140,6 +140,8 @@ Standard array properties and methods like `length`, `push`, `pop`, etc. are imp
140
140
  ### RGXClassToken (abstract)
141
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
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
+
143
145
  #### Abstract Methods
144
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).
145
147
 
@@ -153,7 +155,7 @@ An abstract base class for creating custom RGX token classes. Subclasses must im
153
155
  ### RGXClassUnionToken extends RGXClassToken
154
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.
155
157
 
156
- 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.
157
159
 
158
160
  #### Constructor
159
161
  ```typescript
@@ -307,10 +309,12 @@ Asserts that the given value is an array of RGX tokens. When `contentCheck` is `
307
309
 
308
310
  ### rgxTokenType
309
311
  ```typescript
310
- function rgxTokenType(value: unknown): RGXTokenType
312
+ function rgxTokenType(value: unknown, recognizeClass?: boolean): RGXTokenType
311
313
  ```
312
314
 
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.
315
+ 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.
316
+
317
+ 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.
314
318
 
315
319
  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.
316
320
 
@@ -326,15 +330,18 @@ if (type === 'native') {
326
330
 
327
331
  #### Parameters
328
332
  - `value` (`unknown`): The value to check.
333
+ - `recognizeClass` (`boolean`, optional): Whether to recognize `RGXClassToken` instances as `'class'` instead of `'convertible'`. Defaults to `true`.
329
334
 
330
335
  #### Returns
331
336
  - `RGXTokenType`: The type of the RGX token.
332
337
 
333
338
  ### rgxTokenTypeFlat
334
339
  ```typescript
335
- function rgxTokenTypeFlat(value: unknown): RGXTokenTypeFlat
340
+ function rgxTokenTypeFlat(value: unknown, recognizeClass?: boolean): RGXTokenTypeFlat
336
341
  ```
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.
342
+ 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.
343
+
344
+ 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.
338
345
 
339
346
  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.
340
347
 
@@ -349,6 +356,7 @@ if (type === 'array') {
349
356
 
350
357
  #### Parameters
351
358
  - `value` (`unknown`): The value to check.
359
+ - `recognizeClass` (`boolean`, optional): Whether to recognize `RGXClassToken` instances as `'class'` instead of `'convertible'`. Defaults to `true`.
352
360
 
353
361
  #### Returns
354
362
  - `RGXTokenTypeFlat`: The flat type of the RGX token.
@@ -398,7 +406,7 @@ Converts an `RGXTokenTypeGuardInput` to its flat equivalent. If the type is `nul
398
406
  function isRGXToken<T extends RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): value is RGXTokenFromType<T>
399
407
  ```
400
408
 
401
- 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.
409
+ 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.
402
410
 
403
411
  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.
404
412
 
@@ -415,7 +423,7 @@ When `type` is an array, it checks that every element of the value array is a va
415
423
  function assertRGXToken<T extends RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): asserts value is RGXTokenFromType<T>
416
424
  ```
417
425
 
418
- 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.
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.
419
427
 
420
428
  #### Parameters
421
429
  - `value` (`unknown`): The value to assert.
@@ -7,3 +7,5 @@ export declare abstract class RGXClassToken {
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,12 +3,14 @@ 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'],
9
10
  'native': ['string', 'number', 'boolean', 'null', 'undefined'],
10
11
  'convertible': ['object with a toRgx method that returns a valid native/literal token or an array of valid native/literal tokens'],
11
12
  'array': ['array of native/literal/convertible tokens'],
13
+ 'class': ['instance of RGXClassToken']
12
14
  };
13
15
  class RGXInvalidTokenError extends errors_1.RGXError {
14
16
  setExpected(expected) {
@@ -44,7 +46,8 @@ class RGXInvalidTokenError extends errors_1.RGXError {
44
46
  this.setExpected(expected);
45
47
  }
46
48
  toString() {
47
- return `${this.name}: ${this.message}; Expected: ${this.expected}; Got: ${JSON.stringify(this.got)}`;
49
+ const gotString = (0, class_1.isRgxClassToken)(this.got) ? `instance of ${this.got.constructor.name}` : JSON.stringify(this.got);
50
+ return `${this.name}: ${this.message}; Expected: ${this.expected}; Got: [${gotString}]`;
48
51
  }
49
52
  }
50
53
  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);
@@ -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;
@@ -58,6 +58,7 @@ 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"));
62
63
  function isRGXNoOpToken(value) {
63
64
  return value === null || value === undefined;
@@ -112,25 +113,28 @@ function assertRGXArrayToken(value, contentCheck = true) {
112
113
  throw new e.RGXInvalidTokenError("Invalid array token", { type: "tokenType", values: ['array'] }, value);
113
114
  }
114
115
  }
115
- function rgxTokenTypeFlat(value) {
116
+ function rgxTokenTypeFlat(value, recognizeClass = true) {
116
117
  if (isRGXNoOpToken(value))
117
118
  return 'no-op';
118
119
  if (isRGXLiteralToken(value))
119
120
  return 'literal';
120
121
  if (isRGXNativeToken(value))
121
122
  return 'native';
123
+ // We have to check class before convertible because class tokens are also convertible.
124
+ if (recognizeClass && (0, class_1.isRgxClassToken)(value))
125
+ return 'class';
122
126
  if (isRGXConvertibleToken(value))
123
127
  return 'convertible';
124
128
  if (isRGXArrayToken(value))
125
129
  return 'array';
126
130
  throw new e.RGXInvalidTokenError("Invalid RGX token", null, value);
127
131
  }
128
- function rgxTokenType(value) {
129
- const flatType = rgxTokenTypeFlat(value);
132
+ function rgxTokenType(value, recognizeClass = true) {
133
+ const flatType = rgxTokenTypeFlat(value, recognizeClass);
130
134
  if (flatType !== 'array')
131
135
  return flatType;
132
136
  else
133
- return value.map(rgxTokenType);
137
+ return value.map(item => rgxTokenType(item, recognizeClass));
134
138
  }
135
139
  function rgxTokenFromType(type, value) {
136
140
  // Ignoring this line because the function is entirely a TypeScript utility that doesn't need to be tested at runtime.
@@ -157,6 +161,9 @@ function isRGXToken(value, type = null, matchLength = true) {
157
161
  return true;
158
162
  if (typeMatches('native') && isRGXNativeToken(value))
159
163
  return true;
164
+ // We have to check class before convertible because class tokens are also convertible.
165
+ if (typeMatches('class') && (0, class_1.isRgxClassToken)(value))
166
+ return true;
160
167
  if (typeMatches('convertible') && isRGXConvertibleToken(value))
161
168
  return true;
162
169
  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,10 +7,10 @@ export type RGXConvertibleToken = {
6
7
  toRgx: () => RGXToken;
7
8
  };
8
9
  export type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
9
- export type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | RGXTokenType[];
10
+ export type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | 'class' | RGXTokenType[];
10
11
  export type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
11
12
  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 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
14
  [K in keyof T]: T[K] extends RGXTokenTypeGuardInput ? RGXTokenFromType<T[K]> : never;
14
15
  } : never;
15
16
  export declare const validRegexSymbol: unique symbol;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "2.8.0",
3
+ "version": "3.0.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",