@ptolemy2002/rgx 2.5.2 → 2.6.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
@@ -23,7 +23,8 @@ type ValidVanillaRegexFlags = Branded<string, [ValidVanillaRegexFlagsBrandSymbol
23
23
 
24
24
  type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | RGXTokenType[];
25
25
  type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
26
- type RGXTokenFromType<T extends RGXTokenType | RGXTokenTypeFlat> =
26
+ type RGXTokenTypeGuardInput = Exclude<RGXTokenType, RGXTokenType[]> | RGXTokenTypeFlat | null | RGXTokenTypeGuardInput[];
27
+ type RGXTokenFromType<T extends RGXTokenTypeGuardInput> =
27
28
  // ... see source for full definition
28
29
  ;
29
30
 
@@ -340,6 +341,64 @@ Does nothing at runtime, but performs a type assertion to the correct subset of
340
341
  #### Returns
341
342
  - `RGXTokenFromType<T>`: The input value, but with its type asserted to the corresponding token type based on the provided `RGXTokenType`.
342
343
 
344
+ ### rgxTokenTypeToFlat
345
+ ```typescript
346
+ function rgxTokenTypeToFlat(type: RGXTokenType): RGXTokenTypeFlat
347
+ ```
348
+
349
+ Converts an `RGXTokenType` to its flat equivalent `RGXTokenTypeFlat`. If the type is an array, it returns `'array'`; otherwise, it returns the type as-is.
350
+
351
+ #### Parameters
352
+ - `type` (`RGXTokenType`): The RGX token type to convert.
353
+
354
+ #### Returns
355
+ - `RGXTokenTypeFlat`: The flat equivalent of the provided token type.
356
+
357
+ ### rgxTokenTypeGuardInputToFlat
358
+ ```typescript
359
+ function rgxTokenTypeGuardInputToFlat(type: RGXTokenTypeGuardInput): RGXTokenTypeFlat | null
360
+ ```
361
+
362
+ 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.
363
+
364
+ #### Parameters
365
+ - `type` (`RGXTokenTypeGuardInput`): The type guard input to convert.
366
+
367
+ #### Returns
368
+ - `RGXTokenTypeFlat | null`: The flat equivalent of the provided type guard input, or `null` if the input is `null`.
369
+
370
+ ### isRGXToken
371
+ ```typescript
372
+ function isRGXToken<T extends RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): value is RGXTokenFromType<T>
373
+ ```
374
+
375
+ 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.
376
+
377
+ 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.
378
+
379
+ #### Parameters
380
+ - `value` (`unknown`): The value to check.
381
+ - `type` (`T`, optional): The token type to check against. Defaults to `null`, which checks against all token types.
382
+ - `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`.
383
+
384
+ #### Returns
385
+ - `boolean`: `true` if the value is a valid RGX token matching the specified type, otherwise `false`.
386
+
387
+ ### assertRGXToken
388
+ ```typescript
389
+ function assertRGXToken<T extends RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): asserts value is RGXTokenFromType<T>
390
+ ```
391
+
392
+ 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.
393
+
394
+ #### Parameters
395
+ - `value` (`unknown`): The value to assert.
396
+ - `type` (`T`, optional): The token type to assert against. Defaults to `null`, which checks against all token types.
397
+ - `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`.
398
+
399
+ #### Returns
400
+ - `void`: This function does not return a value, but will throw an error if the assertion fails.
401
+
343
402
  ### isValidRegexString
344
403
  ```typescript
345
404
  function isValidRegexString(value: string): value is ValidRegexString
@@ -7,8 +7,8 @@ const tokenExpectationMap = {
7
7
  'no-op': ['null', 'undefined'],
8
8
  'literal': ['RegExp'],
9
9
  'native': ['string', 'number', 'boolean', 'null', 'undefined'],
10
- 'convertible': ['object with a toRgx method that returns a valid token'],
11
- 'array': ['array of native/literal tokens'],
10
+ 'convertible': ['object with a toRgx method that returns a valid native/literal token or an array of valid native/literal tokens'],
11
+ 'array': ['array of native/literal/convertible tokens'],
12
12
  };
13
13
  class RGXInvalidTokenError extends errors_1.RGXError {
14
14
  setExpected(expected) {
@@ -10,6 +10,10 @@ export declare function assertRGXConvertibleToken(value: unknown): asserts value
10
10
  export declare function rgxTokenTypeFlat(value: t.RGXToken): t.RGXTokenTypeFlat;
11
11
  export declare function rgxTokenType(value: t.RGXToken): t.RGXTokenType;
12
12
  export declare function rgxTokenFromType<T extends t.RGXTokenType | t.RGXTokenTypeFlat>(type: T, value: t.RGXToken): t.RGXTokenFromType<T>;
13
+ export declare function rgxTokenTypeToFlat(type: t.RGXTokenType): t.RGXTokenTypeFlat;
14
+ export declare function rgxTokenTypeGuardInputToFlat(type: t.RGXTokenTypeGuardInput): t.RGXTokenTypeFlat | null;
15
+ export declare function isRGXToken<T extends t.RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): value is t.RGXTokenFromType<T>;
16
+ export declare function assertRGXToken<T extends t.RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): asserts value is t.RGXTokenFromType<T>;
13
17
  export declare function isValidRegexString(value: string): value is t.ValidRegexString;
14
18
  export declare function assertValidRegexString(value: string): asserts value is t.ValidRegexString;
15
19
  export declare function isValidVanillaRegexFlags(value: string): value is t.ValidVanillaRegexFlags;
@@ -47,6 +47,10 @@ exports.assertRGXConvertibleToken = assertRGXConvertibleToken;
47
47
  exports.rgxTokenTypeFlat = rgxTokenTypeFlat;
48
48
  exports.rgxTokenType = rgxTokenType;
49
49
  exports.rgxTokenFromType = rgxTokenFromType;
50
+ exports.rgxTokenTypeToFlat = rgxTokenTypeToFlat;
51
+ exports.rgxTokenTypeGuardInputToFlat = rgxTokenTypeGuardInputToFlat;
52
+ exports.isRGXToken = isRGXToken;
53
+ exports.assertRGXToken = assertRGXToken;
50
54
  exports.isValidRegexString = isValidRegexString;
51
55
  exports.assertValidRegexString = assertValidRegexString;
52
56
  exports.isValidVanillaRegexFlags = isValidVanillaRegexFlags;
@@ -125,6 +129,47 @@ function rgxTokenFromType(type, value) {
125
129
  /* istanbul ignore next */
126
130
  return value;
127
131
  }
132
+ function rgxTokenTypeToFlat(type) {
133
+ return Array.isArray(type) ? 'array' : type;
134
+ }
135
+ function rgxTokenTypeGuardInputToFlat(type) {
136
+ if (type === null)
137
+ return null;
138
+ if (Array.isArray(type))
139
+ return 'array';
140
+ return type;
141
+ }
142
+ function isRGXToken(value, type = null, matchLength = true) {
143
+ function typeMatches(s) {
144
+ return type === null || type === s;
145
+ }
146
+ if (typeMatches('no-op') && isRGXNoOpToken(value))
147
+ return true;
148
+ if (typeMatches('literal') && isRGXLiteralToken(value))
149
+ return true;
150
+ if (typeMatches('native') && isRGXNativeToken(value))
151
+ return true;
152
+ if (typeMatches('convertible') && isRGXConvertibleToken(value))
153
+ return true;
154
+ if (typeMatches('array') && Array.isArray(value)) {
155
+ // @ts-ignore Excessively deep type is not a problem here.
156
+ return value.every(item => isRGXToken(item, null));
157
+ }
158
+ if (Array.isArray(type) && Array.isArray(value) && (!matchLength || type.length === value.length)) {
159
+ // This will always be false.
160
+ if (value.length < type.length)
161
+ return false;
162
+ // @ts-ignore Excessively deep type is not a problem here.
163
+ return value.every((item, i) => isRGXToken(item, type[i] ?? null));
164
+ }
165
+ return false;
166
+ }
167
+ function assertRGXToken(value, type = null, matchLength = true) {
168
+ if (!isRGXToken(value, type, matchLength)) {
169
+ const flatType = rgxTokenTypeGuardInputToFlat(type);
170
+ throw new e.RGXInvalidTokenError("Invalid RGX token", flatType === null ? null : { type: "tokenType", values: [flatType] }, value);
171
+ }
172
+ }
128
173
  function isValidRegexString(value) {
129
174
  try {
130
175
  new RegExp(value);
package/dist/types.d.ts CHANGED
@@ -10,8 +10,9 @@ export type RGXConvertibleToken = {
10
10
  export type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
11
11
  export type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | RGXTokenType[];
12
12
  export type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
13
- export type RGXTokenFromType<T extends RGXTokenType | RGXTokenTypeFlat> = T extends 'no-op' ? RGXNoOpToken : T extends 'literal' ? RGXLiteralToken : T extends 'native' ? RGXNativeToken : T extends 'convertible' ? RGXConvertibleToken : T extends 'array' ? RGXToken[] : T extends RGXTokenType[] ? {
14
- [K in keyof T]: T[K] extends RGXTokenType ? RGXTokenFromType<T[K]> : never;
13
+ export type RGXTokenTypeGuardInput = Exclude<RGXTokenType, RGXTokenType[]> | RGXTokenTypeFlat | null | 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 'array' ? RGXToken[] : T extends RGXTokenTypeGuardInput[] ? {
15
+ [K in keyof T]: T[K] extends RGXTokenTypeGuardInput ? RGXTokenFromType<T[K]> : never;
15
16
  } : never;
16
17
  export declare const validRegexSymbol: unique symbol;
17
18
  export type ValidRegexBrandSymbol = typeof validRegexSymbol;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "2.5.2",
3
+ "version": "2.6.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",