@ptolemy2002/rgx 3.1.1 → 4.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 +103 -10
- package/dist/class/base.d.ts +5 -2
- package/dist/class/base.js +9 -5
- package/dist/class/group.d.ts +22 -0
- package/dist/class/group.js +59 -0
- package/dist/class/index.d.ts +1 -0
- package/dist/class/index.js +1 -0
- package/dist/class/init.js +4 -0
- package/dist/class/union.d.ts +2 -2
- package/dist/class/union.js +3 -3
- package/dist/collection.d.ts +4 -1
- package/dist/collection.js +5 -0
- package/dist/errors/base.d.ts +1 -1
- package/dist/errors/index.d.ts +1 -0
- package/dist/errors/index.js +1 -0
- package/dist/errors/invalidIdentifier.d.ts +6 -0
- package/dist/errors/invalidIdentifier.js +15 -0
- package/dist/errors/invalidToken.js +1 -1
- package/dist/resolve.d.ts +1 -1
- package/dist/resolve.js +6 -4
- package/dist/typeGuards.d.ts +2 -0
- package/dist/typeGuards.js +17 -3
- package/dist/types.d.ts +4 -0
- package/dist/types.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ import { Branded } from "@ptolemy2002/ts-brand-utils";
|
|
|
8
8
|
type RGXNoOpToken = null | undefined;
|
|
9
9
|
type RGXLiteralToken = RegExp;
|
|
10
10
|
type RGXNativeToken = string | number | boolean | RGXNoOpToken;
|
|
11
|
-
type RGXConvertibleToken = { toRgx: () => RGXToken };
|
|
11
|
+
type RGXConvertibleToken = { toRgx: () => RGXToken, readonly rgxGroupWrap?: boolean };
|
|
12
12
|
type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
|
|
13
13
|
type RGXClassTokenConstructor = new (...args: unknown[]) => RGXClassToken;
|
|
14
14
|
|
|
@@ -20,6 +20,10 @@ const validVanillaRegexFlagsSymbol = Symbol('rgx.ValidVanillaRegexFlags');
|
|
|
20
20
|
type ValidVanillaRegexFlagsBrandSymbol = typeof validVanillaRegexFlagsSymbol;
|
|
21
21
|
type ValidVanillaRegexFlags = Branded<string, [ValidVanillaRegexFlagsBrandSymbol]>;
|
|
22
22
|
|
|
23
|
+
const validIdentifierSymbol = Symbol('rgx.ValidIdentifier');
|
|
24
|
+
type ValidIdentifierBrandSymbol = typeof validIdentifierSymbol;
|
|
25
|
+
type ValidIdentifier = Branded<string, [ValidIdentifierBrandSymbol]>;
|
|
26
|
+
|
|
23
27
|
type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | 'class' | RGXTokenType[];
|
|
24
28
|
type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
|
|
25
29
|
type RGXTokenTypeGuardInput = RGXTokenTypeFlat | null | RGXClassTokenConstructor | RGXTokenTypeGuardInput[];
|
|
@@ -30,7 +34,7 @@ type RGXTokenFromType<T extends RGXTokenTypeGuardInput> =
|
|
|
30
34
|
// ... see source for full definition
|
|
31
35
|
;
|
|
32
36
|
|
|
33
|
-
type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_VANILLA_REGEX_FLAGS' | 'NOT_IMPLEMENTED';
|
|
37
|
+
type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_VANILLA_REGEX_FLAGS' | 'NOT_IMPLEMENTED' | 'INVALID_IDENTIFIER';
|
|
34
38
|
type ExpectedTokenType = {
|
|
35
39
|
type: "tokenType";
|
|
36
40
|
values: RGXTokenTypeFlat[];
|
|
@@ -42,6 +46,11 @@ type RGXTokenCollectionMode = 'union' | 'concat';
|
|
|
42
46
|
type RGXTokenCollectionInput = RGXToken | RGXTokenCollection;
|
|
43
47
|
|
|
44
48
|
type RGXUnionInsertionPosition = 'prefix' | 'suffix';
|
|
49
|
+
|
|
50
|
+
type RGXGroupTokenArgs = {
|
|
51
|
+
name?: string | null;
|
|
52
|
+
capturing?: boolean;
|
|
53
|
+
};
|
|
45
54
|
```
|
|
46
55
|
|
|
47
56
|
## Classes
|
|
@@ -117,6 +126,22 @@ constructor(functionality: string, message?: string | null)
|
|
|
117
126
|
#### Methods
|
|
118
127
|
- `toString() => string`: Returns a formatted string indicating the unimplemented functionality and any additional message.
|
|
119
128
|
|
|
129
|
+
### RGXInvalidIdentifierError extends RGXError
|
|
130
|
+
A specific error class for invalid identifiers. This error is thrown when a string fails validation as a valid identifier. The error code is set to `INVALID_IDENTIFIER` on instantiation.
|
|
131
|
+
|
|
132
|
+
#### Constructor
|
|
133
|
+
```typescript
|
|
134
|
+
constructor(message: string, got: string)
|
|
135
|
+
```
|
|
136
|
+
- `message` (`string`): The error message.
|
|
137
|
+
- `got` (`string`): The actual string that was received, which failed validation.
|
|
138
|
+
|
|
139
|
+
#### Properties
|
|
140
|
+
- `got` (`string`): The actual string that was received, which failed validation.
|
|
141
|
+
|
|
142
|
+
#### Methods
|
|
143
|
+
- `toString() => string`: Returns a formatted string indicating the invalid identifier and the reason for failure.
|
|
144
|
+
|
|
120
145
|
### RGXTokenCollection
|
|
121
146
|
A class representing a collection of RGX tokens. This class manages collections of RGX tokens like an array, but with additional metadata about the collection mode (union or concat). Since `toRgx()` returns a `RegExp`, instances of this class satisfy the `RGXConvertibleToken` interface and can be used directly as tokens in `rgx`, `rgxa`, and other token-accepting functions.
|
|
122
147
|
|
|
@@ -132,6 +157,7 @@ constructor(tokens: RGXTokenCollectionInput = [], mode: RGXTokenCollectionMode =
|
|
|
132
157
|
#### Properties
|
|
133
158
|
- `tokens` (`RGXToken[]`): The array of RGX tokens managed by the collection. In almost all cases, use `getTokens()` instead of accessing this property directly, as it will be copied to prevent external mutation.
|
|
134
159
|
- `mode` (`RGXTokenCollectionMode`): The mode of the collection, either 'union' or 'concat'. This determines how the tokens in the collection will be resolved when `toRgx()` is called.
|
|
160
|
+
- `resolve() => ValidRegexString`: A convenience method that resolves this collection by calling `resolveRGXToken(this)`, returning the resolved regex string representation.
|
|
135
161
|
- `toRgx() => RegExp`: A method that resolves the collection to a `RegExp` object based on the collection mode. In 'union' mode, the tokens are resolved as alternatives (using the `|` operator), while in 'concat' mode, the tokens are resolved as concatenated together. No flags are applied to the resulting `RegExp`. Since this method returns a `RegExp` (which is `RGXLiteralToken`), `RGXTokenCollection` instances satisfy the `RGXConvertibleToken` interface and can be used directly as tokens in `rgx`, `rgxa`, and other token-accepting functions.
|
|
136
162
|
- `getTokens() => RGXToken[]`: A method that returns a copy of the array of RGX tokens managed by the collection. This is used to prevent external mutation of the internal `tokens` array.
|
|
137
163
|
- `toArray() => RGXToken[]`: An alias for `getTokens()`, provided for convenience.
|
|
@@ -141,25 +167,37 @@ constructor(tokens: RGXTokenCollectionInput = [], mode: RGXTokenCollectionMode =
|
|
|
141
167
|
|
|
142
168
|
Standard array properties and methods like `length`, `push`, `pop`, etc. are implemented to work with the internal `tokens` array, but providing collection instances instead of raw arrays when relevant (e.g., `map` has the third parameter typed as `RGXTokenCollection` instead of `RGXToken[]`).
|
|
143
169
|
|
|
170
|
+
#### Static Properties
|
|
171
|
+
- `check(value: unknown): value is RGXTokenCollection`: A type guard that checks if the given value is an instance of `RGXTokenCollection`.
|
|
172
|
+
- `assert(value: unknown): asserts value is RGXTokenCollection`: An assertion that checks if the given value is an instance of `RGXTokenCollection`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
173
|
+
|
|
144
174
|
### RGXClassToken (abstract)
|
|
145
175
|
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).
|
|
146
176
|
|
|
147
|
-
|
|
177
|
+
#### Static Properties
|
|
178
|
+
- `check(value: unknown): value is RGXClassToken`: A type guard that checks if the given value is an instance of `RGXClassToken`.
|
|
179
|
+
- `assert(value: unknown): asserts value is RGXClassToken`: An assertion that checks if the given value is an instance of `RGXClassToken`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
148
180
|
|
|
149
181
|
#### Abstract Methods
|
|
150
182
|
- `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).
|
|
151
183
|
|
|
152
184
|
#### Properties
|
|
153
185
|
- `isGroup` (`boolean`): Returns `false` by default. Subclasses can override this to indicate whether the token represents a group.
|
|
186
|
+
- `rgxGroupWrap` (`boolean`): Returns `true` by default. Controls whether the resolver wraps this token's resolved output in a non-capturing group. Subclasses can override this to prevent double-wrapping (e.g., when the token already wraps itself in a group).
|
|
154
187
|
|
|
155
188
|
#### Methods
|
|
156
189
|
- `or(...others: RGXTokenCollectionInput[]) => RGXClassUnionToken`: Creates an `RGXClassUnionToken` that represents a union (alternation) of this token with the provided others. If any of the `others` are `RGXClassUnionToken` instances, their tokens are flattened into the union rather than nested. If `this` is already an `RGXClassUnionToken`, its existing tokens are preserved and the others are appended.
|
|
157
|
-
- `
|
|
190
|
+
- `group(args?: RGXGroupTokenArgs) => RGXGroupToken`: Wraps this token in an `RGXGroupToken` with the provided arguments. The `args` parameter defaults to `{}`, which creates a capturing group with no name. This is a convenience method that creates a new `RGXGroupToken` with `this` as the sole token.
|
|
191
|
+
- `resolve() => ValidRegexString`: A convenience method that resolves this token by calling `resolveRGXToken(this)`, returning the resolved regex string representation. Since this method is defined on `RGXClassToken`, it is available on all subclasses including `RGXClassUnionToken` and `RGXGroupToken`.
|
|
158
192
|
|
|
159
193
|
### RGXClassUnionToken extends RGXClassToken
|
|
160
194
|
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.
|
|
161
195
|
|
|
162
|
-
A function `rgxClassUnion` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
|
|
196
|
+
A function `rgxClassUnion` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
|
|
197
|
+
|
|
198
|
+
#### Static Properties
|
|
199
|
+
- `check(value: unknown): value is RGXClassUnionToken`: A type guard that checks if the given value is an instance of `RGXClassUnionToken`.
|
|
200
|
+
- `assert(value: unknown): asserts value is RGXClassUnionToken`: An assertion that checks if the given value is an instance of `RGXClassUnionToken`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
163
201
|
|
|
164
202
|
#### Constructor
|
|
165
203
|
```typescript
|
|
@@ -177,6 +215,34 @@ constructor(tokens: RGXTokenCollectionInput = [])
|
|
|
177
215
|
- `cleanTokens() => this`: Expands any nested union tokens and removes duplicates from the internal token collection. Returns `this` for chaining. Called automatically during construction and after `add` or `concat`.
|
|
178
216
|
- `toRgx() => RegExp`: Resolves the union by calling `toRgx()` on the internal `RGXTokenCollection`, returning a `RegExp`.
|
|
179
217
|
|
|
218
|
+
### RGXGroupToken extends RGXClassToken
|
|
219
|
+
A class representing a group (capturing, non-capturing, or named) wrapping one or more RGX tokens. This is typically created via the `group()` method on `RGXClassToken`, but can also be instantiated directly.
|
|
220
|
+
|
|
221
|
+
A function `rgxGroup` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
|
|
222
|
+
|
|
223
|
+
#### Static Properties
|
|
224
|
+
- `check(value: unknown): value is RGXGroupToken`: A type guard that checks if the given value is an instance of `RGXGroupToken`.
|
|
225
|
+
- `assert(value: unknown): asserts value is RGXGroupToken`: An assertion that checks if the given value is an instance of `RGXGroupToken`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
226
|
+
|
|
227
|
+
#### Constructor
|
|
228
|
+
```typescript
|
|
229
|
+
constructor(args?: RGXGroupTokenArgs, tokens?: RGXTokenCollectionInput)
|
|
230
|
+
```
|
|
231
|
+
- `args` (`RGXGroupTokenArgs`, optional): An object specifying the group configuration. Defaults to `{}`.
|
|
232
|
+
- `name` (`string | null`, optional): The name of the group for named capture groups. Must be a valid identifier (validated via `assertValidIdentifier`). Defaults to `null`.
|
|
233
|
+
- `capturing` (`boolean`, optional): Whether the group is capturing. Defaults to `true`. Setting this to `false` also clears any `name`.
|
|
234
|
+
- `tokens` (`RGXTokenCollectionInput`, optional): The tokens to be wrapped by the group. Internally stored as an `RGXTokenCollection` in 'concat' mode. Defaults to an empty array.
|
|
235
|
+
|
|
236
|
+
#### Properties
|
|
237
|
+
- `tokens` (`RGXTokenCollection`): The internal collection of tokens managed in 'concat' mode.
|
|
238
|
+
- `name` (`string | null`): The name of the group. Setting this to a non-null value validates it as a valid identifier via `assertValidIdentifier`.
|
|
239
|
+
- `capturing` (`boolean`): Whether the group is capturing. Any named group is automatically capturing (returns `true` when `name` is not `null`). Setting this to `false` also clears `name` to `null`.
|
|
240
|
+
- `isGroup` (`boolean`): Returns `true`, indicating this token represents a group.
|
|
241
|
+
- `rgxGroupWrap` (`boolean`): Returns `false`, since the group already wraps itself, preventing the resolver from double-wrapping.
|
|
242
|
+
|
|
243
|
+
#### Methods
|
|
244
|
+
- `toRgx() => RegExp`: Resolves the group by concatenating the internal tokens and wrapping the result in the appropriate group syntax: `(?<name>...)` for named groups, `(?:...)` for non-capturing groups, or `(...)` for capturing groups.
|
|
245
|
+
|
|
180
246
|
## Functions
|
|
181
247
|
The following functions are exported by the library:
|
|
182
248
|
|
|
@@ -263,7 +329,7 @@ Asserts that the given value is a native token (string, number, boolean, or no-o
|
|
|
263
329
|
function isRGXConvertibleToken(value: unknown, returnCheck?: boolean): value is RGXConvertibleToken
|
|
264
330
|
```
|
|
265
331
|
|
|
266
|
-
Checks if the given value is a convertible token (an object with a `toRgx` method). When `returnCheck` is `true` (the default), also validates that `toRgx` is callable and returns a valid `RGXToken` (which can be any RGX token type, including other convertible tokens, allowing for recursive structures).
|
|
332
|
+
Checks if the given value is a convertible token (an object with a `toRgx` method). If the `rgxGroupWrap` property is present, it must be a boolean; otherwise, the check fails. When `returnCheck` is `true` (the default), also validates that `toRgx` is callable and returns a valid `RGXToken` (which can be any RGX token type, including other convertible tokens, allowing for recursive structures).
|
|
267
333
|
|
|
268
334
|
#### Parameters
|
|
269
335
|
- `value` (`unknown`): The value to check.
|
|
@@ -276,7 +342,7 @@ Checks if the given value is a convertible token (an object with a `toRgx` metho
|
|
|
276
342
|
```typescript
|
|
277
343
|
function assertRGXConvertibleToken(value: unknown, returnCheck?: boolean): asserts value is RGXConvertibleToken
|
|
278
344
|
```
|
|
279
|
-
Asserts that the given value is a convertible token (an object with a `toRgx` method). When `returnCheck` is `true` (the default), also validates that `toRgx` is callable and returns a valid `RGXToken` (which can be any RGX token type, including other convertible tokens, allowing for recursive structures). If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
345
|
+
Asserts that the given value is a convertible token (an object with a `toRgx` method). If the `rgxGroupWrap` property is present, it must be a boolean; otherwise, the assertion fails. When `returnCheck` is `true` (the default), also validates that `toRgx` is callable and returns a valid `RGXToken` (which can be any RGX token type, including other convertible tokens, allowing for recursive structures). If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
280
346
|
|
|
281
347
|
#### Parameters
|
|
282
348
|
- `value` (`unknown`): The value to assert.
|
|
@@ -489,6 +555,30 @@ Asserts that the given string is a valid combination of vanilla regex flags (g,
|
|
|
489
555
|
#### Returns
|
|
490
556
|
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
491
557
|
|
|
558
|
+
### isValidIdentifier
|
|
559
|
+
```typescript
|
|
560
|
+
function isValidIdentifier(value: string): value is ValidIdentifier
|
|
561
|
+
```
|
|
562
|
+
Checks if the given string is a valid identifier, used for group names and backreferences. Valid identifiers contain only letters, digits, dollar signs, and underscores, and cannot start with a digit.
|
|
563
|
+
|
|
564
|
+
#### Parameters
|
|
565
|
+
- `value` (`string`): The string to check.
|
|
566
|
+
|
|
567
|
+
#### Returns
|
|
568
|
+
- `boolean`: `true` if the string is a valid identifier, otherwise `false`.
|
|
569
|
+
|
|
570
|
+
### assertValidIdentifier
|
|
571
|
+
```typescript
|
|
572
|
+
function assertValidIdentifier(value: string): asserts value is ValidIdentifier
|
|
573
|
+
```
|
|
574
|
+
Asserts that the given string is a valid identifier, used for group names and backreferences. Valid identifiers contain only letters, digits, dollar signs, and underscores, and cannot start with a digit. If the assertion fails, an `RGXInvalidIdentifierError` will be thrown.
|
|
575
|
+
|
|
576
|
+
#### Parameters
|
|
577
|
+
- `value` (`string`): The string to assert.
|
|
578
|
+
|
|
579
|
+
#### Returns
|
|
580
|
+
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
581
|
+
|
|
492
582
|
### escapeRegex
|
|
493
583
|
```typescript
|
|
494
584
|
function escapeRegex(value: string): ValidRegexString
|
|
@@ -504,14 +594,17 @@ Escapes special regex characters in the given string and brands the result as a
|
|
|
504
594
|
|
|
505
595
|
### resolveRGXToken
|
|
506
596
|
```typescript
|
|
507
|
-
function resolveRGXToken(token: RGXToken, groupWrap?: boolean): ValidRegexString
|
|
597
|
+
function resolveRGXToken(token: RGXToken, groupWrap?: boolean, topLevel?: boolean): ValidRegexString
|
|
508
598
|
```
|
|
509
599
|
|
|
510
600
|
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 when `groupWrap` is `true`), 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 (repeats removed, placed in a non-capturing group when `groupWrap` is `true`).
|
|
511
601
|
|
|
602
|
+
For convertible tokens, if the token has an `rgxGroupWrap` property, that value always takes precedence. If `rgxGroupWrap` is not present, the behavior depends on whether the call is top-level: at the top level, the `groupWrap` parameter is passed through; in recursive calls, it falls back to `true` regardless of the `groupWrap` parameter. This ensures that the caller's `groupWrap` preference only affects the outermost convertible token and does not leak into deeply nested resolution.
|
|
603
|
+
|
|
512
604
|
#### Parameters
|
|
513
605
|
- `token` (`RGXToken`): The RGX token to resolve.
|
|
514
|
-
- `groupWrap` (`boolean`, optional): Whether to wrap literal tokens and array unions in non-capturing groups (`(?:...)`). Defaults to `true`. When `false`, literals use their raw source and array unions omit the wrapping group.
|
|
606
|
+
- `groupWrap` (`boolean`, optional): Whether to wrap literal tokens and array unions in non-capturing groups (`(?:...)`). Defaults to `true`. When `false`, literals use their raw source and array unions omit the wrapping group. For convertible tokens, the token's `rgxGroupWrap` property always takes precedence; otherwise, this value is only passed through at the top level (in recursive calls it falls back to `true`). Array union elements always use `groupWrap=true` internally.
|
|
607
|
+
- `topLevel` (`boolean`, optional): Tracks whether the current call is the initial (top-level) invocation. Defaults to `true`. **Warning**: This parameter is intended for internal use by the resolver's own recursion. External callers should not set this parameter, as doing so may produce unexpected wrapping behavior.
|
|
515
608
|
|
|
516
609
|
#### Returns
|
|
517
610
|
- `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.
|
|
@@ -605,7 +698,7 @@ Removes duplicate tokens from the provided list using `Set` equality and returns
|
|
|
605
698
|
function rgxClassInit(): void
|
|
606
699
|
```
|
|
607
700
|
|
|
608
|
-
Initializes internal method patches required for `RGXClassToken` subclass methods (such as `or`) to work correctly. This function is called automatically when importing from the main module entry point, so you typically do not need to call it yourself. It only needs to be called manually if you import directly from sub-modules.
|
|
701
|
+
Initializes internal method patches required for `RGXClassToken` subclass methods (such as `or` and `group`) to work correctly. This function is called automatically when importing from the main module entry point, so you typically do not need to call it yourself. It only needs to be called manually if you import directly from sub-modules.
|
|
609
702
|
|
|
610
703
|
## Peer Dependencies
|
|
611
704
|
- `@ptolemy2002/immutability-utils` ^2.0.0
|
package/dist/class/base.d.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { RGXToken, ValidRegexString } from "../types";
|
|
2
2
|
import { RGXTokenCollectionInput } from "../collection";
|
|
3
3
|
import type { RGXClassUnionToken } from "./union";
|
|
4
|
+
import type { RGXGroupToken, RGXGroupTokenArgs } from "./group";
|
|
4
5
|
export declare abstract class RGXClassToken {
|
|
5
6
|
abstract toRgx(): RGXToken;
|
|
7
|
+
static check: (value: unknown) => value is RGXClassToken;
|
|
8
|
+
static assert: (value: unknown) => asserts value is RGXClassToken;
|
|
6
9
|
get isGroup(): boolean;
|
|
10
|
+
get rgxGroupWrap(): boolean;
|
|
7
11
|
or(...others: RGXTokenCollectionInput[]): RGXClassUnionToken;
|
|
12
|
+
group(args?: RGXGroupTokenArgs): RGXGroupToken;
|
|
8
13
|
resolve(): ValidRegexString;
|
|
9
14
|
}
|
|
10
|
-
export declare const isRgxClassToken: (value: unknown) => value is RGXClassToken;
|
|
11
|
-
export declare const assertRgxClassToken: (value: unknown) => asserts value is RGXClassToken;
|
package/dist/class/base.js
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.RGXClassToken = void 0;
|
|
4
4
|
const errors_1 = require("../errors");
|
|
5
5
|
const resolve_1 = require("../resolve");
|
|
6
6
|
class RGXClassToken {
|
|
7
7
|
get isGroup() {
|
|
8
8
|
return false;
|
|
9
9
|
}
|
|
10
|
+
get rgxGroupWrap() {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
10
13
|
or(...others) {
|
|
11
14
|
throw new errors_1.RGXNotImplementedError('RGXClassToken.or(...others)', 'call rgxClassInit() first.');
|
|
12
15
|
}
|
|
16
|
+
group(args = {}) {
|
|
17
|
+
throw new errors_1.RGXNotImplementedError('RGXClassToken.group(args)', 'call rgxClassInit() first.');
|
|
18
|
+
}
|
|
13
19
|
resolve() {
|
|
14
20
|
return (0, resolve_1.resolveRGXToken)(this);
|
|
15
21
|
}
|
|
@@ -17,11 +23,9 @@ class RGXClassToken {
|
|
|
17
23
|
exports.RGXClassToken = RGXClassToken;
|
|
18
24
|
// The createClassGuard function only accepts non-abstract classes, so we
|
|
19
25
|
// manually define the guard and assertion functions for RGXClassToken here.
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const assertRgxClassToken = (value) => {
|
|
26
|
+
RGXClassToken.check = (value) => value instanceof RGXClassToken;
|
|
27
|
+
RGXClassToken.assert = (value) => {
|
|
23
28
|
if (!(value instanceof RGXClassToken)) {
|
|
24
29
|
throw new errors_1.RGXInvalidTokenError("Invalid token type", { type: "custom", values: ["instance of RGXClassToken"] }, value);
|
|
25
30
|
}
|
|
26
31
|
};
|
|
27
|
-
exports.assertRgxClassToken = assertRgxClassToken;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { RGXTokenCollection, RGXTokenCollectionInput } from "../collection";
|
|
2
|
+
import { RGXClassToken } from "./base";
|
|
3
|
+
export type RGXGroupTokenArgs = {
|
|
4
|
+
name?: string | null;
|
|
5
|
+
capturing?: boolean;
|
|
6
|
+
};
|
|
7
|
+
export declare class RGXGroupToken extends RGXClassToken {
|
|
8
|
+
tokens: RGXTokenCollection;
|
|
9
|
+
_name: string | null;
|
|
10
|
+
_capturing: boolean;
|
|
11
|
+
static check: (value: unknown) => value is RGXGroupToken;
|
|
12
|
+
static assert: (value: unknown) => asserts value is RGXGroupToken;
|
|
13
|
+
get name(): string | null;
|
|
14
|
+
set name(value: string | null);
|
|
15
|
+
get capturing(): boolean;
|
|
16
|
+
set capturing(value: boolean);
|
|
17
|
+
get isGroup(): boolean;
|
|
18
|
+
get rgxGroupWrap(): boolean;
|
|
19
|
+
constructor({ name, capturing }?: RGXGroupTokenArgs, tokens?: RGXTokenCollectionInput);
|
|
20
|
+
toRgx(): RegExp;
|
|
21
|
+
}
|
|
22
|
+
export declare const rgxGroup: (args_0?: RGXGroupTokenArgs | undefined, tokens?: RGXTokenCollectionInput) => RGXGroupToken;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.rgxGroup = exports.RGXGroupToken = void 0;
|
|
4
|
+
const collection_1 = require("../collection");
|
|
5
|
+
const base_1 = require("./base");
|
|
6
|
+
const internal_1 = require("../internal");
|
|
7
|
+
const typeGuards_1 = require("../typeGuards");
|
|
8
|
+
class RGXGroupToken extends base_1.RGXClassToken {
|
|
9
|
+
get name() {
|
|
10
|
+
return this._name;
|
|
11
|
+
}
|
|
12
|
+
set name(value) {
|
|
13
|
+
if (value !== null)
|
|
14
|
+
(0, typeGuards_1.assertValidIdentifier)(value);
|
|
15
|
+
this._name = value;
|
|
16
|
+
}
|
|
17
|
+
get capturing() {
|
|
18
|
+
// Any named group is automatically capturing
|
|
19
|
+
return this.name !== null || this._capturing;
|
|
20
|
+
}
|
|
21
|
+
set capturing(value) {
|
|
22
|
+
if (!value)
|
|
23
|
+
this.name = null; // Non-capturing groups cannot have names
|
|
24
|
+
this._capturing = value;
|
|
25
|
+
}
|
|
26
|
+
get isGroup() {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
get rgxGroupWrap() {
|
|
30
|
+
// When this token is resolved, it will wrap itself in a group, so we don't want the resolver to group wrap it again.
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
constructor({ name = null, capturing = true } = {}, tokens = []) {
|
|
34
|
+
super();
|
|
35
|
+
this._name = null;
|
|
36
|
+
this._capturing = true;
|
|
37
|
+
this.name = name;
|
|
38
|
+
this.capturing = capturing;
|
|
39
|
+
if (tokens instanceof collection_1.RGXTokenCollection && tokens.mode === 'union')
|
|
40
|
+
this.tokens = new collection_1.RGXTokenCollection(tokens, 'concat');
|
|
41
|
+
else
|
|
42
|
+
this.tokens = new collection_1.RGXTokenCollection(tokens, 'concat');
|
|
43
|
+
}
|
|
44
|
+
toRgx() {
|
|
45
|
+
// The collection token doesn't group itself, so this is safe.
|
|
46
|
+
let result = this.tokens.toRgx().source;
|
|
47
|
+
if (this.name !== null)
|
|
48
|
+
result = `(?<${this.name}>${result})`;
|
|
49
|
+
else if (!this.capturing)
|
|
50
|
+
result = `(?:${result})`;
|
|
51
|
+
else
|
|
52
|
+
result = `(${result})`;
|
|
53
|
+
return new RegExp(result);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.RGXGroupToken = RGXGroupToken;
|
|
57
|
+
RGXGroupToken.check = (0, internal_1.createClassGuardFunction)(RGXGroupToken);
|
|
58
|
+
RGXGroupToken.assert = (0, internal_1.createAssertClassGuardFunction)(RGXGroupToken);
|
|
59
|
+
exports.rgxGroup = (0, internal_1.createConstructFunction)(RGXGroupToken);
|
package/dist/class/index.d.ts
CHANGED
package/dist/class/index.js
CHANGED
package/dist/class/init.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.rgxClassInit = rgxClassInit;
|
|
4
4
|
const base_1 = require("./base");
|
|
5
5
|
const union_1 = require("./union");
|
|
6
|
+
const group_1 = require("./group");
|
|
6
7
|
function rgxClassInit() {
|
|
7
8
|
// Patch RGXClassToken here, Since classes like RGXClassUnionToken are instances of RGXClassToken
|
|
8
9
|
// themselves. If we tried to import RGXClassUnionToken in base.ts, it would cause a circular dependency.
|
|
@@ -19,4 +20,7 @@ function rgxClassInit() {
|
|
|
19
20
|
return new union_1.RGXClassUnionToken([this, ...filteredOthers]);
|
|
20
21
|
}
|
|
21
22
|
};
|
|
23
|
+
base_1.RGXClassToken.prototype.group = function (args = {}) {
|
|
24
|
+
return new group_1.RGXGroupToken(args, [this]);
|
|
25
|
+
};
|
|
22
26
|
}
|
package/dist/class/union.d.ts
CHANGED
|
@@ -4,6 +4,8 @@ import { RGXClassToken } from "./base";
|
|
|
4
4
|
export type RGXUnionInsertionPosition = 'prefix' | 'suffix';
|
|
5
5
|
export declare class RGXClassUnionToken extends RGXClassToken {
|
|
6
6
|
tokens: RGXTokenCollection;
|
|
7
|
+
static check: (value: unknown) => value is RGXClassUnionToken;
|
|
8
|
+
static assert: (value: unknown) => asserts value is RGXClassUnionToken;
|
|
7
9
|
get isGroup(): boolean;
|
|
8
10
|
constructor(tokens?: RGXTokenCollectionInput);
|
|
9
11
|
cleanTokens(): this;
|
|
@@ -14,5 +16,3 @@ export declare class RGXClassUnionToken extends RGXClassToken {
|
|
|
14
16
|
export declare function expandRgxUnionTokens(...tokens: RGXTokenCollectionInput[]): RGXTokenCollection;
|
|
15
17
|
export declare function removeRgxUnionDuplicates(...tokens: RGXTokenCollectionInput[]): RGXTokenCollection;
|
|
16
18
|
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;
|
package/dist/class/union.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.rgxClassUnion = exports.RGXClassUnionToken = void 0;
|
|
4
4
|
exports.expandRgxUnionTokens = expandRgxUnionTokens;
|
|
5
5
|
exports.removeRgxUnionDuplicates = removeRgxUnionDuplicates;
|
|
6
6
|
const internal_1 = require("../internal");
|
|
@@ -49,6 +49,8 @@ class RGXClassUnionToken extends base_1.RGXClassToken {
|
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
exports.RGXClassUnionToken = RGXClassUnionToken;
|
|
52
|
+
RGXClassUnionToken.check = (0, internal_1.createClassGuardFunction)(RGXClassUnionToken);
|
|
53
|
+
RGXClassUnionToken.assert = (0, internal_1.createAssertClassGuardFunction)(RGXClassUnionToken);
|
|
52
54
|
function expandRgxUnionTokens(...tokens) {
|
|
53
55
|
const result = new collection_1.RGXTokenCollection();
|
|
54
56
|
for (const token of tokens) {
|
|
@@ -87,5 +89,3 @@ function removeRgxUnionDuplicates(...tokens) {
|
|
|
87
89
|
return new collection_1.RGXTokenCollection(uniqueTokens, 'union');
|
|
88
90
|
}
|
|
89
91
|
exports.rgxClassUnion = (0, internal_1.createConstructFunction)(RGXClassUnionToken);
|
|
90
|
-
exports.isRgxClassUnionToken = (0, internal_1.createClassGuardFunction)(RGXClassUnionToken);
|
|
91
|
-
exports.assertRgxClassUnionToken = (0, internal_1.createAssertClassGuardFunction)(RGXClassUnionToken);
|
package/dist/collection.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RGXToken } from "./types";
|
|
1
|
+
import { RGXToken, ValidRegexString } from "./types";
|
|
2
2
|
import { CloneDepth } from "@ptolemy2002/immutability-utils";
|
|
3
3
|
import { Collection } from "@ptolemy2002/ts-utils";
|
|
4
4
|
export type RGXTokenCollectionMode = 'union' | 'concat';
|
|
@@ -6,7 +6,10 @@ export type RGXTokenCollectionInput = RGXToken | RGXTokenCollection;
|
|
|
6
6
|
export declare class RGXTokenCollection implements Collection<RGXToken> {
|
|
7
7
|
mode: RGXTokenCollectionMode;
|
|
8
8
|
tokens: RGXToken[];
|
|
9
|
+
static check: (value: unknown) => value is RGXTokenCollection;
|
|
10
|
+
static assert: (value: unknown) => asserts value is RGXTokenCollection;
|
|
9
11
|
constructor(tokens?: RGXTokenCollectionInput, mode?: RGXTokenCollectionMode);
|
|
12
|
+
resolve(): ValidRegexString;
|
|
10
13
|
toRgx(): RegExp;
|
|
11
14
|
getTokens(): RGXToken[];
|
|
12
15
|
clone(depth?: CloneDepth): RGXTokenCollection;
|
package/dist/collection.js
CHANGED
|
@@ -19,6 +19,9 @@ class RGXTokenCollection {
|
|
|
19
19
|
}
|
|
20
20
|
this.mode = mode;
|
|
21
21
|
}
|
|
22
|
+
resolve() {
|
|
23
|
+
return (0, resolve_1.resolveRGXToken)(this);
|
|
24
|
+
}
|
|
22
25
|
toRgx() {
|
|
23
26
|
let pattern;
|
|
24
27
|
if (this.mode === 'union') {
|
|
@@ -164,4 +167,6 @@ class RGXTokenCollection {
|
|
|
164
167
|
}
|
|
165
168
|
}
|
|
166
169
|
exports.RGXTokenCollection = RGXTokenCollection;
|
|
170
|
+
RGXTokenCollection.check = (0, internal_1.createClassGuardFunction)(RGXTokenCollection);
|
|
171
|
+
RGXTokenCollection.assert = (0, internal_1.createAssertClassGuardFunction)(RGXTokenCollection);
|
|
167
172
|
exports.rgxTokenCollection = (0, internal_1.createConstructFunction)(RGXTokenCollection);
|
package/dist/errors/base.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_VANILLA_REGEX_FLAGS' | 'NOT_IMPLEMENTED';
|
|
1
|
+
export type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_VANILLA_REGEX_FLAGS' | 'NOT_IMPLEMENTED' | 'INVALID_IDENTIFIER';
|
|
2
2
|
export declare class RGXError extends Error {
|
|
3
3
|
code: RGXErrorCode;
|
|
4
4
|
constructor(message: string, code?: RGXErrorCode);
|
package/dist/errors/index.d.ts
CHANGED
package/dist/errors/index.js
CHANGED
|
@@ -19,3 +19,4 @@ __exportStar(require("./invalidToken"), exports);
|
|
|
19
19
|
__exportStar(require("./invalidRegexString"), exports);
|
|
20
20
|
__exportStar(require("./invalidVanillaRegexFlags"), exports);
|
|
21
21
|
__exportStar(require("./notImplemented"), exports);
|
|
22
|
+
__exportStar(require("./invalidIdentifier"), exports);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RGXInvalidIdentifierError = void 0;
|
|
4
|
+
const errors_1 = require("./");
|
|
5
|
+
class RGXInvalidIdentifierError extends errors_1.RGXError {
|
|
6
|
+
constructor(message, got) {
|
|
7
|
+
super(message, 'INVALID_IDENTIFIER');
|
|
8
|
+
this.name = 'RGXInvalidIdentifierError';
|
|
9
|
+
this.got = got;
|
|
10
|
+
}
|
|
11
|
+
toString() {
|
|
12
|
+
return `${this.name}: ${this.message}; Got: ${JSON.stringify(this.got)}`;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.RGXInvalidIdentifierError = RGXInvalidIdentifierError;
|
|
@@ -46,7 +46,7 @@ class RGXInvalidTokenError extends errors_1.RGXError {
|
|
|
46
46
|
this.setExpected(expected);
|
|
47
47
|
}
|
|
48
48
|
toString() {
|
|
49
|
-
const gotString =
|
|
49
|
+
const gotString = class_1.RGXClassToken.check(this.got) ? `instance of ${this.got.constructor.name}` : JSON.stringify(this.got);
|
|
50
50
|
return `${this.name}: ${this.message}; Expected: ${this.expected}; Got: [${gotString}]`;
|
|
51
51
|
}
|
|
52
52
|
}
|
package/dist/resolve.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import * as t from "./types";
|
|
2
2
|
export declare function escapeRegex(value: string): t.ValidRegexString;
|
|
3
|
-
export declare function resolveRGXToken(token: t.RGXToken, groupWrap?: boolean): t.ValidRegexString;
|
|
3
|
+
export declare function resolveRGXToken(token: t.RGXToken, groupWrap?: boolean, topLevel?: boolean): t.ValidRegexString;
|
package/dist/resolve.js
CHANGED
|
@@ -41,7 +41,7 @@ const tg = __importStar(require("./typeGuards"));
|
|
|
41
41
|
function escapeRegex(value) {
|
|
42
42
|
return value.replaceAll(/[\-\^\$.*+?^${}()|[\]\\]/g, '\\$&');
|
|
43
43
|
}
|
|
44
|
-
function resolveRGXToken(token, groupWrap = true) {
|
|
44
|
+
function resolveRGXToken(token, groupWrap = true, topLevel = true) {
|
|
45
45
|
if (tg.isRGXNoOpToken(token))
|
|
46
46
|
return '';
|
|
47
47
|
if (tg.isRGXLiteralToken(token)) {
|
|
@@ -53,7 +53,9 @@ function resolveRGXToken(token, groupWrap = true) {
|
|
|
53
53
|
if (tg.isRGXNativeToken(token))
|
|
54
54
|
return escapeRegex(String(token));
|
|
55
55
|
if (tg.isRGXConvertibleToken(token)) {
|
|
56
|
-
|
|
56
|
+
// The top-level group-wrapping preference propogates to a direct convertible token, but after that
|
|
57
|
+
// the preference falls back to true whenever a token doesn't explicitly specify a preference.
|
|
58
|
+
return resolveRGXToken(token.toRgx(), token.rgxGroupWrap ?? (topLevel ? groupWrap : true), false);
|
|
57
59
|
}
|
|
58
60
|
// Interpret arrays as unions
|
|
59
61
|
if (tg.isRGXArrayToken(token, false)) {
|
|
@@ -64,9 +66,9 @@ function resolveRGXToken(token, groupWrap = true) {
|
|
|
64
66
|
token = [...(0, class_1.removeRgxUnionDuplicates)(...token)];
|
|
65
67
|
// Don't preserve group wrapping preference for the recursive calls
|
|
66
68
|
if (groupWrap)
|
|
67
|
-
return '(?:' + token.map(t => resolveRGXToken(t, true)).join('|') + ')';
|
|
69
|
+
return '(?:' + token.map(t => resolveRGXToken(t, true, false)).join('|') + ')';
|
|
68
70
|
else
|
|
69
|
-
return token.map(t => resolveRGXToken(t, true)).join('|');
|
|
71
|
+
return token.map(t => resolveRGXToken(t, true, false)).join('|');
|
|
70
72
|
}
|
|
71
73
|
return resolveRGXToken(token[0]);
|
|
72
74
|
}
|
package/dist/typeGuards.d.ts
CHANGED
|
@@ -20,3 +20,5 @@ export declare function isValidRegexString(value: string): value is t.ValidRegex
|
|
|
20
20
|
export declare function assertValidRegexString(value: string): asserts value is t.ValidRegexString;
|
|
21
21
|
export declare function isValidVanillaRegexFlags(value: string): value is t.ValidVanillaRegexFlags;
|
|
22
22
|
export declare function assertValidVanillaRegexFlags(value: string): asserts value is t.ValidVanillaRegexFlags;
|
|
23
|
+
export declare function isValidIdentifier(value: string): value is t.ValidIdentifier;
|
|
24
|
+
export declare function assertValidIdentifier(value: string): asserts value is t.ValidIdentifier;
|
package/dist/typeGuards.js
CHANGED
|
@@ -57,10 +57,12 @@ exports.isValidRegexString = isValidRegexString;
|
|
|
57
57
|
exports.assertValidRegexString = assertValidRegexString;
|
|
58
58
|
exports.isValidVanillaRegexFlags = isValidVanillaRegexFlags;
|
|
59
59
|
exports.assertValidVanillaRegexFlags = assertValidVanillaRegexFlags;
|
|
60
|
+
exports.isValidIdentifier = isValidIdentifier;
|
|
61
|
+
exports.assertValidIdentifier = assertValidIdentifier;
|
|
60
62
|
const e = __importStar(require("./errors"));
|
|
61
|
-
const class_1 = require("./class");
|
|
62
63
|
const is_callable_1 = __importDefault(require("is-callable"));
|
|
63
64
|
const internal_1 = require("./internal");
|
|
65
|
+
const class_1 = require("./class");
|
|
64
66
|
function isRGXNoOpToken(value) {
|
|
65
67
|
return value === null || value === undefined;
|
|
66
68
|
}
|
|
@@ -87,6 +89,9 @@ function assertRGXNativeToken(value) {
|
|
|
87
89
|
}
|
|
88
90
|
function isRGXConvertibleToken(value, returnCheck = true) {
|
|
89
91
|
if (typeof value === 'object' && value !== null && 'toRgx' in value) {
|
|
92
|
+
// The rgxGroupWrap property is optional, but if it exists it must be a boolean.
|
|
93
|
+
if ('rgxGroupWrap' in value && typeof value.rgxGroupWrap !== 'boolean')
|
|
94
|
+
return false;
|
|
90
95
|
if ((0, is_callable_1.default)(value.toRgx)) {
|
|
91
96
|
if (!returnCheck)
|
|
92
97
|
return true;
|
|
@@ -122,7 +127,7 @@ function rgxTokenTypeFlat(value, recognizeClass = true) {
|
|
|
122
127
|
if (isRGXNativeToken(value))
|
|
123
128
|
return 'native';
|
|
124
129
|
// We have to check class before convertible because class tokens are also convertible.
|
|
125
|
-
if (recognizeClass &&
|
|
130
|
+
if (recognizeClass && class_1.RGXClassToken.check(value))
|
|
126
131
|
return 'class';
|
|
127
132
|
if (isRGXConvertibleToken(value))
|
|
128
133
|
return 'convertible';
|
|
@@ -167,7 +172,7 @@ function isRGXToken(value, type = null, matchLength = true) {
|
|
|
167
172
|
if (typeMatches('native') && isRGXNativeToken(value))
|
|
168
173
|
return true;
|
|
169
174
|
// We have to check class before convertible because class tokens are also convertible.
|
|
170
|
-
if (typeMatches('class') &&
|
|
175
|
+
if (typeMatches('class') && class_1.RGXClassToken.check(value))
|
|
171
176
|
return true;
|
|
172
177
|
if (typeMatches('convertible') && isRGXConvertibleToken(value))
|
|
173
178
|
return true;
|
|
@@ -215,3 +220,12 @@ function assertValidVanillaRegexFlags(value) {
|
|
|
215
220
|
throw new e.RGXInvalidVanillaRegexFlagsError("Invalid vanilla regex flags", value);
|
|
216
221
|
}
|
|
217
222
|
}
|
|
223
|
+
function isValidIdentifier(value) {
|
|
224
|
+
// This regex checks for valid JavaScript identifiers, which can be used for named capture groups.
|
|
225
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(value);
|
|
226
|
+
}
|
|
227
|
+
function assertValidIdentifier(value) {
|
|
228
|
+
if (!isValidIdentifier(value)) {
|
|
229
|
+
throw new e.RGXInvalidIdentifierError("Invalid identifier", value);
|
|
230
|
+
}
|
|
231
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export type RGXLiteralToken = RegExp;
|
|
|
5
5
|
export type RGXNativeToken = string | number | boolean | RGXNoOpToken;
|
|
6
6
|
export type RGXConvertibleToken = {
|
|
7
7
|
toRgx: () => RGXToken;
|
|
8
|
+
readonly rgxGroupWrap?: boolean;
|
|
8
9
|
};
|
|
9
10
|
export type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
|
|
10
11
|
export type RGXClassTokenConstructor = new (...args: unknown[]) => RGXClassToken;
|
|
@@ -20,3 +21,6 @@ export type ValidRegexString = Branded<string, [ValidRegexBrandSymbol]>;
|
|
|
20
21
|
export declare const validVanillaRegexFlagsSymbol: unique symbol;
|
|
21
22
|
export type ValidVanillaRegexFlagsBrandSymbol = typeof validVanillaRegexFlagsSymbol;
|
|
22
23
|
export type ValidVanillaRegexFlags = Branded<string, [ValidVanillaRegexFlagsBrandSymbol]>;
|
|
24
|
+
export declare const validIdentifierSymbol: unique symbol;
|
|
25
|
+
export type ValidIdentifierBrandSymbol = typeof validIdentifierSymbol;
|
|
26
|
+
export type ValidIdentifier = Branded<string, [ValidIdentifierBrandSymbol]>;
|
package/dist/types.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.validVanillaRegexFlagsSymbol = exports.validRegexSymbol = void 0;
|
|
3
|
+
exports.validIdentifierSymbol = exports.validVanillaRegexFlagsSymbol = exports.validRegexSymbol = void 0;
|
|
4
4
|
exports.validRegexSymbol = Symbol('rgx.ValidRegex');
|
|
5
5
|
exports.validVanillaRegexFlagsSymbol = Symbol('rgx.ValidVanillaRegexFlags');
|
|
6
|
+
exports.validIdentifierSymbol = Symbol('rgx.ValidIdentifier');
|