@ptolemy2002/rgx 1.1.0 → 1.2.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
@@ -4,11 +4,12 @@ A library for easy construction and validation of regular expressions in TypeScr
4
4
  ## Type Reference
5
5
  ```typescript
6
6
  import { Branded } from "@ptolemy2002/ts-brand-utils";
7
+ import { MaybeArray } from "@ptolemy2002/ts-utils";
7
8
 
8
9
  type RGXNoOpToken = null | undefined;
9
10
  type RGXLiteralToken = RegExp;
10
11
  type RGXNativeToken = string | number | boolean | RGXNoOpToken;
11
- type RGXConvertibleToken = { toRgx: () => RGXNativeToken | RGXNativeToken[] };
12
+ type RGXConvertibleToken = { toRgx: () => MaybeArray<RGXNativeToken | RGXLiteralToken> };
12
13
  type RGXToken = RGXNativeToken | RGXConvertibleToken | RGXToken[];
13
14
 
14
15
  const validRegexSymbol = Symbol('ValidRegex');
@@ -143,7 +144,7 @@ Escapes special regex characters in the given string and brands the result as a
143
144
 
144
145
  ### resolveRGXToken
145
146
  ```typescript
146
- function resolveRGXToken(token: RGXToken): string
147
+ function resolveRGXToken(token: RGXToken): ValidRegexString
147
148
  ```
148
149
 
149
150
  Resolves an RGX token to a string. No-op tokens resolve to an empty string, literal tokens are included as-is (wrapped in a non-capturing group), native tokens are converted to strings and escaped, convertible tokens are converted using their `toRgx` method and then resolved recursively, and arrays of tokens are resolved as unions of their resolved elements (placed in a non-capturing group).
@@ -152,11 +153,11 @@ Resolves an RGX token to a string. No-op tokens resolve to an empty string, lite
152
153
  - `token` (`RGXToken`): The RGX token to resolve.
153
154
 
154
155
  #### Returns
155
- - `string`: The resolved string representation of the RGX token.
156
+ - `ValidRegexString`: The resolved string representation of the RGX token. This is guaranteed to be a valid regex string, as convertible tokens are validated to only produce valid regex strings or arrays of valid regex strings.
156
157
 
157
158
  ### rgxConcat
158
159
  ```typescript
159
- function rgxConcat(tokens: RGXToken[]): string
160
+ function rgxConcat(tokens: RGXToken[]): ValidRegexString
160
161
  ```
161
162
 
162
163
  A helper function that resolves an array of RGX tokens and concatenates their resolved string representations together. This is useful for cases where you want to concatenate multiple tokens without creating a union between them.
@@ -165,7 +166,7 @@ A helper function that resolves an array of RGX tokens and concatenates their re
165
166
  - `tokens` (`RGXToken[]`): The array of RGX tokens to resolve and concatenate.
166
167
 
167
168
  #### Returns
168
- - `string`: The concatenated string representation of the resolved RGX tokens.
169
+ - `ValidRegexString`: The concatenated string representation of the resolved RGX tokens. This is guaranteed to be a valid regex string, as it is composed of the resolved forms of RGX tokens, which are all valid regex strings.
169
170
 
170
171
  ### rgx
171
172
  ```typescript
package/dist/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { Branded } from "@ptolemy2002/ts-brand-utils";
2
+ import { MaybeArray } from "@ptolemy2002/ts-utils";
2
3
  export type RGXNoOpToken = null | undefined;
3
4
  export type RGXLiteralToken = RegExp;
4
5
  export type RGXNativeToken = string | number | boolean | RGXNoOpToken;
5
6
  export type RGXConvertibleToken = {
6
- toRgx: () => RGXNativeToken | RGXNativeToken[];
7
+ toRgx: () => MaybeArray<RGXNativeToken | RGXLiteralToken>;
7
8
  };
8
9
  export type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
9
10
  export type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | RGXTokenType[];
@@ -21,6 +22,6 @@ export declare function rgxTokenType(value: RGXToken): RGXTokenType;
21
22
  export declare function rgxTokenFromType<T extends RGXTokenType>(type: T, value: RGXToken): RGXTokenFromType<T>;
22
23
  export declare function isValidRegex(value: string): value is ValidRegexString;
23
24
  export declare function escapeRegex(value: string): ValidRegexString;
24
- export declare function resolveRGXToken(token: RGXToken): string;
25
- export declare function rgxConcat(tokens: RGXToken[]): string;
25
+ export declare function resolveRGXToken(token: RGXToken): ValidRegexString;
26
+ export declare function rgxConcat(tokens: RGXToken[]): ValidRegexString;
26
27
  export default function rgx(strings: TemplateStringsArray, ...tokens: RGXToken[]): RegExp;
package/dist/index.js CHANGED
@@ -31,9 +31,9 @@ function isRGXConvertibleToken(value) {
31
31
  if ((0, is_callable_1.default)(value.toRgx)) {
32
32
  const returnValue = value.toRgx();
33
33
  if (Array.isArray(returnValue)) {
34
- return returnValue.every(isRGXNativeToken);
34
+ return returnValue.every(value => isRGXNativeToken(value) || isRGXLiteralToken(value));
35
35
  }
36
- return isRGXNativeToken(returnValue);
36
+ return isRGXNativeToken(returnValue) || isRGXLiteralToken(returnValue);
37
37
  }
38
38
  return false;
39
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",