@ptolemy2002/rgx 2.9.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> =
@@ -309,10 +309,12 @@ Asserts that the given value is an array of RGX tokens. When `contentCheck` is `
309
309
 
310
310
  ### rgxTokenType
311
311
  ```typescript
312
- function rgxTokenType(value: unknown): RGXTokenType
312
+ function rgxTokenType(value: unknown, recognizeClass?: boolean): RGXTokenType
313
313
  ```
314
314
 
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.
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.
316
318
 
317
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.
318
320
 
@@ -328,15 +330,18 @@ if (type === 'native') {
328
330
 
329
331
  #### Parameters
330
332
  - `value` (`unknown`): The value to check.
333
+ - `recognizeClass` (`boolean`, optional): Whether to recognize `RGXClassToken` instances as `'class'` instead of `'convertible'`. Defaults to `true`.
331
334
 
332
335
  #### Returns
333
336
  - `RGXTokenType`: The type of the RGX token.
334
337
 
335
338
  ### rgxTokenTypeFlat
336
339
  ```typescript
337
- function rgxTokenTypeFlat(value: unknown): RGXTokenTypeFlat
340
+ function rgxTokenTypeFlat(value: unknown, recognizeClass?: boolean): RGXTokenTypeFlat
338
341
  ```
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.
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.
340
345
 
341
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.
342
347
 
@@ -351,6 +356,7 @@ if (type === 'array') {
351
356
 
352
357
  #### Parameters
353
358
  - `value` (`unknown`): The value to check.
359
+ - `recognizeClass` (`boolean`, optional): Whether to recognize `RGXClassToken` instances as `'class'` instead of `'convertible'`. Defaults to `true`.
354
360
 
355
361
  #### Returns
356
362
  - `RGXTokenTypeFlat`: The flat type of the RGX token.
@@ -400,7 +406,7 @@ Converts an `RGXTokenTypeGuardInput` to its flat equivalent. If the type is `nul
400
406
  function isRGXToken<T extends RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): value is RGXTokenFromType<T>
401
407
  ```
402
408
 
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.
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.
404
410
 
405
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.
406
412
 
@@ -417,7 +423,7 @@ When `type` is an array, it checks that every element of the value array is a va
417
423
  function assertRGXToken<T extends RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): asserts value is RGXTokenFromType<T>
418
424
  ```
419
425
 
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.
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.
421
427
 
422
428
  #### Parameters
423
429
  - `value` (`unknown`): The value to assert.
@@ -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) {
@@ -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.9.0",
3
+ "version": "3.0.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",