@ptolemy2002/rgx 7.7.1 → 7.7.3
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 +31 -1709
- package/dist/errors/partValidationFailed.d.ts +2 -1
- package/dist/errors/partValidationFailed.js +3 -2
- package/dist/index.js +1 -0
- package/dist/types.d.ts +3 -2
- package/dist/utils/regexWithFlags.js +5 -5
- package/dist/walker/part.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,1716 +1,38 @@
|
|
|
1
1
|
# RGX
|
|
2
|
-
A library for easy construction and validation of regular expressions in TypeScript. You can use `rgx` to concatenate various types of tokens into a valid regular expression string, with type safety and validation.
|
|
2
|
+
A library for easy construction and validation of regular expressions in TypeScript. You can use `rgx` to concatenate various types of tokens into a valid regular expression string, with type safety and validation. You can also use a combination of `RGXWalker`, `RGXPart`, and plain tokens to create powerful matchers that can validate partial matches and transform captured values with custom logic.
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
## Type Reference
|
|
7
|
-
```typescript
|
|
8
|
-
import { Branded } from "@ptolemy2002/ts-brand-utils";
|
|
9
|
-
import { CloneDepth } from "@ptolemy2002/immutability-utils";
|
|
10
|
-
// type CloneDepth = number | "max";
|
|
11
|
-
|
|
12
|
-
type RGXNoOpToken = null | undefined;
|
|
13
|
-
type RGXLiteralToken = RegExp;
|
|
14
|
-
type RGXNativeToken = string | number | boolean | RGXNoOpToken;
|
|
15
|
-
type RGXConvertibleToken = {
|
|
16
|
-
toRgx: () => RGXToken,
|
|
17
|
-
rgxAcceptInsertion?: (tokens: RGXToken[], flags: ValidRegexFlags) => string | boolean,
|
|
18
|
-
readonly rgxGroupWrap?: boolean,
|
|
19
|
-
readonly rgxIsGroup?: boolean,
|
|
20
|
-
readonly rgxIsRepeatable?: boolean
|
|
21
|
-
};
|
|
22
|
-
type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
|
|
23
|
-
|
|
24
|
-
type RGXClassTokenConstructor = new (...args: unknown[]) => RGXClassToken;
|
|
25
|
-
type RGXGroupedToken = RGXToken[] | RGXLiteralToken | RGXGroupedConvertibleToken;
|
|
26
|
-
type RGXGroupedConvertibleToken = (RGXConvertibleToken & { readonly rgxIsGroup: true }) | (Omit<RGXConvertibleToken, "toRgx"> & { toRgx: () => RGXGroupedToken, readonly rgxGroupWrap: true });
|
|
27
|
-
type RGXRepeatableConvertibleToken = RGXConvertibleToken & { readonly rgxIsRepeatable: true | undefined };
|
|
28
|
-
|
|
29
|
-
const validRegexSymbol = Symbol('rgx.ValidRegex');
|
|
30
|
-
type ValidRegexBrandSymbol = typeof validRegexSymbol;
|
|
31
|
-
type ValidRegexString = Branded<string, [ValidRegexBrandSymbol]>;
|
|
32
|
-
|
|
33
|
-
const validVanillaRegexFlagsSymbol = Symbol('rgx.ValidVanillaRegexFlags');
|
|
34
|
-
type ValidVanillaRegexFlagsBrandSymbol = typeof validVanillaRegexFlagsSymbol;
|
|
35
|
-
type ValidVanillaRegexFlags = Branded<string, [ValidVanillaRegexFlagsBrandSymbol]>;
|
|
36
|
-
|
|
37
|
-
const validRegexFlagsSymbol = Symbol('rgx.ValidRegexFlags');
|
|
38
|
-
type ValidRegexFlagsBrandSymbol = typeof validRegexFlagsSymbol;
|
|
39
|
-
type ValidRegexFlags = Branded<string, [ValidRegexFlagsBrandSymbol]> | ValidVanillaRegexFlags;
|
|
40
|
-
|
|
41
|
-
type RegExpFlagTransformer = (exp: RegExp) => RegExp;
|
|
42
|
-
|
|
43
|
-
const validIdentifierSymbol = Symbol('rgx.ValidIdentifier');
|
|
44
|
-
type ValidIdentifierBrandSymbol = typeof validIdentifierSymbol;
|
|
45
|
-
type ValidIdentifier = Branded<string, [ValidIdentifierBrandSymbol]>;
|
|
46
|
-
|
|
47
|
-
type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | 'class' | RGXTokenType[];
|
|
48
|
-
type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
|
|
49
|
-
type RGXTokenTypeGuardInput = "repeatable" | RGXTokenTypeFlat | null | RGXClassTokenConstructor | typeof RegExp | typeof ExtRegExp | typeof RGXTokenCollection | RGXTokenTypeGuardInput[];
|
|
50
|
-
type RGXTokenFromType<T extends RGXTokenTypeGuardInput> =
|
|
51
|
-
// Maps token type strings to their corresponding types, e.g.:
|
|
52
|
-
// 'no-op' -> RGXNoOpToken, 'literal' -> RGXLiteralToken, etc.
|
|
53
|
-
// Also maps any constructor to InstanceType<T>,
|
|
54
|
-
// and preserves tuple types for constant arrays.
|
|
55
|
-
// ... see source for full definition
|
|
56
|
-
;
|
|
57
|
-
|
|
58
|
-
type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_REGEX_FLAGS' | 'INVALID_VANILLA_REGEX_FLAGS' | 'NOT_IMPLEMENTED' | 'NOT_SUPPORTED' | 'INVALID_IDENTIFIER' | 'OUT_OF_BOUNDS' | 'INVALID_FLAG_TRANSFORMER_KEY' | 'FLAG_TRANSFORMER_CONFLICT' | 'CONSTANT_CONFLICT' | 'INVALID_CONSTANT_KEY' | 'INSERTION_REJECTED' | 'REGEX_NOT_MATCHED_AT_POSITION';
|
|
59
|
-
|
|
60
|
-
type RangeObject = {
|
|
61
|
-
min?: number | null;
|
|
62
|
-
max?: number | null;
|
|
63
|
-
inclusiveLeft?: boolean;
|
|
64
|
-
inclusiveRight?: boolean;
|
|
65
|
-
};
|
|
66
|
-
type ExpectedTokenType = {
|
|
67
|
-
type: "tokenType";
|
|
68
|
-
values: RGXTokenTypeFlat[];
|
|
69
|
-
} | {
|
|
70
|
-
type: "custom";
|
|
71
|
-
values: string[];
|
|
72
|
-
};
|
|
73
|
-
type RGXTokenCollectionMode = 'union' | 'concat';
|
|
74
|
-
type RGXTokenCollectionInput = RGXToken | RGXTokenCollection;
|
|
75
|
-
|
|
76
|
-
type RGXUnionInsertionPosition = 'prefix' | 'suffix';
|
|
77
|
-
|
|
78
|
-
type RGXGroupTokenArgs = {
|
|
79
|
-
name?: string | null;
|
|
80
|
-
capturing?: boolean;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
type RGXPartControl = "skip" | "stop" | "silent" | void;
|
|
84
|
-
|
|
85
|
-
type RGXCapture<T = unknown> = {
|
|
86
|
-
raw: string;
|
|
87
|
-
value: T;
|
|
88
|
-
start: number;
|
|
89
|
-
end: number;
|
|
90
|
-
ownerId: string | null;
|
|
91
|
-
branch: number;
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
type RGXPartOptions<R, T=string> = {
|
|
95
|
-
id: string;
|
|
96
|
-
rawTransform: (captured: string) => string;
|
|
97
|
-
transform: (captured: string) => T;
|
|
98
|
-
validate: (captured: RGXCapture<T>, part: RGXPart<R, T>, walker: RGXWalker<R>) => boolean | string;
|
|
99
|
-
beforeCapture: ((part: RGXPart<R, T>, walker: RGXWalker<R>) => RGXPartControl) | null;
|
|
100
|
-
afterCapture: ((capture: RGXCapture<T>, part: RGXPart<R, T>, walker: RGXWalker<R>) => void) | null;
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
type RGXWalkerOptions<R> = {
|
|
104
|
-
startingSourcePosition?: number;
|
|
105
|
-
reduced?: R;
|
|
106
|
-
infinite?: boolean;
|
|
107
|
-
looping?: boolean;
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
type RGXWOptions<R = unknown> = Omit<RGXWalkerOptions<R>, "startingSourcePosition"> & {
|
|
111
|
-
multiline?: boolean;
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
// See src/constants.ts for the actual mapping of predefined constant names to their token values
|
|
115
|
-
type RGXPredefinedConstant = keyof typeof RGX_PREDEFINED_CONSTANTS;
|
|
116
|
-
type RGXConstantName = RGXPredefinedConstant | (string & {});
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
## Classes
|
|
120
|
-
The library exports the following classes:
|
|
121
|
-
|
|
122
|
-
### RGXError
|
|
123
|
-
A custom error class for RGX-related errors. This can be used to throw specific errors related to RGX token validation or resolution.
|
|
124
|
-
|
|
125
|
-
#### Constructor
|
|
126
|
-
```typescript
|
|
127
|
-
constructor(message: string, code?: RGXErrorCode)
|
|
128
|
-
```
|
|
129
|
-
- `message` (`string`): The error message.
|
|
130
|
-
- `code` (`RGXErrorCode`, optional): An optional error code that can be used to categorize the error. If not provided, it defaults to 'UNKNOWN'.
|
|
131
|
-
|
|
132
|
-
#### Properties
|
|
133
|
-
- `code` (`RGXErrorCode`): The error code associated with the error, which can be used to identify the type of error that occurred.
|
|
134
|
-
|
|
135
|
-
#### Methods
|
|
136
|
-
- `toString() => string`: Returns a formatted string in the format `${name}: ${message}`. Subclasses customize the message portion via internal formatting rather than overriding `toString()` directly, so all `RGXError` subclasses produce consistently formatted strings through this single method.
|
|
137
|
-
|
|
138
|
-
### RGXInvalidTokenError extends RGXError
|
|
139
|
-
A specific error class for invalid RGX tokens. This error is thrown when a value fails validation as a specific RGX token type. The error code is set to `INVALID_RGX_TOKEN` on instantiation.
|
|
140
|
-
|
|
141
|
-
#### Constructor
|
|
142
|
-
```typescript
|
|
143
|
-
constructor(message: string, expected: ExpectedTokenType | null, got: unknown)
|
|
144
|
-
```
|
|
145
|
-
- `message` (`string`): The error message.
|
|
146
|
-
- `expected` (`ExpectedTokenType | null`): Either an object describing the expected token type(s) or `null` if all token types are expected. This is used to generate a human-readable description of what was expected.
|
|
147
|
-
- `got` (`unknown`): The actual value that was received, which failed validation.
|
|
148
|
-
|
|
149
|
-
#### Properties
|
|
150
|
-
- `expected` (`string`): A human-readable description of the expected token type(s), generated from the `expected` parameter in the constructor. This can be used to provide more informative error messages.
|
|
151
|
-
- `got` (`unknown`): The actual value that was received, which failed validation.
|
|
152
|
-
|
|
153
|
-
### RGXInvalidRegexStringError extends RGXError
|
|
154
|
-
A specific error class for invalid regex strings. This error is thrown when a string fails validation as a valid regex string. The error code is set to `INVALID_REGEX_STRING` on instantiation.
|
|
155
|
-
|
|
156
|
-
#### Constructor
|
|
157
|
-
```typescript
|
|
158
|
-
constructor(message: string, got: string)
|
|
159
|
-
```
|
|
160
|
-
- `message` (`string`): The error message.
|
|
161
|
-
- `got` (`string`): The actual string that was received, which failed validation.
|
|
162
|
-
|
|
163
|
-
#### Properties
|
|
164
|
-
- `got` (`string`): The actual string that was received, which failed validation.
|
|
165
|
-
|
|
166
|
-
### RGXInvalidVanillaRegexFlagsError extends RGXError
|
|
167
|
-
A specific error class for invalid vanilla regex flags. This error is thrown when a string fails validation as valid vanilla regex flags. The error code is set to `INVALID_VANILLA_REGEX_FLAGS` on instantiation.
|
|
168
|
-
|
|
169
|
-
#### Constructor
|
|
170
|
-
```typescript
|
|
171
|
-
constructor(message: string, got: string)
|
|
172
|
-
```
|
|
173
|
-
- `message` (`string`): The error message.
|
|
174
|
-
- `got` (`string`): The actual string that was received, which failed validation.
|
|
175
|
-
|
|
176
|
-
#### Properties
|
|
177
|
-
- `got` (`string`): The actual string that was received, which failed validation.
|
|
178
|
-
|
|
179
|
-
### RGXNotImplementedError extends RGXError
|
|
180
|
-
A specific error class for unimplemented functionality. This error is thrown when a feature or method has not been implemented yet. The error code is set to `NOT_IMPLEMENTED` on instantiation.
|
|
181
|
-
|
|
182
|
-
#### Constructor
|
|
183
|
-
```typescript
|
|
184
|
-
constructor(functionality: string, message?: string | null)
|
|
185
|
-
```
|
|
186
|
-
- `functionality` (`string`): A description of the functionality that is not yet implemented.
|
|
187
|
-
- `message` (`string | null`, optional): An optional additional message providing more context. Defaults to `null`.
|
|
188
|
-
|
|
189
|
-
#### Properties
|
|
190
|
-
- `functionality` (`string`): The description of the unimplemented functionality.
|
|
191
|
-
|
|
192
|
-
### RGXNotSupportedError extends RGXError
|
|
193
|
-
A specific error class for unsupported functionality. This error is thrown when a feature or method is intentionally not supported (as opposed to simply not yet implemented). The error code is set to `NOT_SUPPORTED` on instantiation.
|
|
194
|
-
|
|
195
|
-
#### Constructor
|
|
196
|
-
```typescript
|
|
197
|
-
constructor(functionality: string, message?: string | null)
|
|
198
|
-
```
|
|
199
|
-
- `functionality` (`string`): A description of the functionality that is not supported.
|
|
200
|
-
- `message` (`string | null`, optional): An optional additional message providing more context. Defaults to `null`.
|
|
201
|
-
|
|
202
|
-
#### Properties
|
|
203
|
-
- `functionality` (`string`): The description of the unsupported functionality.
|
|
204
|
-
|
|
205
|
-
### RGXInvalidIdentifierError extends RGXError
|
|
206
|
-
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.
|
|
207
|
-
|
|
208
|
-
#### Constructor
|
|
209
|
-
```typescript
|
|
210
|
-
constructor(message: string, got: string)
|
|
211
|
-
```
|
|
212
|
-
- `message` (`string`): The error message.
|
|
213
|
-
- `got` (`string`): The actual string that was received, which failed validation.
|
|
214
|
-
|
|
215
|
-
#### Properties
|
|
216
|
-
- `got` (`string`): The actual string that was received, which failed validation.
|
|
217
|
-
|
|
218
|
-
### RGXInvalidRegexFlagsError extends RGXError
|
|
219
|
-
A specific error class for invalid regex flags (including both vanilla and custom registered flags). This error is thrown when a string fails validation as valid regex flags. The error code is set to `INVALID_REGEX_FLAGS` on instantiation.
|
|
220
|
-
|
|
221
|
-
#### Constructor
|
|
222
|
-
```typescript
|
|
223
|
-
constructor(message: string, got: string)
|
|
224
|
-
```
|
|
225
|
-
- `message` (`string`): The error message.
|
|
226
|
-
- `got` (`string`): The actual string that was received, which failed validation.
|
|
227
|
-
|
|
228
|
-
#### Properties
|
|
229
|
-
- `got` (`string`): The actual string that was received, which failed validation.
|
|
230
|
-
|
|
231
|
-
### RGXInvalidFlagTransformerKeyError extends RGXError
|
|
232
|
-
A specific error class for invalid flag transformer keys. This error is thrown when an invalid key is provided to `registerFlagTransformer` (e.g., a key that is not a single character). The error code is set to `INVALID_FLAG_TRANSFORMER_KEY` on instantiation.
|
|
233
|
-
|
|
234
|
-
#### Constructor
|
|
235
|
-
```typescript
|
|
236
|
-
constructor(message: string, got: string)
|
|
237
|
-
```
|
|
238
|
-
- `message` (`string`): The error message.
|
|
239
|
-
- `got` (`string`): The actual key string that was received, which failed validation.
|
|
240
|
-
|
|
241
|
-
#### Properties
|
|
242
|
-
- `got` (`string`): The actual key string that was received, which failed validation.
|
|
243
|
-
|
|
244
|
-
### RGXFlagTransformerConflictError extends RGXError
|
|
245
|
-
A specific error class for flag transformer conflicts. This error is thrown when attempting to register a flag transformer with a key that conflicts with an existing vanilla regex flag or an already-registered transformer. The error code is set to `FLAG_TRANSFORMER_CONFLICT` on instantiation.
|
|
246
|
-
|
|
247
|
-
#### Constructor
|
|
248
|
-
```typescript
|
|
249
|
-
constructor(message: string, got: string)
|
|
250
|
-
```
|
|
251
|
-
- `message` (`string`): The error message.
|
|
252
|
-
- `got` (`string`): The conflicting key string.
|
|
253
|
-
|
|
254
|
-
#### Properties
|
|
255
|
-
- `got` (`string`): The conflicting key string.
|
|
256
|
-
|
|
257
|
-
### RGXInvalidConstantKeyError extends RGXError
|
|
258
|
-
A specific error class for invalid constant keys. This error is thrown when attempting to access or assert an RGX constant with a name that does not exist. The error code is set to `INVALID_CONSTANT_KEY` on instantiation.
|
|
259
|
-
|
|
260
|
-
#### Constructor
|
|
261
|
-
```typescript
|
|
262
|
-
constructor(message: string, got: string)
|
|
263
|
-
```
|
|
264
|
-
- `message` (`string`): The error message.
|
|
265
|
-
- `got` (`string`): The constant name that was not found.
|
|
266
|
-
|
|
267
|
-
#### Properties
|
|
268
|
-
- `got` (`string`): The constant name that was not found.
|
|
269
|
-
|
|
270
|
-
### RGXConstantConflictError extends RGXError
|
|
271
|
-
A specific error class for constant name conflicts. This error is thrown when attempting to define an RGX constant with a name that is already in use. The error code is set to `CONSTANT_CONFLICT` on instantiation.
|
|
272
|
-
|
|
273
|
-
#### Constructor
|
|
274
|
-
```typescript
|
|
275
|
-
constructor(message: string, got: string)
|
|
276
|
-
```
|
|
277
|
-
- `message` (`string`): The error message.
|
|
278
|
-
- `got` (`string`): The conflicting constant name.
|
|
279
|
-
|
|
280
|
-
#### Properties
|
|
281
|
-
- `got` (`string`): The conflicting constant name.
|
|
282
|
-
|
|
283
|
-
### RGXInsertionRejectedError extends RGXError
|
|
284
|
-
A specific error class for token insertion rejection. This error is thrown when a convertible token's `rgxAcceptInsertion` method returns `false` or a string (rejection reason) during pattern construction via `rgx`, `rgxa`, or `rgxConcat`. The error code is set to `INSERTION_REJECTED` on instantiation.
|
|
285
|
-
|
|
286
|
-
#### Constructor
|
|
287
|
-
```typescript
|
|
288
|
-
constructor(reason?: string | null, message?: string | null)
|
|
289
|
-
```
|
|
290
|
-
- `reason` (`string | null`, optional): The reason the insertion was rejected. Defaults to `null`.
|
|
291
|
-
- `message` (`string | null`, optional): An optional additional message providing more context. Defaults to `null`.
|
|
292
|
-
|
|
293
|
-
#### Properties
|
|
294
|
-
- `reason` (`string | null`): The reason the insertion was rejected, or `null` if no reason was provided.
|
|
295
|
-
|
|
296
|
-
### RGXRegexNotMatchedAtPositionError extends RGXError
|
|
297
|
-
A specific error class for regex match failures at a given position. This error is thrown when a regex is expected to match at a specific position in a string but does not (e.g., via `assertRegexMatchesAtPosition`). The error code is set to `REGEX_NOT_MATCHED_AT_POSITION` on instantiation.
|
|
298
|
-
|
|
299
|
-
#### Constructor
|
|
300
|
-
```typescript
|
|
301
|
-
constructor(message: string, pattern: RegExp, source: string, position: number, contextSize?: number | null)
|
|
302
|
-
```
|
|
303
|
-
- `message` (`string`): The error message.
|
|
304
|
-
- `pattern` (`RegExp`): The regex pattern that failed to match.
|
|
305
|
-
- `source` (`string`): The string that was being matched against.
|
|
306
|
-
- `position` (`number`): The zero-based index in the source string where the match was expected. Must be >= 0 and < `source.length`, or an `RGXOutOfBoundsError` will be thrown.
|
|
307
|
-
- `contextSize` (`number | null`, optional): The number of characters on each side of the position to include in contextual output. Defaults to `null` (full source shown).
|
|
308
|
-
|
|
309
|
-
#### Properties
|
|
310
|
-
- `pattern` (`RegExp`): The regex pattern that failed to match.
|
|
311
|
-
- `source` (`string`): The string that was being matched against.
|
|
312
|
-
- `position` (`number`): The position where the match was expected. Setting this validates that the value is >= 0 and < `source.length`, throwing `RGXOutOfBoundsError` if not.
|
|
313
|
-
- `contextSize` (`number | null`): The number of characters on each side of the position to include in contextual output, or `null` for the full source.
|
|
314
|
-
|
|
315
|
-
#### Methods
|
|
316
|
-
- `sourceContext() => string`: Returns the relevant portion of the source string around the position. When `contextSize` is `null` or covers the entire string, returns the full source. Otherwise, returns a substring from `max(0, position - contextSize)` to `min(source.length, position + contextSize)`.
|
|
317
|
-
- `hasLeftContext() => boolean`: Returns `true` if the context window starts after the beginning of the source string (i.e., there is truncated content on the left). Returns `false` when `contextSize` is `null`.
|
|
318
|
-
- `hasRightContext() => boolean`: Returns `true` if the context window ends before the end of the source string (i.e., there is truncated content on the right). Returns `false` when `contextSize` is `null`.
|
|
319
|
-
- `hasFullContext() => boolean`: Returns `true` when the full source is shown (neither side is truncated). This is the case when `contextSize` is `null` or when the context window covers the entire source string.
|
|
320
|
-
|
|
321
|
-
### RGXOutOfBoundsError extends RGXError
|
|
322
|
-
A specific error class for out-of-bounds values. This error is thrown when a numeric value falls outside an expected range. The error code is set to `OUT_OF_BOUNDS` on instantiation.
|
|
323
|
-
|
|
324
|
-
#### Constructor
|
|
325
|
-
```typescript
|
|
326
|
-
constructor(message: string, got: number, { min, max, inclusiveLeft, inclusiveRight }?: RangeObject)
|
|
327
|
-
```
|
|
328
|
-
- `message` (`string`): The error message.
|
|
329
|
-
- `got` (`number`): The actual numeric value that was received, which fell outside the expected range.
|
|
330
|
-
- `min` (`number | null`, optional): The minimum bound of the range. Defaults to `null` (no minimum).
|
|
331
|
-
- `max` (`number | null`, optional): The maximum bound of the range. Defaults to `null` (no maximum). Setting `min` to a value greater than `max` will adjust `max` to equal `min`, and vice versa.
|
|
332
|
-
- `inclusiveLeft` (`boolean`, optional): Whether the minimum bound is inclusive. Defaults to `true`.
|
|
333
|
-
- `inclusiveRight` (`boolean`, optional): Whether the maximum bound is inclusive. Defaults to `true`.
|
|
334
|
-
|
|
335
|
-
#### Properties
|
|
336
|
-
- `got` (`number`): The actual numeric value that was received.
|
|
337
|
-
- `min` (`number | null`): The minimum bound of the range. Setting this to a value greater than `max` will adjust `max` to equal `min`.
|
|
338
|
-
- `max` (`number | null`): The maximum bound of the range. Setting this to a value less than `min` will adjust `min` to equal `max`.
|
|
339
|
-
- `inclusiveLeft` (`boolean`): Whether the minimum bound is inclusive.
|
|
340
|
-
- `inclusiveRight` (`boolean`): Whether the maximum bound is inclusive.
|
|
341
|
-
|
|
342
|
-
#### Methods
|
|
343
|
-
- `failedAtMin() => boolean`: Returns `true` if the `got` value is below the minimum bound (respecting `inclusiveLeft`), otherwise `false`. Returns `false` if `min` is `null`.
|
|
344
|
-
- `failedAtMax() => boolean`: Returns `true` if the `got` value is above the maximum bound (respecting `inclusiveRight`), otherwise `false`. Returns `false` if `max` is `null`.
|
|
345
|
-
- `failedAtAny() => boolean`: Returns `true` if the value failed at either the minimum or maximum bound.
|
|
346
|
-
|
|
347
|
-
### RGXPartValidationFailedError extends RGXError
|
|
348
|
-
A specific error class for RGX part validation failures. This error is thrown when a captured value fails validation in a custom part's `validate` function. The error code is set to `PART_VALIDATION_FAILED` on instantiation.
|
|
349
|
-
|
|
350
|
-
#### Constructor
|
|
351
|
-
```typescript
|
|
352
|
-
constructor(message: string, gotRaw: string, gotTransformed: unknown)
|
|
353
|
-
```
|
|
354
|
-
- `message` (`string`): The error message.
|
|
355
|
-
- `gotRaw` (`string`): The raw captured string value that failed validation.
|
|
356
|
-
- `gotTransformed` (`unknown`): The transformed value that was produced by the part's `transform` function, which also failed validation.
|
|
357
|
-
|
|
358
|
-
#### Properties
|
|
359
|
-
- `gotRaw` (`string`): The raw captured string value that failed validation.
|
|
360
|
-
- `gotTransformed` (`unknown`): The transformed value that was produced by the part's `transform` function, which also failed validation.
|
|
361
|
-
|
|
362
|
-
### RGXTokenCollection
|
|
363
|
-
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.
|
|
364
|
-
|
|
365
|
-
A function `rgxTokenCollection` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
|
|
366
|
-
|
|
367
|
-
#### Constructor
|
|
368
|
-
```typescript
|
|
369
|
-
constructor(tokens: RGXTokenCollectionInput = [], mode: RGXTokenCollectionMode = 'concat')
|
|
370
|
-
```
|
|
371
|
-
- `tokens` (`RGXTokenCollectionInput`, optional): The tokens to be managed by the collection. This can be an array of RGX tokens, a single RGX token (which will be wrapped in an array), or another `RGXTokenCollection` (whose tokens will be copied). Defaults to an empty array.
|
|
372
|
-
- `mode` (`RGXTokenCollectionMode`, optional): The mode of the collection, either 'union' or 'concat'. Defaults to 'concat'.
|
|
373
|
-
|
|
374
|
-
#### Properties
|
|
375
|
-
- `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.
|
|
376
|
-
- `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.
|
|
377
|
-
- `resolve() => ValidRegexString`: A convenience method that resolves this collection by calling `resolveRGXToken(this)`, returning the resolved regex string representation.
|
|
378
|
-
- `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.
|
|
379
|
-
- `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.
|
|
380
|
-
- `toArray() => RGXToken[]`: An alias for `getTokens()`, provided for convenience.
|
|
381
|
-
- `clone(depth: CloneDepth = "max") => RGXTokenCollection`: A method that creates and returns a deep clone of the RGXTokenCollection instance. This is useful for creating a new collection with the same tokens and mode without affecting the original collection. The `depth` parameter controls how deeply nested collections are cloned: `0` for no clone, `1` for a shallow clone of the top-level collection, any other number for that many levels down, and `"max"` (the default) for a full deep clone.
|
|
382
|
-
- `asConcat() => RGXTokenCollection`: If this collection is in 'union' mode, this method returns a new RGXTokenCollection instance with the same tokens but in 'concat' mode. If the collection is already in 'concat' mode, it simply returns itself.
|
|
383
|
-
- `asUnion() => RGXTokenCollection`: If this collection is in 'concat' mode, this method returns a new RGXTokenCollection instance with the same tokens but in 'union' mode. If the collection is already in 'union' mode, it simply returns itself.
|
|
384
|
-
|
|
385
|
-
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[]`).
|
|
386
|
-
|
|
387
|
-
#### Static Properties
|
|
388
|
-
- `check(value: unknown): value is RGXTokenCollection`: A type guard that checks if the given value is an instance of `RGXTokenCollection`.
|
|
389
|
-
- `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.
|
|
390
|
-
|
|
391
|
-
### RGXClassToken (abstract)
|
|
392
|
-
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).
|
|
393
|
-
|
|
394
|
-
#### Static Properties
|
|
395
|
-
- `check(value: unknown): value is RGXClassToken`: A type guard that checks if the given value is an instance of `RGXClassToken`.
|
|
396
|
-
- `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.
|
|
397
|
-
|
|
398
|
-
#### Abstract Methods
|
|
399
|
-
- `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).
|
|
400
|
-
- `clone(depth: CloneDepth = "max") => RGXClassToken`: Must be implemented by subclasses to return a deep clone of the token instance. The `depth` parameter controls how deeply nested tokens are cloned: `0` for no clone, `1` for a shallow clone of the top-level token, any other number for that many levels down, and `"max"` (the default) for a full deep clone.
|
|
401
|
-
|
|
402
|
-
#### Properties
|
|
403
|
-
- `rgxIsGroup` (`boolean`): Returns `false` by default. Subclasses can override this to indicate whether the token represents a group.
|
|
404
|
-
- `rgxIsRepeatable` (`boolean`): Returns `true` by default. Subclasses can override this to indicate that the token cannot be wrapped in an `RGXRepeatToken`. When `false`, attempting to set this token as the `token` property of an `RGXRepeatToken` (including via `repeat()` or `optional()`) will throw an `RGXNotSupportedError`.
|
|
405
|
-
- `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).
|
|
406
|
-
|
|
407
|
-
#### Methods
|
|
408
|
-
- `rgxAcceptInsertion(tokens: RGXToken[], flags: ValidRegexFlags) => string | boolean`: Called during pattern construction (via `rgx`, `rgxa`, or `rgxConcat`) to allow the token to reject its own insertion based on the surrounding tokens and the pattern's flags. Returns `true` to accept insertion (the default), `false` to reject with no reason, or a string to reject with a reason message. When a token rejects insertion, an `RGXInsertionRejectedError` is thrown. Subclasses can override this to enforce constraints such as requiring certain flags to be present.
|
|
409
|
-
- `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.
|
|
410
|
-
- `group(args?: RGXGroupTokenArgs, ...others: RGXTokenCollectionInput[]) => RGXGroupToken`: Wraps this token in an `RGXGroupToken` with the provided arguments. The `args` parameter defaults to `{}`, which creates a capturing group with no name. Any additional `others` are included in the group alongside `this`. This is a convenience method that creates a new `RGXGroupToken` with `this` and the `others` as the tokens.
|
|
411
|
-
- `repeat(min?: number, max?: number | null, lazy?: boolean) => RGXRepeatToken`: Wraps this token in an `RGXRepeatToken` with the given repetition bounds. `min` defaults to `1`, `max` defaults to `min`, `lazy` defaults to `false`. Pass `null` for `max` to allow unlimited repetitions. When `lazy` is `true`, the resulting quantifier will be non-greedy. This is a convenience method that creates a new `RGXRepeatToken` with `this` as the token. Throws `RGXNotSupportedError` if called on a token with `rgxIsRepeatable` set to `false` (e.g., `RGXLookaroundToken`).
|
|
412
|
-
- `optional(lazy?: boolean) => RGXRepeatToken`: Shorthand for `repeat(0, 1, lazy)`. Wraps this token in an `RGXRepeatToken` that matches the token zero or one times. `lazy` defaults to `false`. Throws `RGXNotSupportedError` if called on a token with `rgxIsRepeatable` set to `false` (e.g., `RGXLookaroundToken`).
|
|
413
|
-
- `asLookahead(positive?: boolean) => RGXLookaheadToken`: Wraps this token in an `RGXLookaheadToken`. `positive` defaults to `true`. If this token is already an `RGXLookaheadToken`, it is returned as-is without re-wrapping.
|
|
414
|
-
- `asLookbehind(positive?: boolean) => RGXLookbehindToken`: Wraps this token in an `RGXLookbehindToken`. `positive` defaults to `true`. If this token is already an `RGXLookbehindToken`, it is returned as-is without re-wrapping.
|
|
415
|
-
- `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`, `RGXGroupToken`, `RGXRepeatToken`, and `RGXLookaroundToken`.
|
|
416
|
-
|
|
417
|
-
### RGXClassUnionToken extends RGXClassToken
|
|
418
|
-
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.
|
|
419
|
-
|
|
420
|
-
A function `rgxClassUnion` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
|
|
421
|
-
|
|
422
|
-
#### Static Properties
|
|
423
|
-
- `check(value: unknown): value is RGXClassUnionToken`: A type guard that checks if the given value is an instance of `RGXClassUnionToken`.
|
|
424
|
-
- `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.
|
|
425
|
-
|
|
426
|
-
#### Constructor
|
|
427
|
-
```typescript
|
|
428
|
-
constructor(tokens: RGXTokenCollectionInput = [])
|
|
429
|
-
```
|
|
430
|
-
- `tokens` (`RGXTokenCollectionInput`, optional): The tokens to include in the union. Internally stored as an `RGXTokenCollection` in 'union' mode. Defaults to an empty array.
|
|
431
|
-
|
|
432
|
-
#### Properties
|
|
433
|
-
- `tokens` (`RGXTokenCollection`): The internal collection of tokens managed in 'union' mode.
|
|
434
|
-
#### Methods
|
|
435
|
-
- `add(token: RGXToken, pos?: RGXUnionInsertionPosition) => this`: Adds a token to the union. The `pos` parameter controls where the token is inserted: `'prefix'` inserts at the beginning, `'suffix'` (default) appends to the end. Returns `this` for chaining.
|
|
436
|
-
- `concat(pos?: RGXUnionInsertionPosition, ...others: RGXTokenCollectionInput[]) => this`: Concatenates additional tokens into the union. The `pos` parameter controls insertion position: `'suffix'` (default) appends to the end, `'prefix'` prepends to the beginning. Returns `this` for chaining.
|
|
437
|
-
- `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`.
|
|
438
|
-
- `toRgx() => RegExp`: Resolves the union by calling `toRgx()` on the internal `RGXTokenCollection`, returning a `RegExp`.
|
|
439
|
-
|
|
440
|
-
### RGXGroupToken extends RGXClassToken
|
|
441
|
-
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.
|
|
442
|
-
|
|
443
|
-
A function `rgxGroup` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
|
|
444
|
-
|
|
445
|
-
#### Static Properties
|
|
446
|
-
- `check(value: unknown): value is RGXGroupToken`: A type guard that checks if the given value is an instance of `RGXGroupToken`.
|
|
447
|
-
- `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.
|
|
448
|
-
|
|
449
|
-
#### Constructor
|
|
450
|
-
```typescript
|
|
451
|
-
constructor(args?: RGXGroupTokenArgs, tokens?: RGXTokenCollectionInput)
|
|
452
|
-
```
|
|
453
|
-
- `args` (`RGXGroupTokenArgs`, optional): An object specifying the group configuration. Defaults to `{}`.
|
|
454
|
-
- `name` (`string | null`, optional): The name of the group for named capture groups. Must be a valid identifier (validated via `assertValidIdentifier`). Defaults to `null`.
|
|
455
|
-
- `capturing` (`boolean`, optional): Whether the group is capturing. Defaults to `true`. Setting this to `false` also clears any `name`.
|
|
456
|
-
- `tokens` (`RGXTokenCollectionInput`, optional): The tokens to be wrapped by the group. Internally stored as an `RGXTokenCollection` in 'concat' mode. Defaults to an empty array.
|
|
457
|
-
|
|
458
|
-
#### Properties
|
|
459
|
-
- `tokens` (`RGXTokenCollection`): The internal collection of tokens managed in 'concat' mode.
|
|
460
|
-
- `name` (`string | null`): The name of the group. Setting this to a non-null value validates it as a valid identifier via `assertValidIdentifier`.
|
|
461
|
-
- `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`.
|
|
462
|
-
- `rgxIsGroup` (`true`): Returns `true` as a constant, indicating this token represents a group.
|
|
463
|
-
- `rgxGroupWrap` (`false`): Returns `false` as a constant, since the group already wraps itself, preventing the resolver from double-wrapping.
|
|
464
|
-
|
|
465
|
-
#### Methods
|
|
466
|
-
- `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.
|
|
467
|
-
|
|
468
|
-
### RGXRepeatToken extends RGXClassToken
|
|
469
|
-
A class representing a repetition quantifier wrapping an RGX token. This allows specifying how many times a token should be matched (e.g., exactly N times, between N and M times, or unlimited). This is typically created via the `repeat()` or `optional()` methods on `RGXClassToken`, but can also be instantiated directly.
|
|
470
|
-
|
|
471
|
-
A function `rgxRepeat` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
|
|
472
|
-
|
|
473
|
-
#### Static Properties
|
|
474
|
-
- `check(value: unknown): value is RGXRepeatToken`: A type guard that checks if the given value is an instance of `RGXRepeatToken`.
|
|
475
|
-
- `assert(value: unknown): asserts value is RGXRepeatToken`: An assertion that checks if the given value is an instance of `RGXRepeatToken`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
476
|
-
|
|
477
|
-
#### Constructor
|
|
478
|
-
```typescript
|
|
479
|
-
constructor(token: RGXToken, min?: number, max?: number | null, lazy?: boolean)
|
|
480
|
-
```
|
|
481
|
-
- `token` (`RGXToken`): The token to repeat. If the token is not already a grouped token, it will be automatically wrapped in a non-capturing `RGXGroupToken`.
|
|
482
|
-
- `min` (`number`, optional): The minimum number of repetitions. Must be >= 0 and <= `max` (when `max` is not `null`). Non-integer values are floored. Defaults to `1`.
|
|
483
|
-
- `max` (`number | null`, optional): The maximum number of repetitions. Must be >= `min` when not `null`. Non-integer values are floored. Pass `null` for unlimited repetitions. Defaults to `min`.
|
|
484
|
-
- `lazy` (`boolean`, optional): Whether the quantifier should be non-greedy (lazy). Defaults to `false`.
|
|
485
|
-
|
|
486
|
-
#### Properties
|
|
487
|
-
- `token` (`RGXGroupedToken`): The token being repeated. Setting this will throw `RGXNotSupportedError` if the value is a convertible token with `rgxIsRepeatable` set to `false`, and will automatically wrap non-grouped tokens in a non-capturing `RGXGroupToken`.
|
|
488
|
-
- `min` (`number`): The minimum number of repetitions. Setting this validates that the value is >= 0 and <= `max` (when `max` is not `null`), and floors non-integer values. Throws `RGXOutOfBoundsError` if validation fails.
|
|
489
|
-
- `max` (`number | null`): The maximum number of repetitions. Setting this validates that the value is >= `min` when not `null`, and floors non-integer values. Pass `null` for unlimited. Throws `RGXOutOfBoundsError` if validation fails.
|
|
490
|
-
- `lazy` (`boolean`): Whether the quantifier is non-greedy (lazy). When `true`, a `?` is appended to the `repeaterSuffix` (except when the suffix is `?` or empty, since those cases don't benefit from a lazy modifier). Defaults to `false`.
|
|
491
|
-
- `repeaterSuffix` (`string`): Returns the regex quantifier suffix based on the current `min`, `max`, and `lazy` values: `*` for `{0,}`, `+` for `{1,}`, `?` for `{0,1}`, `{n}` for exact repetitions, `{n,}` for minimum-only, `{n,m}` for a range, or an empty string for `{1,1}` (exactly once, no quantifier needed). When `lazy` is `true`, a `?` is appended to the suffix (e.g., `*?`, `+?`, `{2,5}?`), except when the suffix is already `?` or empty.
|
|
492
|
-
- `rgxGroupWrap` (`false`): Returns `false` as a constant, since the quantifier suffix binds tightly to the preceding group and does not need additional wrapping.
|
|
493
|
-
|
|
494
|
-
#### Methods
|
|
495
|
-
- `toRgx() => RGXToken`: Resolves the repeat token to a `RegExp` by resolving the inner token and appending the `repeaterSuffix`. Returns `null` (a no-op) when both `min` and `max` are `0`.
|
|
496
|
-
|
|
497
|
-
### RGXLookaroundToken extends RGXClassToken (abstract)
|
|
498
|
-
An abstract base class for lookaround assertion tokens (lookahead and lookbehind). Lookaround assertions match a pattern without consuming characters in the string. Subclasses must implement the `toRgx()`, `negate()`, and `reverse()` methods.
|
|
499
|
-
|
|
500
|
-
#### Static Properties
|
|
501
|
-
- `check(value: unknown): value is RGXLookaroundToken`: A type guard that checks if the given value is an instance of `RGXLookaroundToken`.
|
|
502
|
-
- `assert(value: unknown): asserts value is RGXLookaroundToken`: An assertion that checks if the given value is an instance of `RGXLookaroundToken`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
503
|
-
|
|
504
|
-
#### Constructor
|
|
505
|
-
```typescript
|
|
506
|
-
constructor(tokens?: RGXTokenCollectionInput, positive?: boolean)
|
|
507
|
-
```
|
|
508
|
-
- `tokens` (`RGXTokenCollectionInput`, optional): The tokens to include in the lookaround. Internally stored as an `RGXTokenCollection` in 'concat' mode. Defaults to an empty array.
|
|
509
|
-
- `positive` (`boolean`, optional): Whether the lookaround is positive (matches if the pattern is present) or negative (matches if the pattern is absent). Defaults to `true`.
|
|
510
|
-
|
|
511
|
-
#### Properties
|
|
512
|
-
- `tokens` (`RGXTokenCollection`): The internal collection of tokens managed in 'concat' mode.
|
|
513
|
-
- `positive` (`boolean`): Whether the lookaround is positive. Setting this updates `negative` accordingly.
|
|
514
|
-
- `negative` (`boolean`): Whether the lookaround is negative. Setting this updates `positive` accordingly.
|
|
515
|
-
- `rgxIsGroup` (`true`): Returns `true` as a constant, indicating this token represents a group.
|
|
516
|
-
- `rgxIsRepeatable` (`false`): Returns `false` as a constant, since lookaround assertions cannot be repeated.
|
|
517
|
-
- `rgxGroupWrap` (`false`): Returns `false` as a constant, since the lookaround already wraps itself in a group.
|
|
518
|
-
|
|
519
|
-
#### Abstract Methods
|
|
520
|
-
- `negate() => RGXLookaroundToken`: Returns a new lookaround token of the same type with the opposite positivity, preserving the original tokens.
|
|
521
|
-
- `reverse() => RGXLookaroundToken`: Returns a new lookaround token of the opposite direction (lookahead becomes lookbehind and vice versa), preserving the original tokens and positivity.
|
|
522
|
-
|
|
523
|
-
### RGXLookaheadToken extends RGXLookaroundToken
|
|
524
|
-
A class representing a lookahead assertion. Positive lookaheads (`(?=...)`) match if the pattern is present ahead, while negative lookaheads (`(?!...)`) match if the pattern is absent. This is typically created via the `asLookahead()` method on `RGXClassToken`, but can also be instantiated directly.
|
|
525
|
-
|
|
526
|
-
A function `rgxLookahead` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
|
|
527
|
-
|
|
528
|
-
#### Static Properties
|
|
529
|
-
- `check(value: unknown): value is RGXLookaheadToken`: A type guard that checks if the given value is an instance of `RGXLookaheadToken`.
|
|
530
|
-
- `assert(value: unknown): asserts value is RGXLookaheadToken`: An assertion that checks if the given value is an instance of `RGXLookaheadToken`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
531
|
-
|
|
532
|
-
#### Methods
|
|
533
|
-
- `negate() => RGXLookaheadToken`: Returns a new `RGXLookaheadToken` with the opposite positivity, preserving the original tokens.
|
|
534
|
-
- `reverse() => RGXLookbehindToken`: Returns a new `RGXLookbehindToken` with the same tokens and positivity.
|
|
535
|
-
- `toRgx() => RegExp`: Resolves the lookahead to a `RegExp`. Positive lookaheads produce `(?=...)` and negative lookaheads produce `(?!...)`.
|
|
536
|
-
|
|
537
|
-
### RGXLookbehindToken extends RGXLookaroundToken
|
|
538
|
-
A class representing a lookbehind assertion. Positive lookbehinds (`(?<=...)`) match if the pattern is present behind, while negative lookbehinds (`(?<!...)`) match if the pattern is absent. This is typically created via the `asLookbehind()` method on `RGXClassToken`, but can also be instantiated directly.
|
|
539
|
-
|
|
540
|
-
A function `rgxLookbehind` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
|
|
541
|
-
|
|
542
|
-
#### Static Properties
|
|
543
|
-
- `check(value: unknown): value is RGXLookbehindToken`: A type guard that checks if the given value is an instance of `RGXLookbehindToken`.
|
|
544
|
-
- `assert(value: unknown): asserts value is RGXLookbehindToken`: An assertion that checks if the given value is an instance of `RGXLookbehindToken`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
545
|
-
|
|
546
|
-
#### Methods
|
|
547
|
-
- `negate() => RGXLookbehindToken`: Returns a new `RGXLookbehindToken` with the opposite positivity, preserving the original tokens.
|
|
548
|
-
- `reverse() => RGXLookaheadToken`: Returns a new `RGXLookaheadToken` with the same tokens and positivity.
|
|
549
|
-
- `toRgx() => RegExp`: Resolves the lookbehind to a `RegExp`. Positive lookbehinds produce `(?<=...)` and negative lookbehinds produce `(?<!...)`.
|
|
550
|
-
|
|
551
|
-
### RGXSubpatternToken extends RGXClassToken
|
|
552
|
-
A class representing a backreference to a previously captured group, either by name or by group number. Named backreferences produce `\k<name>` and numbered backreferences produce `\N` (where N is the group number). This is useful for matching the same text that was captured by a previous group.
|
|
553
|
-
|
|
554
|
-
A function `rgxSubpattern` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
|
|
555
|
-
|
|
556
|
-
#### Static Properties
|
|
557
|
-
- `check(value: unknown): value is RGXSubpatternToken`: A type guard that checks if the given value is an instance of `RGXSubpatternToken`.
|
|
558
|
-
- `assert(value: unknown): asserts value is RGXSubpatternToken`: An assertion that checks if the given value is an instance of `RGXSubpatternToken`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
559
|
-
|
|
560
|
-
#### Constructor
|
|
561
|
-
```typescript
|
|
562
|
-
constructor(pattern: string | number)
|
|
563
|
-
```
|
|
564
|
-
- `pattern` (`string | number`): The backreference pattern. If a string, it must be a valid identifier (validated via `assertValidIdentifier`) and produces a named backreference (`\k<name>`). If a number, it must be a positive integer (>= 1, as groups are 1-indexed) and produces a numbered backreference (`\N`). Non-integer numbers are floored.
|
|
565
|
-
|
|
566
|
-
#### Properties
|
|
567
|
-
- `pattern` (`string | number`): The backreference pattern. Setting this validates the value: strings must be valid identifiers, numbers must be positive integers (>= 1). Non-integer numbers are floored.
|
|
568
|
-
|
|
569
|
-
#### Methods
|
|
570
|
-
- `toRgx() => RegExp`: Resolves the backreference to a `RegExp`. Named patterns produce `/\k<name>/` and numbered patterns produce `/\N/`.
|
|
571
|
-
- `clone(depth: CloneDepth = "max") => RGXSubpatternToken`: Creates a clone of this token. When `depth` is `0`, returns `this`; otherwise, returns a new `RGXSubpatternToken` with the same pattern.
|
|
572
|
-
|
|
573
|
-
### RGXClassWrapperToken extends RGXClassToken
|
|
574
|
-
A class that wraps any `RGXToken` as an `RGXClassToken`, giving you access to the extended API class tokens provide. It delegates `rgxIsGroup` and `rgxIsRepeatable` to the wrapped token where possible.
|
|
575
|
-
|
|
576
|
-
A function `rgxClassWrapper` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
|
|
577
|
-
|
|
578
|
-
#### Static Properties
|
|
579
|
-
- `check(value: unknown): value is RGXClassWrapperToken`: A type guard that checks if the given value is an instance of `RGXClassWrapperToken`.
|
|
580
|
-
- `assert(value: unknown): asserts value is RGXClassWrapperToken`: An assertion that checks if the given value is an instance of `RGXClassWrapperToken`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
581
|
-
|
|
582
|
-
#### Constructor
|
|
583
|
-
```typescript
|
|
584
|
-
constructor(token: RGXToken)
|
|
585
|
-
```
|
|
586
|
-
- `token` (`RGXToken`): The token to wrap.
|
|
587
|
-
|
|
588
|
-
#### Properties
|
|
589
|
-
- `token` (`RGXToken`): The wrapped token.
|
|
590
|
-
- `rgxIsGroup` (`boolean`): Delegates to the wrapped token's group status via `isRGXGroupedToken`. Returns `true` if the wrapped token is a grouped token, otherwise `false`.
|
|
591
|
-
- `rgxIsRepeatable` (`boolean`): If the wrapped token is an `RGXConvertibleToken`, delegates to its `rgxIsRepeatable` property (defaulting to `true` if not present). Otherwise, returns `true`.
|
|
592
|
-
|
|
593
|
-
#### Methods
|
|
594
|
-
- `unwrap() => RGXToken`: Returns the original wrapped token.
|
|
595
|
-
- `toRgx() => RGXToken`: Returns the original wrapped token (alias for `unwrap()`).
|
|
596
|
-
|
|
597
|
-
### ExtRegExp extends RegExp
|
|
598
|
-
A subclass of `RegExp` that supports custom flag transformers in addition to the standard vanilla regex flags (g, i, m, s, u, y). When constructed, custom flags are extracted, their corresponding transformers are applied to the pattern and vanilla flags, and the resulting transformed `RegExp` is created. The `flags` getter returns both the vanilla flags and any custom flags.
|
|
599
|
-
|
|
600
|
-
A function `extRegExp` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
|
|
601
|
-
|
|
602
|
-
#### Constructor
|
|
603
|
-
```typescript
|
|
604
|
-
constructor(pattern: string | RegExp, flags?: string)
|
|
605
|
-
```
|
|
606
|
-
- `pattern` (`string | RegExp`): The regex pattern. If a `RegExp` is provided, its `source` is used and its existing `flags` are tracked as already-applied flags to avoid re-applying transformers.
|
|
607
|
-
- `flags` (`string`, optional): The flags string, which may include both vanilla regex flags and custom registered flag keys. Validated via `assertValidRegexFlags`. Defaults to `''`.
|
|
608
|
-
|
|
609
|
-
#### Properties
|
|
610
|
-
- `flags` (`string`): Returns the combination of the vanilla flags (from the underlying `RegExp`) and any custom flags that were applied during construction.
|
|
611
|
-
|
|
612
|
-
#### Static Properties
|
|
613
|
-
- `[Symbol.species]` (`RegExpConstructor`): Returns `ExtRegExp`, ensuring that derived `RegExp` methods (like those returning new regex instances) produce `ExtRegExp` instances rather than plain `RegExp`.
|
|
614
|
-
|
|
615
|
-
### RGXPart\<R, T=string\>
|
|
616
|
-
A class that wraps an `RGXToken` with optional callbacks for use within an `RGXWalker`. It implements `RGXConvertibleToken`, delegating `rgxIsGroup` and `rgxIsRepeatable` to the wrapped token. Unlike plain tokens, Parts can control walker behavior via `beforeCapture` (returning an `RGXPartControl` value) and react to captures via `afterCapture`. Parts are purely definitions and do not store capture state — all captures are stored on the walker as `RGXCapture` objects.
|
|
617
|
-
|
|
618
|
-
A function `rgxPart` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
|
|
619
|
-
|
|
620
|
-
#### Static Properties
|
|
621
|
-
- `check(value: unknown): value is RGXPart`: A type guard that checks if the given value is an instance of `RGXPart`.
|
|
622
|
-
- `assert(value: unknown): asserts value is RGXPart`: An assertion that checks if the given value is an instance of `RGXPart`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
623
|
-
|
|
624
|
-
#### Constructor
|
|
625
|
-
```typescript
|
|
626
|
-
constructor(token: RGXToken, options?: Partial<RGXPartOptions<R, T>>)
|
|
627
|
-
```
|
|
628
|
-
- `token` (`RGXToken`): The token to wrap.
|
|
629
|
-
- `options` (`Partial<RGXPartOptions<R, T>>`, optional): Configuration options. Defaults to `{}`.
|
|
630
|
-
- `id` (`string`, optional): An optional identifier for this part. Defaults to `null`, but must be a string if provided.
|
|
631
|
-
- `rawTransform` (`(captured: string) => string`, optional): A function that transforms the raw captured string before it is stored as `raw` on the capture result and before `transform` is applied. Defaults to an identity function.
|
|
632
|
-
- `transform` (`(captured: string) => T`, optional): A function that transforms the captured string into the desired type `T`. Defaults to an identity function that casts the string to `T`.
|
|
633
|
-
- `beforeCapture` (`((part: RGXPart<R, T>, walker: RGXWalker<R>) => RGXPartControl) | null`, optional): A callback invoked before capturing this part during walking. Returns an `RGXPartControl` value to control walker behavior: `"skip"` to skip this token without capturing, `"silent"` to capture but not record in `captures`, `"stop"` to halt immediately without capturing or advancing, or `void`/`undefined` to proceed normally. Defaults to `null`.
|
|
634
|
-
- `afterCapture` (`((capture: RGXCapture<T>, part: RGXPart<R, T>, walker: RGXWalker<R>) => void) | null`, optional): A callback invoked after capturing this part during walking. Receives the typed `RGXCapture<T>` result. Can call `walker.stop()` to halt walking after this capture. Defaults to `null`.
|
|
635
|
-
- `validate` (`((capture: RGXCapture<T>, walker: RGXWalker<R>) => boolean | string) | null`, optional): A callback invoked during validation after capturing and transforming, but before `afterCapture`. Returns `true` if validation passes, `false` to fail with a generic error, or a string to fail with that string as the error message. Defaults to `null`.
|
|
636
|
-
|
|
637
|
-
#### Properties
|
|
638
|
-
- `id` (`string | null`): An optional identifier for this part.
|
|
639
|
-
- `token` (`RGXToken`): The wrapped token.
|
|
640
|
-
- `rawTransform` (`(captured: string) => string`, readonly): The raw transform function applied to the matched string before it is stored as `raw` and before `transform` is called.
|
|
641
|
-
- `transform` (`(captured: string) => T`, readonly): The transform function used to convert captured strings to values of type `T`.
|
|
642
|
-
- `beforeCapture` (`((part: RGXPart<R, T>, walker: RGXWalker<R>) => RGXPartControl) | null`, readonly): The before-capture callback, or `null`.
|
|
643
|
-
- `afterCapture` (`((capture: RGXCapture<T>, part: RGXPart<R, T>, walker: RGXWalker<R>) => void) | null`, readonly): The after-capture callback, or `null`.
|
|
644
|
-
- `rgxIsGroup` (`boolean`): Delegates to the wrapped token's group status via `isRGXGroupedToken`.
|
|
645
|
-
- `rgxIsRepeatable` (`boolean`): If the wrapped token is an `RGXConvertibleToken`, delegates to its `rgxIsRepeatable` property (defaulting to `true` if not present). Otherwise, returns `true`.
|
|
646
|
-
|
|
647
|
-
#### Methods
|
|
648
|
-
- `toRgx() => RGXToken`: Returns the wrapped token.
|
|
649
|
-
- `clone(depth: CloneDepth = "max") => RGXPart`: Creates a clone of this part. When `depth` is `0`, returns `this`; otherwise, returns a new `RGXPart` with a cloned token and the same `rawTransform`, `transform`, `beforeCapture`, and `afterCapture` references.
|
|
650
|
-
- `hasId() => this is RGXPart<R, T> & { id: string }`: A type guard that checks if this part has a non-null `id`. If `true`, narrows the type to indicate that `id` is a string.
|
|
651
|
-
- `validate(capture: RGXCapture<T>, walker: RGXWalker<R>) => void`: A method that calls the inner passed validation logic for this part, if any. If it returns `false`, a generic `RGXPartValidationFailedError` is thrown. If it returns a string, an `RGXPartValidationFailedError` is thrown with that string as the message. If it returns `true`, validation passed. This is called internally by the walker after capturing and transforming a part, before invoking `afterCapture`.
|
|
652
|
-
|
|
653
|
-
### RGXWalker\<R\>
|
|
654
|
-
A class that walks through a sequence of RGX tokens, matching each token against a source string at the current position. It implements `RGXConvertibleToken`, delegating to its internal `RGXTokenCollection`. The walker maintains a source position and a token position, advancing through both as tokens are matched. When an `RGXPart` is encountered, its `beforeCapture` callback can control behavior via return values (`RGXPartControl`), and its `afterCapture` callback is invoked with the typed capture result. All captures are stored as structured `RGXCapture` objects on the walker, and captures with ids are stored in the `namedCaptures` property also. The generic type `R` represents a user-defined "reduced" value that can accumulate state during walking (e.g., via `RGXPart` callbacks).
|
|
655
|
-
|
|
656
|
-
A function `rgxWalker` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
|
|
657
|
-
|
|
658
|
-
#### Static Properties
|
|
659
|
-
- `check(value: unknown): value is RGXWalker`: A type guard that checks if the given value is an instance of `RGXWalker`.
|
|
660
|
-
- `assert(value: unknown): asserts value is RGXWalker`: An assertion that checks if the given value is an instance of `RGXWalker`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
661
|
-
|
|
662
|
-
#### Constructor
|
|
663
|
-
```typescript
|
|
664
|
-
constructor(source: string, tokens: RGXTokenCollectionInput, options?: RGXWalkerOptions<R>)
|
|
665
|
-
```
|
|
666
|
-
- `source` (`string`): The string to walk through, matching tokens against.
|
|
667
|
-
- `tokens` (`RGXTokenCollectionInput`): The tokens to match sequentially. Internally stored as an `RGXTokenCollection` in 'concat' mode.
|
|
668
|
-
- `options` (`RGXWalkerOptions<R>`, optional): Configuration options. Defaults to `{}`.
|
|
669
|
-
- `startingSourcePosition` (`number`, optional): The starting index in the source string. Defaults to `0`.
|
|
670
|
-
- `reduced` (`R`, optional): The initial value for the `reduced` accumulator. Defaults to `null`.
|
|
671
|
-
- `infinite` (`boolean`, optional): When `true`, the walker stays at the last token indefinitely rather than stopping when the token collection is exhausted, continuing to match the last token until the source is consumed. Defaults to `false`.
|
|
672
|
-
- `looping` (`boolean`, optional): When `true`, the walker loops back to token position `0` when the token collection is exhausted, continuing to match from the start until the source is consumed. Defaults to `false`.
|
|
673
|
-
|
|
674
|
-
#### Properties
|
|
675
|
-
- `source` (`string`): The source string being walked (readonly).
|
|
676
|
-
- `sourcePosition` (`number`): The current index in the source string. Range is `[0, source.length]` inclusive, where `source.length` represents "fully consumed". Setting this validates that the value is >= 0 and <= `source.length`, throwing `RGXOutOfBoundsError` if not.
|
|
677
|
-
- `tokens` (`RGXTokenCollection`): The internal collection of tokens in 'concat' mode (readonly).
|
|
678
|
-
- `tokenPosition` (`number`): The current index in the token collection. Setting this validates that the value is >= 0 and <= `tokens.length`, throwing `RGXOutOfBoundsError` if not.
|
|
679
|
-
- `reduced` (`R`): A user-defined accumulator value, typically updated by `RGXPart` callbacks during walking.
|
|
680
|
-
- `captures` (`RGXCapture[]`): An array of structured capture results recorded during walking. Each entry has a `raw` string (the `rawTransform` result for Parts, or the matched string for plain tokens), a `value` (the `transform` result for Parts, or the matched string for plain tokens), `start` and `end` indices in the source string, an `ownerId` that is the `id` of the Part that produced it (or `null` for captures from plain tokens or parts without ids), and a `branch` index indicating which alternative of a multi-branch Part token was matched (or `0` if there is only one branch or the token is not a Part).
|
|
681
|
-
- `namedCaptures` (`Record<string, RGXCapture[]>`): An object mapping capture IDs to their corresponding `RGXCapture` results. Only Parts with non-null IDs are included. The captures occur in the same order as they appear in the `captures` array.
|
|
682
|
-
- `infinite` (`boolean`): Whether the walker is in infinite mode — stays at the last token when the token collection is exhausted until the source is consumed.
|
|
683
|
-
- `looping` (`boolean`): Whether the walker is in looping mode — loops back to token position `0` when the token collection is exhausted until the source is consumed.
|
|
684
|
-
- `stopped` (`boolean`, readonly): Whether the walker has been stopped, either by a Part's `beforeCapture` returning `"stop"` or by calling `stop()` in an `afterCapture` callback.
|
|
685
|
-
|
|
686
|
-
#### Methods
|
|
687
|
-
- `stop() => this`: Sets `stopped` to `true`, causing any active `stepToToken`, `stepToPart`, or `walk` loop to halt after the current iteration. Typically called from an `afterCapture` callback to stop walking after the current capture.
|
|
688
|
-
- `atTokenEnd() => boolean`: Returns `true` if the token position is at or past the end of the token collection.
|
|
689
|
-
- `hasNextToken(predicate?: (token: RGXToken) => boolean) => boolean`: Returns `true` if there is a current token and it satisfies the optional predicate (defaults to `() => true`).
|
|
690
|
-
- `atSourceEnd() => boolean`: Returns `true` if the source has been fully consumed (`sourcePosition >= source.length`).
|
|
691
|
-
- `hasNextSource(predicate?: (rest: string) => boolean) => boolean`: Returns `true` if the source is not fully consumed and the remaining source satisfies the optional predicate (defaults to `() => true`).
|
|
692
|
-
- `lastCapture() => RGXCapture | null`: Returns the last entry in `captures`, or `null` if empty.
|
|
693
|
-
- `currentToken() => RGXToken | null`: Returns the token at the current token position, or `null` if at the end.
|
|
694
|
-
- `remainingSource() => string | null`: Returns the remaining source string from the current position onward, or `null` if the source is fully consumed.
|
|
695
|
-
- `capture(token: RGXToken, includeMatch?: false) => string`: Resolves the token to a regex, asserts that it matches at the current source position (throwing `RGXRegexNotMatchedAtPositionError` if not), and advances the source position by the match length. Returns the matched string.
|
|
696
|
-
- `capture(token: RGXToken, includeMatch: true) => RegExpExecArray`: Same as above, but returns the full `RegExpExecArray` from the match instead of just the matched string.
|
|
697
|
-
- `step() => RGXCapture | null`: Steps through the next token in the collection. If the token is an `RGXPart`, calls `beforeCapture` first — if it returns `"stop"`, sets `stopped` and returns `null` without advancing; if `"skip"`, advances the token position and returns `null` without capturing; if `"silent"`, captures but does not add to `captures` or `namedCaptures`. After capturing, validates. After validating, calls `afterCapture` if present. Returns the `RGXCapture` result, or `null` if there are no more tokens (or no more source in `infinite`/`looping` mode), the step was skipped, or the walker was stopped.
|
|
698
|
-
- `stepToToken(predicate: (token: RGXToken) => boolean) => this`: Steps through tokens until the predicate returns `true` for the current token or the walker is stopped. The matching token is not consumed.
|
|
699
|
-
- `stepToPart(predicate?: (part: RGXPart<R>) => boolean) => this`: Steps through tokens until the next `RGXPart` satisfying the predicate is reached. If already at a Part, steps once first to move past it. The matching Part is not consumed.
|
|
700
|
-
- `walk() => this`: Steps through all remaining tokens until the end of the token collection (or until the source is consumed in `infinite`/`looping` mode) or the walker is stopped.
|
|
701
|
-
- `toRgx() => RGXToken`: Returns the internal `RGXTokenCollection`, allowing the walker to be used as a convertible token.
|
|
702
|
-
- `clone(depth: CloneDepth = "max") => RGXWalker`: Creates a clone of the walker. When `depth` is `0`, returns `this`; otherwise, creates a new `RGXWalker` with cloned tokens, source position, reduced value, captures, stopped state, and the `infinite`/`looping` flags.
|
|
703
|
-
|
|
704
|
-
## Functions
|
|
705
|
-
The following functions are exported by the library:
|
|
706
|
-
|
|
707
|
-
### isRGXNoOpToken
|
|
708
|
-
```typescript
|
|
709
|
-
function isRGXNoOpToken(value: unknown): value is RGXNoOpToken
|
|
710
|
-
```
|
|
711
|
-
|
|
712
|
-
Checks if the given value is a no-op token (`null` or `undefined`).
|
|
713
|
-
|
|
714
|
-
#### Parameters
|
|
715
|
-
- `value` (`unknown`): The value to check.
|
|
716
|
-
|
|
717
|
-
#### Returns
|
|
718
|
-
- `boolean`: `true` if the value is a no-op token, otherwise `false`.
|
|
719
|
-
|
|
720
|
-
### assertRGXNoOpToken
|
|
721
|
-
```typescript
|
|
722
|
-
function assertRGXNoOpToken(value: unknown): asserts value is RGXNoOpToken
|
|
723
|
-
```
|
|
724
|
-
|
|
725
|
-
Asserts that the given value is a no-op token (`null` or `undefined`). If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
726
|
-
|
|
727
|
-
#### Parameters
|
|
728
|
-
- `value` (`unknown`): The value to assert.
|
|
729
|
-
|
|
730
|
-
#### Returns
|
|
731
|
-
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
732
|
-
|
|
733
|
-
### isRGXLiteralToken
|
|
734
|
-
```typescript
|
|
735
|
-
function isRGXLiteralToken(value: unknown): value is RGXLiteralToken
|
|
736
|
-
```
|
|
737
|
-
|
|
738
|
-
Checks if the given value is a literal token (a `RegExp` object).
|
|
739
|
-
|
|
740
|
-
#### Parameters
|
|
741
|
-
- `value` (`unknown`): The value to check.
|
|
742
|
-
|
|
743
|
-
#### Returns
|
|
744
|
-
- `boolean`: `true` if the value is a literal token, otherwise `false`.
|
|
745
|
-
|
|
746
|
-
### assertRGXLiteralToken
|
|
747
|
-
```typescript
|
|
748
|
-
function assertRGXLiteralToken(value: unknown): asserts value is RGXLiteralToken
|
|
749
|
-
```
|
|
750
|
-
|
|
751
|
-
Asserts that the given value is a literal token (a `RegExp` object). If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
752
|
-
|
|
753
|
-
#### Parameters
|
|
754
|
-
- `value` (`unknown`): The value to assert.
|
|
755
|
-
|
|
756
|
-
#### Returns
|
|
757
|
-
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
758
|
-
|
|
759
|
-
### isRGXNativeToken
|
|
760
|
-
```typescript
|
|
761
|
-
function isRGXNativeToken(value: unknown): value is RGXNativeToken
|
|
762
|
-
```
|
|
763
|
-
|
|
764
|
-
Checks if the given value is a native token (string, number, boolean, or no-op).
|
|
765
|
-
|
|
766
|
-
#### Parameters
|
|
767
|
-
- `value` (`unknown`): The value to check.
|
|
768
|
-
|
|
769
|
-
#### Returns
|
|
770
|
-
- `boolean`: `true` if the value is a native token, otherwise `false`.
|
|
771
|
-
|
|
772
|
-
### assertRGXNativeToken
|
|
773
|
-
```typescript
|
|
774
|
-
function assertRGXNativeToken(value: unknown): asserts value is RGXNativeToken
|
|
775
|
-
```
|
|
776
|
-
|
|
777
|
-
Asserts that the given value is a native token (string, number, boolean, or no-op). If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
778
|
-
|
|
779
|
-
#### Parameters
|
|
780
|
-
- `value` (`unknown`): The value to assert.
|
|
781
|
-
|
|
782
|
-
#### Returns
|
|
783
|
-
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
784
|
-
|
|
785
|
-
### isRGXConvertibleToken
|
|
786
|
-
```typescript
|
|
787
|
-
function isRGXConvertibleToken(value: unknown, returnCheck?: boolean): value is RGXConvertibleToken
|
|
788
|
-
```
|
|
789
|
-
|
|
790
|
-
Checks if the given value is a convertible token (an object with a `toRgx` method). If the `rgxGroupWrap`, `rgxIsRepeatable`, or `rgxIsGroup` properties are present, they must be booleans; otherwise, the check fails. If the `rgxAcceptInsertion` property is present, it must be a callable that returns a `string` or `boolean`; otherwise, the check fails. 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). When `returnCheck` is `false`, only checks for the presence and callability of function properties instead of also checking their return values.
|
|
791
|
-
|
|
792
|
-
#### Parameters
|
|
793
|
-
- `value` (`unknown`): The value to check.
|
|
794
|
-
- `returnCheck` (`boolean`, optional): Whether to validate the return value of the `toRgx` method. Defaults to `true`. When `false`, only checks that `toRgx` exists and is callable. **Note**: Setting this to `false` makes the type guard assertion strictly unsafe, as it doesn't verify that the methods actually return valid values. However, depending on the type of the value you're checking, you might not need that safety (e.g., when checking values that you know are valid based on other context).
|
|
795
|
-
|
|
796
|
-
#### Returns
|
|
797
|
-
- `boolean`: `true` if the value is a convertible token, otherwise `false`.
|
|
798
|
-
|
|
799
|
-
### assertRGXConvertibleToken
|
|
800
|
-
```typescript
|
|
801
|
-
function assertRGXConvertibleToken(value: unknown, returnCheck?: boolean): asserts value is RGXConvertibleToken
|
|
802
|
-
```
|
|
803
|
-
Asserts that the given value is a convertible token (an object with a `toRgx` method). If the `rgxGroupWrap`, `rgxIsRepeatable`, or `rgxIsGroup` properties are present, they must be booleans; otherwise, the assertion fails. If the `rgxAcceptInsertion` property is present, it must be a callable that returns a `string` or `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.
|
|
804
|
-
|
|
805
|
-
#### Parameters
|
|
806
|
-
- `value` (`unknown`): The value to assert.
|
|
807
|
-
- `returnCheck` (`boolean`, optional): Whether to validate the return value of the `toRgx` method. Defaults to `true`. When `false`, only checks that `toRgx` exists and is callable. **Note**: Setting this to `false` makes the type guard assertion strictly unsafe, as it doesn't verify that the `toRgx` method actually returns a valid `RGXToken`. However, depending on the type of the value you're asserting, you might not need that safety (e.g., when asserting values that you know are valid based on other context).
|
|
808
|
-
|
|
809
|
-
#### Returns
|
|
810
|
-
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
811
|
-
|
|
812
|
-
### isRGXArrayToken
|
|
813
|
-
```typescript
|
|
814
|
-
function isRGXArrayToken(value: unknown, contentCheck?: boolean): value is RGXToken[]
|
|
815
|
-
```
|
|
816
|
-
Checks if the given value is an array of RGX tokens. When `contentCheck` is `true` (the default), validates that the value is an array and that every element is a valid RGX token (of any type, including nested arrays). When `contentCheck` is `false`, only checks that the value is an array without validating the contents.
|
|
817
|
-
|
|
818
|
-
#### Parameters
|
|
819
|
-
- `value` (`unknown`): The value to check.
|
|
820
|
-
- `contentCheck` (`boolean`, optional): Whether to validate that every element is a valid RGX token. Defaults to `true`. When `false`, only checks that the value is an array. **Note**: Setting this to `false` makes the type guard assertion strictly unsafe, as it doesn't verify that the array elements are actually valid `RGXToken` values. However, depending on the context, you might not need that safety (e.g., when checking arrays that you know are valid based on other validation).
|
|
821
|
-
|
|
822
|
-
#### Returns
|
|
823
|
-
- `boolean`: `true` if the value is an array of RGX tokens (or just an array when `contentCheck` is `false`), otherwise `false`.
|
|
824
|
-
|
|
825
|
-
### assertRGXArrayToken
|
|
826
|
-
```typescript
|
|
827
|
-
function assertRGXArrayToken(value: unknown, contentCheck?: boolean): asserts value is RGXToken[]
|
|
828
|
-
```
|
|
829
|
-
Asserts that the given value is an array of RGX tokens. When `contentCheck` is `true` (the default), validates that the value is an array and that every element is a valid RGX token (of any type, including nested arrays). When `contentCheck` is `false`, only checks that the value is an array without validating the contents. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
830
|
-
|
|
831
|
-
#### Parameters
|
|
832
|
-
- `value` (`unknown`): The value to assert.
|
|
833
|
-
- `contentCheck` (`boolean`, optional): Whether to validate that every element is a valid RGX token. Defaults to `true`. When `false`, only checks that the value is an array. **Note**: Setting this to `false` makes the type guard assertion strictly unsafe, as it doesn't verify that the array elements are actually valid `RGXToken` values. However, depending on the context, you might not need that safety (e.g., when asserting arrays that you know are valid based on other validation).
|
|
834
|
-
|
|
835
|
-
#### Returns
|
|
836
|
-
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
837
|
-
|
|
838
|
-
### rgxTokenType
|
|
839
|
-
```typescript
|
|
840
|
-
function rgxTokenType(value: unknown, recognizeClass?: boolean): RGXTokenType
|
|
841
|
-
```
|
|
842
|
-
|
|
843
|
-
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.
|
|
844
|
-
|
|
845
|
-
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.
|
|
846
|
-
|
|
847
|
-
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.
|
|
848
|
-
|
|
849
|
-
```typescript
|
|
850
|
-
const token: RGXToken = ...;
|
|
851
|
-
const type = rgxTokenType(token);
|
|
852
|
-
|
|
853
|
-
if (type === 'native') {
|
|
854
|
-
const narrowedToken1 = token as RGXTokenFromType<typeof type>; // narrowedToken is RGXNativeToken
|
|
855
|
-
const narrowedToken2 = rgxTokenFromType(type, token); // same as above
|
|
856
|
-
}
|
|
857
|
-
```
|
|
858
|
-
|
|
859
|
-
#### Parameters
|
|
860
|
-
- `value` (`unknown`): The value to check.
|
|
861
|
-
- `recognizeClass` (`boolean`, optional): Whether to recognize `RGXClassToken` instances as `'class'` instead of `'convertible'`. Defaults to `true`.
|
|
862
|
-
|
|
863
|
-
#### Returns
|
|
864
|
-
- `RGXTokenType`: The type of the RGX token.
|
|
865
|
-
|
|
866
|
-
### rgxTokenTypeFlat
|
|
867
|
-
```typescript
|
|
868
|
-
function rgxTokenTypeFlat(value: unknown, recognizeClass?: boolean): RGXTokenTypeFlat
|
|
869
|
-
```
|
|
870
|
-
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.
|
|
871
|
-
|
|
872
|
-
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.
|
|
873
|
-
|
|
874
|
-
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.
|
|
875
|
-
|
|
876
|
-
```typescript
|
|
877
|
-
const token: RGXToken = ...;
|
|
878
|
-
const type = rgxTokenTypeFlat(token);
|
|
879
|
-
if (type === 'array') {
|
|
880
|
-
const narrowedToken1 = token as RGXTokenFromType<typeof type>; // narrowedToken is RGXToken[]
|
|
881
|
-
const narrowedToken2 = rgxTokenFromType(type, token); // same as above
|
|
882
|
-
}
|
|
883
|
-
```
|
|
884
|
-
|
|
885
|
-
#### Parameters
|
|
886
|
-
- `value` (`unknown`): The value to check.
|
|
887
|
-
- `recognizeClass` (`boolean`, optional): Whether to recognize `RGXClassToken` instances as `'class'` instead of `'convertible'`. Defaults to `true`.
|
|
888
|
-
|
|
889
|
-
#### Returns
|
|
890
|
-
- `RGXTokenTypeFlat`: The flat type of the RGX token.
|
|
891
|
-
|
|
892
|
-
### rgxTokenFromType
|
|
893
|
-
```typescript
|
|
894
|
-
function rgxTokenFromType<T extends RGXTokenTypeGuardInput>(type: T, value: RGXToken): RGXTokenFromType<T>
|
|
895
|
-
```
|
|
896
|
-
|
|
897
|
-
Does nothing at runtime, but performs a type assertion to the correct subset of `RGXToken` based on the provided `RGXTokenType`.
|
|
898
|
-
|
|
899
|
-
#### Parameters
|
|
900
|
-
- `type` (`T`): The RGX token type to assert to.
|
|
901
|
-
- `value` (`RGXToken`): The RGX token to assert.
|
|
902
|
-
|
|
903
|
-
#### Returns
|
|
904
|
-
- `RGXTokenFromType<T>`: The input value, but with its type asserted to the corresponding token type based on the provided `RGXTokenType`.
|
|
905
|
-
|
|
906
|
-
### rgxTokenTypeToFlat
|
|
907
|
-
```typescript
|
|
908
|
-
function rgxTokenTypeToFlat(type: RGXTokenType): RGXTokenTypeFlat
|
|
909
|
-
```
|
|
910
|
-
|
|
911
|
-
Converts an `RGXTokenType` to its flat equivalent `RGXTokenTypeFlat`. If the type is an array, it returns `'array'`; otherwise, it returns the type as-is.
|
|
912
|
-
|
|
913
|
-
#### Parameters
|
|
914
|
-
- `type` (`RGXTokenType`): The RGX token type to convert.
|
|
915
|
-
|
|
916
|
-
#### Returns
|
|
917
|
-
- `RGXTokenTypeFlat`: The flat equivalent of the provided token type.
|
|
918
|
-
|
|
919
|
-
### rgxTokenTypeGuardInputToFlat
|
|
920
|
-
```typescript
|
|
921
|
-
function rgxTokenTypeGuardInputToFlat(type: RGXTokenTypeGuardInput): RGXTokenTypeFlat | null
|
|
922
|
-
```
|
|
923
|
-
|
|
924
|
-
Converts an `RGXTokenTypeGuardInput` to its flat equivalent. If the type is `null`, it returns `null`; if it is an array, it returns `'array'`; if it is a RegEx constructor, it returns `'literal`; if it is the `RGXTokenCollection` constructor, it returns `'convertible'`; if it is an `RGXClassTokenConstructor` (a constructor for an `RGXClassToken` subclass), it returns `'class'` (making it slightly lossy in that case); otherwise, it returns the type as-is.
|
|
925
|
-
|
|
926
|
-
#### Parameters
|
|
927
|
-
- `type` (`RGXTokenTypeGuardInput`): The type guard input to convert.
|
|
928
|
-
|
|
929
|
-
#### Returns
|
|
930
|
-
- `RGXTokenTypeFlat | null`: The flat equivalent of the provided type guard input, or `null` if the input is `null`.
|
|
931
|
-
|
|
932
|
-
### isRGXToken
|
|
933
|
-
```typescript
|
|
934
|
-
function isRGXToken<T extends RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): value is RGXTokenFromType<T>
|
|
935
|
-
```
|
|
4
|
+
All public components are exported at the main module entry point.
|
|
936
5
|
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
When `type` is a constructor, it performs an `instanceof` check against that specific constructor, allowing you to narrow to a specific class token subclass rather than all class tokens. In this case, `RGXTokenFromType` resolves to `InstanceType<T>`, giving you the specific subclass type.
|
|
940
|
-
|
|
941
|
-
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.
|
|
942
|
-
|
|
943
|
-
When `type` is `"repeatable"`, it passes for any token that is not convertible, then checks if `rgxIsRepeatable` is `true | undefined` for convertible tokens.
|
|
944
|
-
|
|
945
|
-
#### Parameters
|
|
946
|
-
- `value` (`unknown`): The value to check.
|
|
947
|
-
- `type` (`T`, optional): The token type to check against. Can be a token type string, `null` (checks against all token types), an `RGXClassTokenConstructor` (checks via `instanceof`), or an array of these. Defaults to `null`.
|
|
948
|
-
- `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`.
|
|
949
|
-
|
|
950
|
-
#### Returns
|
|
951
|
-
- `boolean`: `true` if the value is a valid RGX token matching the specified type, otherwise `false`.
|
|
952
|
-
|
|
953
|
-
### assertRGXToken
|
|
954
|
-
```typescript
|
|
955
|
-
function assertRGXToken<T extends RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): asserts value is RGXTokenFromType<T>
|
|
956
|
-
```
|
|
957
|
-
|
|
958
|
-
Asserts that the given value is a valid RGX token, optionally narrowed to a specific token type (including `'class'` for `RGXClassToken` instances, or a specific `RGXClassTokenConstructor` for `instanceof` checks). Uses the same logic as `isRGXToken`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
959
|
-
|
|
960
|
-
#### Parameters
|
|
961
|
-
- `value` (`unknown`): The value to assert.
|
|
962
|
-
- `type` (`T`, optional): The token type to assert against. Can be a token type string, `null` (checks against all token types), an `RGXClassTokenConstructor` (checks via `instanceof`), or an array of these. Defaults to `null`.
|
|
963
|
-
- `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`.
|
|
964
|
-
|
|
965
|
-
#### Returns
|
|
966
|
-
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
967
|
-
|
|
968
|
-
### isRGXGroupedToken
|
|
969
|
-
```typescript
|
|
970
|
-
function isRGXGroupedToken(value: unknown, contentCheck?: boolean): value is RGXGroupedToken
|
|
971
|
-
```
|
|
972
|
-
|
|
973
|
-
Checks if the given value is a grouped token — a token that is implicitly or explicitly a group. Arrays and literal tokens (`RegExp`) are implicitly groups. Convertible tokens (including class tokens) are groups if they have `rgxIsGroup` set to `true`, or if they have `rgxGroupWrap` set to `true` and their `toRgx()` method returns a grouped token.
|
|
974
|
-
|
|
975
|
-
#### Parameters
|
|
976
|
-
- `value` (`unknown`): The value to check.
|
|
977
|
-
- `contentCheck` (`boolean`, optional): Whether to validate the contents of array tokens and the return values of convertible tokens. Defaults to `true`. When `false`, arrays are accepted without checking their elements, and convertible tokens with `rgxGroupWrap` set to `true` are accepted without checking their `toRgx()` return value. This has no effect on the `rgxIsGroup` check, which always accepts the token as grouped regardless of `contentCheck`.
|
|
978
|
-
|
|
979
|
-
#### Returns
|
|
980
|
-
- `boolean`: `true` if the value is a grouped token, otherwise `false`.
|
|
981
|
-
|
|
982
|
-
### assertRGXGroupedToken
|
|
983
|
-
```typescript
|
|
984
|
-
function assertRGXGroupedToken(value: unknown, contentCheck?: boolean): asserts value is RGXGroupedToken
|
|
985
|
-
```
|
|
986
|
-
|
|
987
|
-
Asserts that the given value is a grouped token. Uses the same logic as `isRGXGroupedToken`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
988
|
-
|
|
989
|
-
#### Parameters
|
|
990
|
-
- `value` (`unknown`): The value to assert.
|
|
991
|
-
- `contentCheck` (`boolean`, optional): Whether to validate the contents of array tokens and the return values of convertible tokens. Defaults to `true`. When `false`, arrays are accepted without checking their elements, and convertible tokens with `rgxGroupWrap` set to `true` are accepted without checking their `toRgx()` return value. This has no effect on the `rgxIsGroup` check, which always accepts the token as grouped regardless of `contentCheck`.
|
|
992
|
-
|
|
993
|
-
#### Returns
|
|
994
|
-
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
995
|
-
|
|
996
|
-
### isValidRegexString
|
|
997
|
-
```typescript
|
|
998
|
-
function isValidRegexString(value: string): value is ValidRegexString
|
|
999
|
-
```
|
|
1000
|
-
|
|
1001
|
-
Checks if the given string is a valid regular expression by attempting to create a new `RegExp` object with it. If it succeeds, the string is branded as a `ValidRegexString`.
|
|
1002
|
-
|
|
1003
|
-
#### Parameters
|
|
1004
|
-
- `value` (`string`): The string to check.
|
|
1005
|
-
|
|
1006
|
-
#### Returns
|
|
1007
|
-
- `boolean`: `true` if the string is a valid regular expression, otherwise `false`.
|
|
1008
|
-
|
|
1009
|
-
### assertValidRegexString
|
|
1010
|
-
```typescript
|
|
1011
|
-
function assertValidRegexString(value: string): asserts value is ValidRegexString
|
|
1012
|
-
```
|
|
1013
|
-
Asserts that the given string is a valid regular expression by attempting to create a new `RegExp` object with it. If it succeeds, the string is branded as a `ValidRegexString`. If the assertion fails, an `RGXInvalidRegexStringError` will be thrown.
|
|
1014
|
-
|
|
1015
|
-
#### Parameters
|
|
1016
|
-
- `value` (`string`): The string to assert.
|
|
1017
|
-
|
|
1018
|
-
#### Returns
|
|
1019
|
-
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
1020
|
-
|
|
1021
|
-
### isValidVanillaRegexFlags
|
|
1022
|
-
```typescript
|
|
1023
|
-
function isValidVanillaRegexFlags(value: string): value is ValidVanillaRegexFlags
|
|
1024
|
-
```
|
|
1025
|
-
|
|
1026
|
-
Checks if the given string is a valid combination of vanilla regex flags (g, i, m, s, u, y). Each flag can only appear once.
|
|
1027
|
-
|
|
1028
|
-
#### Parameters
|
|
1029
|
-
- `value` (`string`): The string to check.
|
|
1030
|
-
|
|
1031
|
-
#### Returns
|
|
1032
|
-
- `boolean`: `true` if the string is a valid combination of vanilla regex flags, otherwise `false`.
|
|
1033
|
-
|
|
1034
|
-
### assertValidVanillaRegexFlags
|
|
1035
|
-
```typescript
|
|
1036
|
-
function assertValidVanillaRegexFlags(value: string): asserts value is ValidVanillaRegexFlags
|
|
1037
|
-
```
|
|
1038
|
-
Asserts that the given string is a valid combination of vanilla regex flags (g, i, m, s, u, y). Each flag can only appear once. If the assertion fails, an `RGXInvalidVanillaRegexFlagsError` will be thrown.
|
|
1039
|
-
|
|
1040
|
-
#### Parameters
|
|
1041
|
-
- `value` (`string`): The string to assert.
|
|
1042
|
-
|
|
1043
|
-
#### Returns
|
|
1044
|
-
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
1045
|
-
|
|
1046
|
-
### isValidIdentifier
|
|
1047
|
-
```typescript
|
|
1048
|
-
function isValidIdentifier(value: string): value is ValidIdentifier
|
|
1049
|
-
```
|
|
1050
|
-
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.
|
|
1051
|
-
|
|
1052
|
-
#### Parameters
|
|
1053
|
-
- `value` (`string`): The string to check.
|
|
1054
|
-
|
|
1055
|
-
#### Returns
|
|
1056
|
-
- `boolean`: `true` if the string is a valid identifier, otherwise `false`.
|
|
1057
|
-
|
|
1058
|
-
### assertValidIdentifier
|
|
1059
|
-
```typescript
|
|
1060
|
-
function assertValidIdentifier(value: string): asserts value is ValidIdentifier
|
|
1061
|
-
```
|
|
1062
|
-
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.
|
|
1063
|
-
|
|
1064
|
-
#### Parameters
|
|
1065
|
-
- `value` (`string`): The string to assert.
|
|
1066
|
-
|
|
1067
|
-
#### Returns
|
|
1068
|
-
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
1069
|
-
|
|
1070
|
-
### escapeRegex
|
|
1071
|
-
```typescript
|
|
1072
|
-
function escapeRegex(value: string): ValidRegexString
|
|
1073
|
-
```
|
|
1074
|
-
|
|
1075
|
-
Escapes special regex characters in the given string and brands the result as a `ValidRegexString`.
|
|
1076
|
-
|
|
1077
|
-
#### Parameters
|
|
1078
|
-
- `value` (`string`): The string to escape.
|
|
1079
|
-
|
|
1080
|
-
#### Returns
|
|
1081
|
-
- `ValidRegexString`: The escaped string, branded as a valid regex string.
|
|
1082
|
-
|
|
1083
|
-
### resolveRGXToken
|
|
1084
|
-
```typescript
|
|
1085
|
-
function resolveRGXToken(token: RGXToken, groupWrap?: boolean, topLevel?: boolean, currentFlags?: string): ValidRegexString
|
|
1086
|
-
```
|
|
1087
|
-
|
|
1088
|
-
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`).
|
|
1089
|
-
|
|
1090
|
-
For literal tokens (`RegExp` instances), if the token's flags differ from `currentFlags` in any of the localizable flags (`i`, `m`, `s`), the token is wrapped in an inline modifier group (e.g., `(?i:...)`, `(?-i:...)`, `(?ms-i:...)`) instead of a plain non-capturing group. Non-localizable flags (such as `g`, `u`, `y`, `d`, `v`) are ignored when computing the diff. When an inline modifier group is used, it always wraps the token regardless of the `groupWrap` setting, since the modifier group itself serves as a group.
|
|
1091
|
-
|
|
1092
|
-
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.
|
|
1093
|
-
|
|
1094
|
-
#### Parameters
|
|
1095
|
-
- `token` (`RGXToken`): The RGX token to resolve.
|
|
1096
|
-
- `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. Note that when a literal token requires an inline modifier group due to a localizable flag diff, it is always wrapped regardless of this setting.
|
|
1097
|
-
- `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.
|
|
1098
|
-
- `currentFlags` (`string`, optional): The flags of the current regex context, used to compute inline modifier groups for literal tokens. Defaults to `''`. When a literal token's localizable flags (`i`, `m`, `s`) differ from this value, the resolver wraps the token in an inline modifier group that adds or removes the differing flags locally.
|
|
1099
|
-
|
|
1100
|
-
#### Returns
|
|
1101
|
-
- `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.
|
|
1102
|
-
|
|
1103
|
-
### rgxConcat
|
|
1104
|
-
```typescript
|
|
1105
|
-
function rgxConcat(tokens: RGXToken[], groupWrap?: boolean, currentFlags?: string): ValidRegexString
|
|
1106
|
-
```
|
|
1107
|
-
|
|
1108
|
-
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. Before returning, any convertible token in the array that defines `rgxAcceptInsertion` is checked; if it returns `false` or a string, an `RGXInsertionRejectedError` is thrown with details about the reason and exactly where the rejection occurred.
|
|
1109
|
-
|
|
1110
|
-
#### Parameters
|
|
1111
|
-
- `tokens` (`RGXToken[]`): The array of RGX tokens to resolve and concatenate.
|
|
1112
|
-
- `groupWrap` (`boolean`, optional): Whether to wrap individual resolved tokens in non-capturing groups. Passed through to `resolveRGXToken`. Defaults to `true`.
|
|
1113
|
-
- `currentFlags` (`string`, optional): The flags of the current regex context, passed through to `resolveRGXToken` as its `currentFlags` parameter. Used to compute inline modifier groups for literal tokens whose localizable flags differ. Defaults to `''`.
|
|
1114
|
-
|
|
1115
|
-
#### Returns
|
|
1116
|
-
- `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.
|
|
1117
|
-
|
|
1118
|
-
### rgx
|
|
1119
|
-
```typescript
|
|
1120
|
-
function rgx(flags?: string, multiline?: boolean): (strings: TemplateStringsArray, ...tokens: RGXToken[]) => ExtRegExp
|
|
1121
|
-
```
|
|
1122
|
-
|
|
1123
|
-
Creates and returns a template tag function that constructs an `ExtRegExp` object from the provided template literal with the provided flags. The template literal can contain RGX tokens, which will be resolved and concatenated with the literal parts to form the final regex pattern. Before constructing the pattern, any convertible token that defines `rgxAcceptInsertion` is checked; if it returns `false` or a string, an `RGXInsertionRejectedError` is thrown with details about the reason and exactly where the rejection occurred.
|
|
1124
|
-
|
|
1125
|
-
When `multiline` is `true` (the default), the literal string parts of the template are processed to strip newlines, trim leading whitespace from each line, and remove empty lines, then joined together. This allows you to write regex patterns across multiple lines in the source code for readability without the newlines and indentation becoming part of the pattern. Only the literal string parts between tokens are affected — interpolated values (tokens) are preserved as-is, including string tokens passed via `${"..."}`. Also, comments (denoted with `//`) ending a line or on a line by themselves are stripped. If a command ends a line, that line is also stripped of whitespace on the right side.
|
|
1126
|
-
|
|
1127
|
-
When `multiline` is `false`, all literal string parts are preserved exactly as written, including newlines and whitespace.
|
|
1128
|
-
|
|
1129
|
-
The provided `flags` are passed as `currentFlags` to the resolver, enabling inline modifier groups for any `RegExp` literal tokens whose localizable flags (`i`, `m`, `s`) differ from the parent flags. For example, embedding `/foo/i` in a no-flag context produces `(?i:foo)`, while embedding `/bar/` in an `i`-flag context produces `(?-i:bar)`.
|
|
1130
|
-
|
|
1131
|
-
Example usages:
|
|
1132
|
-
```typescript
|
|
1133
|
-
const beginning = /^/;
|
|
1134
|
-
const end = /$/;
|
|
1135
|
-
const word = /\w+/;
|
|
1136
|
-
const pattern = rgx()`${beginning}testing ${word}${end}`; // /^testing \w+$/ - matches the string "testing " followed by a word, anchored to the start and end of the string
|
|
1137
|
-
|
|
1138
|
-
const optionalDigit = /\d?/;
|
|
1139
|
-
const pattern2 = rgx()`${beginning}optional digit: ${optionalDigit}${end}`; // /^optional digit: \d?$/ - matches the string "optional digit: " followed by an optional digit, anchored to the start and end of the string
|
|
1140
|
-
|
|
1141
|
-
const pattern3 = rgx()`${beginning}value: ${[word, optionalDigit]}${end}`; // /^value: (?:\w+|\d?)$/ - matches the string "value: " followed by either a word or an optional digit, anchored to the start and end of the string
|
|
1142
|
-
|
|
1143
|
-
const caseInsensitiveWord = /hello/i;
|
|
1144
|
-
const pattern4 = rgx()`${beginning}${caseInsensitiveWord} world${end}`; // /^(?i:hello) world$/ - "hello" matches case-insensitively via an inline modifier group, while " world" remains case-sensitive
|
|
1145
|
-
|
|
1146
|
-
// Multiline template for readability (multiline is true by default):
|
|
1147
|
-
const pattern5 = rgx()`
|
|
1148
|
-
${beginning}
|
|
1149
|
-
testing ${word}
|
|
1150
|
-
${end}
|
|
1151
|
-
`; // /^testing \w+$/ - same as pattern, but written across multiple lines
|
|
1152
|
-
|
|
1153
|
-
// Preserving literal newlines with multiline mode:
|
|
1154
|
-
const pattern6 = rgx()`
|
|
1155
|
-
foo
|
|
1156
|
-
bar${rgxConstant("newline")}
|
|
1157
|
-
baz
|
|
1158
|
-
`; // /foobar\nbaz/ - the constant newline is preserved, but template newlines are stripped
|
|
1159
|
-
```
|
|
1160
|
-
|
|
1161
|
-
#### Parameters
|
|
1162
|
-
**Direct**
|
|
1163
|
-
- `flags` (`string`, optional): The regex flags to apply to the resulting `ExtRegExp` object (e.g., 'g', 'i', 'm', or custom registered flags). If not provided, no flags will be applied. If provided and not valid regex flags (vanilla or registered custom), an `RGXInvalidRegexFlagsError` will be thrown.
|
|
1164
|
-
- `multiline` (`boolean`, optional): Whether to strip newlines and trim leading whitespace from the literal string parts of the template. Defaults to `true`. When `true`, each literal string part is split by newlines, each line has its leading whitespace trimmed, empty lines are removed, comments (denoted with `//`) ending a line or on a line by themselves are stripped, and the remaining lines are joined together. Interpolated tokens (including string tokens via `${"..."}`) are not affected. When `false`, literal string parts are preserved exactly as written.
|
|
1165
|
-
|
|
1166
|
-
**Template Tag**
|
|
1167
|
-
- `strings` (`TemplateStringsArray`): The literal parts of the template string.
|
|
1168
|
-
- `tokens` (`RGXToken[]`): The RGX tokens to be resolved and concatenated with the literal parts.
|
|
1169
|
-
|
|
1170
|
-
#### Returns
|
|
1171
|
-
- `(strings: TemplateStringsArray, ...tokens: RGXToken[]) => ExtRegExp`: A template tag function that takes a template literal and returns an `ExtRegExp` object constructed from the resolved tokens, literal parts, and the provided flags.
|
|
1172
|
-
|
|
1173
|
-
### rgxa
|
|
1174
|
-
```typescript
|
|
1175
|
-
function rgxa(tokens: RGXToken[], flags?: string): ExtRegExp
|
|
1176
|
-
```
|
|
1177
|
-
As an alternative to using the `rgx` template tag, you can directly call `rgxa` with an array of RGX tokens and optional flags to get an `ExtRegExp` object. This is useful in cases where you don't want to use a template literal. Like `rgx`, the provided `flags` are passed as `currentFlags` to the resolver, enabling inline modifier groups for `RegExp` literal tokens whose localizable flags differ. Before constructing the pattern, any convertible token in the array that defines `rgxAcceptInsertion` is checked; if it returns `false` or a string, an `RGXInsertionRejectedError` is thrown with details about the reason and exactly where the rejection occurred.
|
|
1178
|
-
|
|
1179
|
-
#### Parameters
|
|
1180
|
-
- `tokens` (`RGXToken[]`): The RGX tokens to be resolved and concatenated to form the regex pattern.
|
|
1181
|
-
- `flags` (`string`, optional): The regex flags to apply to the resulting `ExtRegExp` object (e.g., 'g', 'i', 'm', or custom registered flags). If not provided, no flags will be applied. If provided and not valid regex flags (vanilla or registered custom), an `RGXInvalidRegexFlagsError` will be thrown.
|
|
1182
|
-
|
|
1183
|
-
#### Returns
|
|
1184
|
-
- `ExtRegExp`: An `ExtRegExp` object constructed from the resolved tokens and the provided flags.
|
|
1185
|
-
|
|
1186
|
-
### rgxw
|
|
1187
|
-
```typescript
|
|
1188
|
-
function rgxw<R = unknown>(source: string, {multiline=true, ...options}: RGXWOptions<R> = {}): (strings: TemplateStringsArray, ...tokens: RGXToken[]
|
|
1189
|
-
) => RGXWalker<R>
|
|
1190
|
-
```
|
|
1191
|
-
A helper function that creates an `RGXWalker` instance from an interpolation of strings and tokens. The token array is processed exactly like in `rgx`, but instead of returning an `ExtRegExp`, it returns an `RGXWalker` that can be used to walk through matches of the regex pattern in the source string.
|
|
1192
|
-
|
|
1193
|
-
#### Parameters
|
|
1194
|
-
- `source` (`string`): An arbitrary string value that will be included in the `source` property of the walker object.
|
|
1195
|
-
- `options` (`RGXWOptions<R>`, optional): Additional options for configuring the behavior of the resulting `RGXWalker`. This includes:
|
|
1196
|
-
- `multiline` (`boolean`, optional): Whether to strip newlines and trim leading whitespace from the literal string parts of the template. Defaults to `true`. When `true`, each literal string part is split by newlines, each line has its leading whitespace trimmed, empty lines are removed, comments (denoted with `//`) ending a line or on a line by themselves are stripped, and the remaining lines are joined together. Interpolated tokens (including string tokens via `${"..."}`) are not affected. When `false`, literal string parts are preserved exactly as written.
|
|
1197
|
-
- `reduced` (`R`, optional): An optional initial value for the walker's `reduced` property, which can be used to accumulate results across matches.
|
|
1198
|
-
|
|
1199
|
-
#### Returns
|
|
1200
|
-
- `(strings: TemplateStringsArray, ...tokens: RGXToken[]) => RGXWalker<R>`: A template tag function that takes a template literal and returns an `RGXWalker` instance configured with the provided source, the provided tokens, and the specified options.
|
|
1201
|
-
|
|
1202
|
-
### rgxwa
|
|
1203
|
-
```typescript
|
|
1204
|
-
function rgxwa<R = unknown>(source: string, tokens: RGXToken[], options: Omit<RGXWOptions<R>, "multiline"> = {}): RGXWalker<R>
|
|
1205
|
-
```
|
|
1206
|
-
As an alternative to using the `rgxw` template tag, you can directly call `rgxwa` with a source string, an array of RGX tokens, and options to get an `RGXWalker` instance. This is useful in cases where you don't want to use a template literal. The token array is processed exactly like in `rgxa`, and the provided options are passed through to configure the resulting walker.
|
|
1207
|
-
|
|
1208
|
-
#### Parameters
|
|
1209
|
-
- `source` (`string`): An arbitrary string value that will be included in the `source` property of the walker object.
|
|
1210
|
-
- `tokens` (`RGXToken[]`): The RGX tokens to be resolved and concatenated to form the regex pattern for the walker.
|
|
1211
|
-
- `options` (`Omit<RGXWOptions<R>, "multiline">`, optional): Additional options for configuring the behavior of the resulting `RGXWalker`, excluding the `multiline` option which is not applicable when not using a template literal. This includes:
|
|
1212
|
-
- `reduced` (`R`, optional): An optional initial value for the walker's `reduced` property, which can be used to accumulate results across matches.
|
|
1213
|
-
|
|
1214
|
-
#### Returns
|
|
1215
|
-
- `RGXWalker<R>`: An `RGXWalker` instance configured with the provided source, the resolved tokens, and the specified options.
|
|
1216
|
-
|
|
1217
|
-
### expandRgxUnionTokens
|
|
1218
|
-
```typescript
|
|
1219
|
-
function expandRgxUnionTokens(...tokens: RGXTokenCollectionInput[]): RGXTokenCollection
|
|
1220
|
-
```
|
|
1221
|
-
|
|
1222
|
-
Recursively expands nested union tokens (arrays, `RGXTokenCollection` instances in union mode, and `RGXClassUnionToken` instances) into a flat `RGXTokenCollection`. This is useful for normalizing a set of union alternatives before deduplication.
|
|
1223
|
-
|
|
1224
|
-
#### Parameters
|
|
1225
|
-
- `tokens` (`...RGXTokenCollectionInput[]`): The tokens to expand.
|
|
1226
|
-
|
|
1227
|
-
#### Returns
|
|
1228
|
-
- `RGXTokenCollection`: A flat collection containing all expanded tokens.
|
|
1229
|
-
|
|
1230
|
-
### removeRgxUnionDuplicates
|
|
1231
|
-
```typescript
|
|
1232
|
-
function removeRgxUnionDuplicates(...tokens: RGXTokenCollectionInput[]): RGXTokenCollection
|
|
1233
|
-
```
|
|
1234
|
-
|
|
1235
|
-
Removes duplicate tokens from the provided list using `Set` equality and returns a new `RGXTokenCollection` in union mode containing only the unique tokens.
|
|
1236
|
-
|
|
1237
|
-
#### Parameters
|
|
1238
|
-
- `tokens` (`...RGXTokenCollectionInput[]`): The tokens to deduplicate.
|
|
1239
|
-
|
|
1240
|
-
#### Returns
|
|
1241
|
-
- `RGXTokenCollection`: A union-mode collection with duplicates removed.
|
|
1242
|
-
|
|
1243
|
-
### rgxClassInit
|
|
1244
|
-
```typescript
|
|
1245
|
-
function rgxClassInit(): void
|
|
1246
|
-
```
|
|
1247
|
-
|
|
1248
|
-
Initializes internal method patches required for `RGXClassToken` subclass methods (such as `or`, `group`, `repeat`, `asLookahead`, and `asLookbehind`) 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.
|
|
1249
|
-
|
|
1250
|
-
### toRGXClassToken
|
|
1251
|
-
```typescript
|
|
1252
|
-
function toRGXClassToken(token: RGXToken): RGXClassToken
|
|
1253
|
-
```
|
|
1254
|
-
|
|
1255
|
-
Converts any `RGXToken` into an appropriate `RGXClassToken` subclass, giving you access to the extended API that class tokens provide. Tokens that are already class tokens are returned as-is. Array tokens and `RGXTokenCollection` instances in union mode are converted to `RGXClassUnionToken`. `RGXTokenCollection` instances in concat mode are converted to a non-capturing `RGXGroupToken`. All other tokens are wrapped in an `RGXClassWrapperToken`.
|
|
1256
|
-
|
|
1257
|
-
#### Parameters
|
|
1258
|
-
- `token` (`RGXToken`): The token to convert.
|
|
1259
|
-
|
|
1260
|
-
#### Returns
|
|
1261
|
-
- `RGXClassToken`: The corresponding class token:
|
|
1262
|
-
- `RGXClassUnionToken` for array tokens and union-mode `RGXTokenCollection` instances.
|
|
1263
|
-
- `RGXGroupToken` (non-capturing) for concat-mode `RGXTokenCollection` instances.
|
|
1264
|
-
- `RGXClassWrapperToken` for all other tokens.
|
|
1265
|
-
|
|
1266
|
-
### isInRange
|
|
1267
|
-
```typescript
|
|
1268
|
-
function isInRange(value: number, { min, max, inclusiveLeft, inclusiveRight }?: RangeObject): boolean
|
|
1269
|
-
```
|
|
1270
|
-
|
|
1271
|
-
Checks if the given numeric value falls within the specified range.
|
|
1272
|
-
|
|
1273
|
-
#### Parameters
|
|
1274
|
-
- `value` (`number`): The value to check.
|
|
1275
|
-
- `min` (`number | null`, optional): The minimum bound of the range. Defaults to `null` (no minimum).
|
|
1276
|
-
- `max` (`number | null`, optional): The maximum bound of the range. Defaults to `null` (no maximum).
|
|
1277
|
-
- `inclusiveLeft` (`boolean`, optional): Whether the minimum bound is inclusive. Defaults to `true`.
|
|
1278
|
-
- `inclusiveRight` (`boolean`, optional): Whether the maximum bound is inclusive. Defaults to `true`.
|
|
1279
|
-
|
|
1280
|
-
#### Returns
|
|
1281
|
-
- `boolean`: `true` if the value is within the specified range, otherwise `false`.
|
|
1282
|
-
|
|
1283
|
-
### assertInRange
|
|
1284
|
-
```typescript
|
|
1285
|
-
function assertInRange(value: number, range: RangeObject, message?: string): void
|
|
1286
|
-
```
|
|
1287
|
-
|
|
1288
|
-
Asserts that the given numeric value falls within the specified range. If the assertion fails, an `RGXOutOfBoundsError` will be thrown.
|
|
1289
|
-
|
|
1290
|
-
#### Parameters
|
|
1291
|
-
- `value` (`number`): The value to assert.
|
|
1292
|
-
- `range` (`RangeObject`): The range to check against.
|
|
1293
|
-
- `message` (`string`, optional): A custom error message. Defaults to `"Value out of bounds"`.
|
|
1294
|
-
|
|
1295
|
-
#### Returns
|
|
1296
|
-
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
1297
|
-
|
|
1298
|
-
### isValidRegexFlags
|
|
1299
|
-
```typescript
|
|
1300
|
-
function isValidRegexFlags(flags: string): flags is ValidRegexFlags
|
|
1301
|
-
```
|
|
1302
|
-
|
|
1303
|
-
Checks if the given string is a valid combination of regex flags, including both vanilla flags (g, i, m, s, u, y) and any custom flags registered via `registerFlagTransformer`. Custom flag characters are stripped before validating the remaining characters as vanilla flags.
|
|
1304
|
-
|
|
1305
|
-
#### Parameters
|
|
1306
|
-
- `flags` (`string`): The string to check.
|
|
1307
|
-
|
|
1308
|
-
#### Returns
|
|
1309
|
-
- `boolean`: `true` if the string is a valid combination of regex flags, otherwise `false`.
|
|
1310
|
-
|
|
1311
|
-
### assertValidRegexFlags
|
|
1312
|
-
```typescript
|
|
1313
|
-
function assertValidRegexFlags(flags: string): asserts flags is ValidRegexFlags
|
|
1314
|
-
```
|
|
1315
|
-
|
|
1316
|
-
Asserts that the given string is a valid combination of regex flags, including both vanilla flags and any custom registered flags. If the assertion fails, an `RGXInvalidRegexFlagsError` will be thrown.
|
|
1317
|
-
|
|
1318
|
-
#### Parameters
|
|
1319
|
-
- `flags` (`string`): The string to assert.
|
|
1320
|
-
|
|
1321
|
-
#### Returns
|
|
1322
|
-
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
1323
|
-
|
|
1324
|
-
### isFlagKeyAvailable
|
|
1325
|
-
```typescript
|
|
1326
|
-
function isFlagKeyAvailable(flags: string): boolean
|
|
1327
|
-
```
|
|
1328
|
-
|
|
1329
|
-
Checks if the given string is available for use as a custom flag transformer key. Returns `false` if the string is a vanilla regex flag or if any character in the string is already registered as a custom flag transformer.
|
|
1330
|
-
|
|
1331
|
-
#### Parameters
|
|
1332
|
-
- `flags` (`string`): The string to check.
|
|
1333
|
-
|
|
1334
|
-
#### Returns
|
|
1335
|
-
- `boolean`: `true` if the string is available for use as a custom flag key, otherwise `false`.
|
|
1336
|
-
|
|
1337
|
-
### registerFlagTransformer
|
|
1338
|
-
```typescript
|
|
1339
|
-
function registerFlagTransformer(key: string, transformer: RegExpFlagTransformer): void
|
|
1340
|
-
```
|
|
1341
|
-
|
|
1342
|
-
Registers a custom flag transformer under the given single-character key. The key must be exactly one character, must not be a vanilla regex flag, and must not already be registered. When an `ExtRegExp` is constructed with this flag character in its flags string, the transformer function will be called with the `RegExp` to transform it.
|
|
1343
|
-
|
|
1344
|
-
#### Parameters
|
|
1345
|
-
- `key` (`string`): A single-character string to use as the flag key. Must not be a vanilla regex flag or an already-registered key.
|
|
1346
|
-
- `transformer` (`RegExpFlagTransformer`): A function that takes a `RegExp` and returns a transformed `RegExp`.
|
|
1347
|
-
|
|
1348
|
-
#### Returns
|
|
1349
|
-
- `void`: This function does not return a value, but will throw an `RGXInvalidFlagTransformerKeyError` if the key is not a single character, or an `RGXFlagTransformerConflictError` if the key conflicts with a vanilla flag or an existing transformer.
|
|
1350
|
-
|
|
1351
|
-
### unregisterFlagTransformer
|
|
1352
|
-
```typescript
|
|
1353
|
-
function unregisterFlagTransformer(key: string): void
|
|
1354
|
-
```
|
|
1355
|
-
|
|
1356
|
-
Unregisters a previously registered custom flag transformer by its key. If the key was not registered, this is a no-op.
|
|
1357
|
-
|
|
1358
|
-
#### Parameters
|
|
1359
|
-
- `key` (`string`): The flag key to unregister.
|
|
1360
|
-
|
|
1361
|
-
#### Returns
|
|
1362
|
-
- `void`: This function does not return a value.
|
|
1363
|
-
|
|
1364
|
-
### accentInsensitiveFlagTransformer
|
|
1365
|
-
```typescript
|
|
1366
|
-
const accentInsensitiveFlagTransformer: RegExpFlagTransformer
|
|
1367
|
-
```
|
|
1368
|
-
|
|
1369
|
-
A pre-built `RegExpFlagTransformer` that makes a regex pattern accent-insensitive. It replaces any accentable characters (a, e, i, o, u and their uppercase equivalents) in the regex source with alternation groups that match both the base character and its accented variants. For example, `é` becomes `(e|é|è|ë|ê)`. The following accent mappings are supported:
|
|
1370
|
-
|
|
1371
|
-
- `a` / `A`: á, à, ä, â, ã / Á, À, Ä, Â, Ã
|
|
1372
|
-
- `e` / `E`: é, è, ë, ê / É, È, Ë, Ê
|
|
1373
|
-
- `i` / `I`: í, ì, ï, î / Í, Ì, Ï, Î
|
|
1374
|
-
- `o` / `O`: ó, ò, ö, ô, õ / Ó, Ò, Ö, Ô, Õ
|
|
1375
|
-
- `u` / `U`: ú, ù, ü, û / Ú, Ù, Ü, Û
|
|
1376
|
-
|
|
1377
|
-
Note that this transformer intentionally excludes replacing characters preceded by an odd number of backslashes, to allow for escaping. For example, in the pattern `\\a`, the `a` is preceded by two backslashes (an even number), so it will be replaced with `(a|á|à|ä|â|ã)`. In the pattern `\a`, the `a` is preceded by one backslash (an odd number), so it will not be replaced.
|
|
1378
|
-
|
|
1379
|
-
Also, characters part of a localized flag diff inline modifier (e.g., `(?i:a)`) are not replaced, as that would introduce invalid syntax. This refers to the `i` here, not the `a`, since only `i` is a localizable flag.
|
|
1380
|
-
|
|
1381
|
-
Finally, characters part of a character class are avoided for transformation, since that would introduce syntax errors again. For example, in the pattern `[a]`, the `a` is part of a character class and will not be replaced.
|
|
1382
|
-
|
|
1383
|
-
These conditions (especially the last two) may cause some patterns that should be transformed to be skipped, but that is better than having the transformer produce invalid regex patterns.
|
|
1384
|
-
|
|
1385
|
-
#### Parameters
|
|
1386
|
-
- `exp` (`RegExp`): The regular expression to transform.
|
|
1387
|
-
|
|
1388
|
-
#### Returns
|
|
1389
|
-
- `RegExp`: A new `RegExp` with the same flags but with accentable characters in the source replaced by accent-insensitive alternation groups.
|
|
1390
|
-
|
|
1391
|
-
### registerCustomFlagTransformers
|
|
1392
|
-
```typescript
|
|
1393
|
-
function registerCustomFlagTransformers(): void
|
|
1394
|
-
```
|
|
1395
|
-
|
|
1396
|
-
Registers the library's built-in custom flag transformers. Currently registers the following:
|
|
1397
|
-
- `"a"` flag: `accentInsensitiveFlagTransformer` — makes patterns accent-insensitive.
|
|
1398
|
-
|
|
1399
|
-
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.
|
|
1400
|
-
|
|
1401
|
-
#### Returns
|
|
1402
|
-
- `void`: This function does not return a value.
|
|
1403
|
-
|
|
1404
|
-
### unregisterCustomFlagTransformers
|
|
1405
|
-
```typescript
|
|
1406
|
-
function unregisterCustomFlagTransformers(): void
|
|
1407
|
-
```
|
|
1408
|
-
|
|
1409
|
-
Unregisters all built-in custom flag transformers that were registered by `registerCustomFlagTransformers`. Currently unregisters the `"a"` flag.
|
|
1410
|
-
|
|
1411
|
-
#### Returns
|
|
1412
|
-
- `void`: This function does not return a value.
|
|
1413
|
-
|
|
1414
|
-
### applyFlagTransformers
|
|
1415
|
-
```typescript
|
|
1416
|
-
function applyFlagTransformers(regex: RegExp, flags: string, alreadyAppliedFlags?: string): RegExp
|
|
1417
|
-
```
|
|
1418
|
-
|
|
1419
|
-
Applies all registered flag transformers whose keys appear in the given flags string to the provided `RegExp`, returning the resulting transformed `RegExp`. Flags present in `alreadyAppliedFlags` are skipped to avoid re-applying transformers.
|
|
1420
|
-
|
|
1421
|
-
#### Parameters
|
|
1422
|
-
- `regex` (`RegExp`): The regular expression to transform.
|
|
1423
|
-
- `flags` (`string`): The flags string containing custom flag characters to apply.
|
|
1424
|
-
- `alreadyAppliedFlags` (`string`, optional): A string of flag characters that have already been applied and should be skipped. Defaults to `''`.
|
|
1425
|
-
|
|
1426
|
-
#### Returns
|
|
1427
|
-
- `RegExp`: The transformed `RegExp` after applying all matching flag transformers.
|
|
1428
|
-
|
|
1429
|
-
### extractCustomRegexFlags
|
|
1430
|
-
```typescript
|
|
1431
|
-
function extractCustomRegexFlags(flags: string): string
|
|
1432
|
-
```
|
|
1433
|
-
|
|
1434
|
-
Extracts the custom (non-vanilla) flag characters from the given flags string by returning only the characters that correspond to registered flag transformers.
|
|
1435
|
-
|
|
1436
|
-
#### Parameters
|
|
1437
|
-
- `flags` (`string`): The flags string to extract custom flags from.
|
|
1438
|
-
|
|
1439
|
-
#### Returns
|
|
1440
|
-
- `string`: A string containing only the custom flag characters found in the input.
|
|
1441
|
-
|
|
1442
|
-
### extractVanillaRegexFlags
|
|
1443
|
-
```typescript
|
|
1444
|
-
function extractVanillaRegexFlags(flags: string): string
|
|
1445
|
-
```
|
|
1446
|
-
|
|
1447
|
-
Extracts the vanilla regex flag characters from the given flags string by removing all characters that correspond to registered flag transformers.
|
|
1448
|
-
|
|
1449
|
-
#### Parameters
|
|
1450
|
-
- `flags` (`string`): The flags string to extract vanilla flags from.
|
|
1451
|
-
|
|
1452
|
-
#### Returns
|
|
1453
|
-
- `string`: A string with all registered custom flag characters removed, leaving only vanilla flags.
|
|
1454
|
-
|
|
1455
|
-
### normalizeRegexFlags
|
|
1456
|
-
```typescript
|
|
1457
|
-
function normalizeRegexFlags(flags: string): string
|
|
1458
|
-
```
|
|
1459
|
-
|
|
1460
|
-
Normalizes a string of regex flags (including both vanilla and custom registered flags) by removing duplicate flags while preserving order. If any character in the string is not a valid regex flag (vanilla or registered custom), an `RGXInvalidRegexFlagsError` will be thrown.
|
|
1461
|
-
|
|
1462
|
-
#### Parameters
|
|
1463
|
-
- `flags` (`string`): The flags string to normalize.
|
|
1464
|
-
|
|
1465
|
-
#### Returns
|
|
1466
|
-
- `string`: The normalized flags string with duplicates removed.
|
|
1467
|
-
|
|
1468
|
-
### normalizeVanillaRegexFlags
|
|
1469
|
-
```typescript
|
|
1470
|
-
function normalizeVanillaRegexFlags(flags: string): string
|
|
1471
|
-
```
|
|
1472
|
-
|
|
1473
|
-
Normalizes a string of vanilla regex flags by removing duplicate flags while preserving order. First validates that all characters are valid vanilla regex flags (g, i, m, s, u, y), throwing an `RGXInvalidVanillaRegexFlagsError` if any are not, then delegates to `normalizeRegexFlags` for deduplication.
|
|
1474
|
-
|
|
1475
|
-
#### Parameters
|
|
1476
|
-
- `flags` (`string`): The flags string to normalize.
|
|
1477
|
-
|
|
1478
|
-
#### Returns
|
|
1479
|
-
- `string`: The normalized flags string with duplicates removed.
|
|
1480
|
-
|
|
1481
|
-
### regexWithFlags
|
|
1482
|
-
```typescript
|
|
1483
|
-
function regexWithFlags(exp: RegExp | ExtRegExp, flags: string, replace?: boolean): ExtRegExp
|
|
1484
|
-
```
|
|
1485
|
-
|
|
1486
|
-
Creates a new `ExtRegExp` from an existing one with additional or replaced flags. When `replace` is `false` (the default), the provided flags are merged with the existing flags and normalized (duplicates removed). When `replace` is `true`, the existing flags are discarded and only the provided flags are used. The provided flags are validated as valid vanilla regex flags via `assertValidVanillaRegexFlags`.
|
|
1487
|
-
|
|
1488
|
-
#### Parameters
|
|
1489
|
-
- `exp` (`RegExp | ExtRegExp`): The source regular expression.
|
|
1490
|
-
- `flags` (`string`): The flags to add or replace with. Must be valid vanilla regex flags, or an `RGXInvalidVanillaRegexFlagsError` will be thrown.
|
|
1491
|
-
- `replace` (`boolean`, optional): Whether to replace the existing flags entirely instead of merging. Defaults to `false`.
|
|
1492
|
-
|
|
1493
|
-
#### Returns
|
|
1494
|
-
- `ExtRegExp`: A new `ExtRegExp` with the same source pattern and the resulting flags.
|
|
1495
|
-
|
|
1496
|
-
### regexMatchAtPosition
|
|
1497
|
-
```typescript
|
|
1498
|
-
function regexMatchAtPosition(regex: RegExp, str: string, position: number, includeMatch: true): RegExpExecArray | null
|
|
1499
|
-
function regexMatchAtPosition(regex: RegExp, str: string, position: number, includeMatch?: false): string | null
|
|
1500
|
-
```
|
|
1501
|
-
|
|
1502
|
-
Attempts to match the given regular expression at a specific position in the string. This is done by creating a sticky (`y` flag) copy of the regex and setting its `lastIndex` to the desired position. The position must be within the bounds of the string (>= 0 and < string length), or an `RGXOutOfBoundsError` will be thrown.
|
|
1503
|
-
|
|
1504
|
-
#### Parameters
|
|
1505
|
-
- `regex` (`RegExp`): The regular expression to match.
|
|
1506
|
-
- `str` (`string`): The string to match against.
|
|
1507
|
-
- `position` (`number`): The zero-based index in the string at which to attempt the match. Must be >= 0 and < `str.length`.
|
|
1508
|
-
- `includeMatch` (`boolean`, optional): When `true`, returns the full `RegExpExecArray` instead of just the matched string. Defaults to `false`.
|
|
1509
|
-
|
|
1510
|
-
#### Returns
|
|
1511
|
-
- `string | null`: When `includeMatch` is `false` (default): the matched string if the regex matches at the specified position, otherwise `null`.
|
|
1512
|
-
- `RegExpExecArray | null`: When `includeMatch` is `true`: the full match array if the regex matches, otherwise `null`.
|
|
1513
|
-
|
|
1514
|
-
### doesRegexMatchAtPosition
|
|
1515
|
-
```typescript
|
|
1516
|
-
function doesRegexMatchAtPosition(regex: RegExp, str: string, position: number, includeMatch: true): RegExpExecArray | false
|
|
1517
|
-
function doesRegexMatchAtPosition(regex: RegExp, str: string, position: number, includeMatch?: false): boolean
|
|
1518
|
-
```
|
|
1519
|
-
|
|
1520
|
-
Tests whether the given regular expression matches at a specific position in the string.
|
|
1521
|
-
|
|
1522
|
-
#### Parameters
|
|
1523
|
-
- `regex` (`RegExp`): The regular expression to test.
|
|
1524
|
-
- `str` (`string`): The string to test against.
|
|
1525
|
-
- `position` (`number`): The zero-based index in the string at which to test the match. Must be >= 0 and < `str.length`.
|
|
1526
|
-
- `includeMatch` (`boolean`, optional): When `true`, returns the full `RegExpExecArray` on a match instead of `true`. Defaults to `false`.
|
|
1527
|
-
|
|
1528
|
-
#### Returns
|
|
1529
|
-
- `boolean`: When `includeMatch` is `false` (default): `true` if the regex matches at the specified position, otherwise `false`.
|
|
1530
|
-
- `RegExpExecArray | false`: When `includeMatch` is `true`: the full match array if the regex matches, otherwise `false`.
|
|
1531
|
-
|
|
1532
|
-
### assertRegexMatchesAtPosition
|
|
1533
|
-
```typescript
|
|
1534
|
-
function assertRegexMatchesAtPosition(regex: RegExp, str: string, position: number, contextSize?: number | null, includeMatch?: false): string
|
|
1535
|
-
function assertRegexMatchesAtPosition(regex: RegExp, str: string, position: number, contextSize: number | null | undefined, includeMatch: true): RegExpExecArray
|
|
1536
|
-
```
|
|
1537
|
-
|
|
1538
|
-
Asserts that the given regular expression matches at a specific position in the string, throwing an `RGXRegexNotMatchedAtPositionError` if it does not. On success, returns the matched string or full match array depending on `includeMatch`.
|
|
1539
|
-
|
|
1540
|
-
#### Parameters
|
|
1541
|
-
- `regex` (`RegExp`): The regular expression to match.
|
|
1542
|
-
- `str` (`string`): The string to match against.
|
|
1543
|
-
- `position` (`number`): The zero-based index in the string at which to assert the match. Must be >= 0 and < `str.length`.
|
|
1544
|
-
- `contextSize` (`number | null`, optional): The number of characters on each side of the position to include in the error's context output. Defaults to `10`.
|
|
1545
|
-
- `includeMatch` (`boolean`, optional): When `true`, returns the full `RegExpExecArray` instead of just the matched string. Defaults to `false`.
|
|
1546
|
-
|
|
1547
|
-
#### Returns
|
|
1548
|
-
- `string`: When `includeMatch` is `false` (default): the matched string. Throws `RGXRegexNotMatchedAtPositionError` if there is no match.
|
|
1549
|
-
- `RegExpExecArray`: When `includeMatch` is `true`: the full match array. Throws `RGXRegexNotMatchedAtPositionError` if there is no match.
|
|
1550
|
-
|
|
1551
|
-
### cloneRGXToken
|
|
1552
|
-
```typescript
|
|
1553
|
-
function cloneRGXTokeN<T extends RGXToken>(token: T, depth: CloneDepth="max"): T
|
|
1554
|
-
```
|
|
1555
|
-
Creates a clone of the given RGX token to the given depth, provided that the token is not a no-op or native token.
|
|
1556
|
-
|
|
1557
|
-
#### Parameters
|
|
1558
|
-
- `token` (`T`): The RGX token to clone. Must not be a no-op or native token, or an error will be thrown.
|
|
1559
|
-
- `depth` (`CloneDepth`, optional): The depth to which to clone the token. Can be a number (with 0 resulting in no clone at all and 1 resulting in a shallow clone) or the string `"max"` for a full deep clone. Defaults to `"max"`.
|
|
1560
|
-
|
|
1561
|
-
#### Returns
|
|
1562
|
-
- `T`: The cloned token.
|
|
1563
|
-
|
|
1564
|
-
### RGX_PREDEFINED_CONSTANTS
|
|
1565
|
-
```typescript
|
|
1566
|
-
const RGX_PREDEFINED_CONSTANTS: Record<RGXPredefinedConstant, RGXToken>
|
|
1567
|
-
```
|
|
1568
|
-
|
|
1569
|
-
A read-only object containing all of the library's built-in constant definitions, keyed by their `RGXPredefinedConstant` name. This is the source from which the predefined constants are registered at module load time. It can be used to inspect available constant names at compile time (via `keyof typeof RGX_PREDEFINED_CONSTANTS`) or to iterate over the predefined set without calling `listRGXConstants`.
|
|
1570
|
-
|
|
1571
|
-
### listRGXConstants
|
|
1572
|
-
```typescript
|
|
1573
|
-
function listRGXConstants(): string[]
|
|
1574
|
-
```
|
|
1575
|
-
|
|
1576
|
-
Returns the names of all currently defined RGX constants.
|
|
1577
|
-
|
|
1578
|
-
#### Returns
|
|
1579
|
-
- `string[]`: An array of constant names.
|
|
1580
|
-
|
|
1581
|
-
### hasRGXConstant
|
|
1582
|
-
```typescript
|
|
1583
|
-
function hasRGXConstant(name: RGXConstantName): boolean
|
|
1584
|
-
```
|
|
1585
|
-
|
|
1586
|
-
Checks if an RGX constant with the given name exists.
|
|
1587
|
-
|
|
1588
|
-
#### Parameters
|
|
1589
|
-
- `name` (`RGXConstantName`): The constant name to check.
|
|
1590
|
-
|
|
1591
|
-
#### Returns
|
|
1592
|
-
- `boolean`: `true` if the constant exists, otherwise `false`.
|
|
1593
|
-
|
|
1594
|
-
### assertHasRGXConstant
|
|
1595
|
-
```typescript
|
|
1596
|
-
function assertHasRGXConstant(name: RGXConstantName): void
|
|
1597
|
-
```
|
|
1598
|
-
|
|
1599
|
-
Asserts that an RGX constant with the given name exists. If the assertion fails, an `RGXInvalidConstantKeyError` will be thrown.
|
|
1600
|
-
|
|
1601
|
-
#### Parameters
|
|
1602
|
-
- `name` (`RGXConstantName`): The constant name to assert.
|
|
1603
|
-
|
|
1604
|
-
#### Returns
|
|
1605
|
-
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
1606
|
-
|
|
1607
|
-
### assertNotHasRGXConstant
|
|
1608
|
-
```typescript
|
|
1609
|
-
function assertNotHasRGXConstant(name: RGXConstantName): void
|
|
1610
|
-
```
|
|
1611
|
-
|
|
1612
|
-
Asserts that an RGX constant with the given name does not exist. If the assertion fails, an `RGXConstantConflictError` will be thrown.
|
|
1613
|
-
|
|
1614
|
-
#### Parameters
|
|
1615
|
-
- `name` (`RGXConstantName`): The constant name to assert.
|
|
1616
|
-
|
|
1617
|
-
#### Returns
|
|
1618
|
-
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
1619
|
-
|
|
1620
|
-
### defineRGXConstant
|
|
1621
|
-
```typescript
|
|
1622
|
-
function defineRGXConstant(name: RGXConstantName, value: RGXToken): RGXToken
|
|
1623
|
-
```
|
|
1624
|
-
|
|
1625
|
-
Defines a new RGX constant with the given name and value. If the value is a native token (string, number, boolean, or no-op), it is automatically wrapped in an `RGXClassWrapperToken` before being stored. This ensures that native-valued constants are not stripped by multiline template processing in `rgx`, since only the literal string parts of the template are affected by multiline mode. Throws an `RGXConstantConflictError` if a constant with the same name already exists.
|
|
1626
|
-
|
|
1627
|
-
#### Parameters
|
|
1628
|
-
- `name` (`RGXConstantName`): The name for the constant.
|
|
1629
|
-
- `value` (`RGXToken`): The token value to associate with the name. Native tokens are automatically wrapped in `RGXClassWrapperToken`.
|
|
1630
|
-
|
|
1631
|
-
#### Returns
|
|
1632
|
-
- `RGXToken`: The stored value (after wrapping, if applicable).
|
|
1633
|
-
|
|
1634
|
-
### rgxConstant
|
|
1635
|
-
```typescript
|
|
1636
|
-
function rgxConstant(name: RGXConstantName): RGXToken
|
|
1637
|
-
```
|
|
1638
|
-
|
|
1639
|
-
Retrieves the value of an RGX constant by name. Throws an `RGXInvalidConstantKeyError` if no constant with the given name exists.
|
|
1640
|
-
|
|
1641
|
-
#### Parameters
|
|
1642
|
-
- `name` (`RGXConstantName`): The constant name to retrieve.
|
|
1643
|
-
|
|
1644
|
-
#### Returns
|
|
1645
|
-
- `RGXToken`: The token value associated with the constant name.
|
|
1646
|
-
|
|
1647
|
-
### deleteRGXConstant
|
|
1648
|
-
```typescript
|
|
1649
|
-
function deleteRGXConstant(name: RGXConstantName): void
|
|
1650
|
-
```
|
|
1651
|
-
|
|
1652
|
-
Deletes an existing RGX constant by name. Throws an `RGXInvalidConstantKeyError` if no constant with the given name exists.
|
|
1653
|
-
|
|
1654
|
-
#### Parameters
|
|
1655
|
-
- `name` (`RGXConstantName`): The constant name to delete.
|
|
1656
|
-
|
|
1657
|
-
#### Returns
|
|
1658
|
-
- `void`: This function does not return a value, but will throw an error if the constant does not exist.
|
|
1659
|
-
|
|
1660
|
-
## Built-in Constants
|
|
1661
|
-
The library defines the following built-in constants, which are available immediately after import. Each can be retrieved via `rgxConstant(name)`.
|
|
1662
|
-
|
|
1663
|
-
### Control Characters
|
|
1664
|
-
Since these are defined as native tokens (strings), they are automatically wrapped in `RGXClassWrapperToken` by `defineRGXConstant`, ensuring they are preserved in multiline mode.
|
|
1665
|
-
|
|
1666
|
-
| Name | Resolves To | Description |
|
|
1667
|
-
| --- | --- | --- |
|
|
1668
|
-
| `"newline"` | `\n` | Newline character |
|
|
1669
|
-
| `"carriage-return"` | `\r` | Carriage return character |
|
|
1670
|
-
| `"tab"` | `\t` | Tab character |
|
|
1671
|
-
| `"null"` | `\0` | Null character |
|
|
1672
|
-
| `"form-feed"` | `\f` | Form feed character |
|
|
1673
|
-
|
|
1674
|
-
### Special Characters
|
|
1675
|
-
| Name | Resolves To | Description |
|
|
1676
|
-
| --- | --- | --- |
|
|
1677
|
-
| `"any"` | `(?s:.)` | Matches any single character, including newlines |
|
|
1678
|
-
| `"non-newline"` | `.` | Matches any single character except newlines |
|
|
1679
|
-
| `"start"` | `^` | Start of string anchor |
|
|
1680
|
-
| `"line-start"` | `^` (with `m` flag) | Start of line anchor |
|
|
1681
|
-
| `"end"` | `$` | End of string anchor |
|
|
1682
|
-
| `"line-end"` | `$` (with `m` flag) | End of line anchor |
|
|
1683
|
-
| `"word-bound"` | `\b` | Word boundary |
|
|
1684
|
-
| `"non-word-bound"` | `\B` | Non-word boundary |
|
|
1685
|
-
| `"word-bound-start"` | `(?<=\W)(?=\w)` | Start of a word |
|
|
1686
|
-
| `"word-bound-end"` | `(?<=\w)(?=\W)` | End of a word |
|
|
1687
|
-
|
|
1688
|
-
### Character Sets
|
|
1689
|
-
| Name | Resolves To | Description |
|
|
1690
|
-
| --- | --- | --- |
|
|
1691
|
-
| `"letter"` | `[a-zA-Z]` | Any letter (uppercase or lowercase) |
|
|
1692
|
-
| `"lowercase-letter"` | `[a-z]` | Any lowercase letter |
|
|
1693
|
-
| `"uppercase-letter"` | `[A-Z]` | Any uppercase letter |
|
|
1694
|
-
| `"non-letter"` | `[^a-zA-Z]` | Any character that is not a letter |
|
|
1695
|
-
| `"alphanumeric"` | `[a-zA-Z0-9]` | Any letter or digit |
|
|
1696
|
-
| `"non-alphanumeric"` | `[^a-zA-Z0-9]` | Any character that is not a letter or digit |
|
|
1697
|
-
|
|
1698
|
-
### Predefined Character Sets
|
|
1699
|
-
| Name | Resolves To | Description |
|
|
1700
|
-
| --- | --- | --- |
|
|
1701
|
-
| `"digit"` | `\d` | Any digit |
|
|
1702
|
-
| `"non-digit"` | `\D` | Any non-digit |
|
|
1703
|
-
| `"whitespace"` | `\s` | Any whitespace character |
|
|
1704
|
-
| `"non-whitespace"` | `\S` | Any non-whitespace character |
|
|
1705
|
-
| `"vertical-whitespace"` | `\v` | Vertical whitespace character |
|
|
1706
|
-
| `"word-char"` | `\w` | Any word character (letter, digit, or underscore) |
|
|
1707
|
-
| `"non-word-char"` | `\W` | Any non-word character |
|
|
1708
|
-
| `"backspace"` | `[\b]` | Backspace character |
|
|
6
|
+
**Note**: This library is tested with nearly 100% coverage, but any override of `RGXClassToken.clone()` does not have the depth parameter fully tested, as that is ultimately part of `@ptolemy2002/immutability-utils`, which is tested, and setting up tests for that functionality is exceedingly complex.
|
|
1709
7
|
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
8
|
+
# Table of Contents
|
|
9
|
+
Because there is so much to document, it has been broken up into multiple files. The following is a list of the documentation files for this library:
|
|
10
|
+
- [type-reference](./docs/type-reference.md) - A reference for all public types used in the library.
|
|
11
|
+
- [general](./docs/general.md) - General utilities of the library, including tagged template functions for creating regular expressions and walkers.
|
|
12
|
+
- [type-guards](./docs/type-guards.md) - Type guards for validating various types of tokens and string values.
|
|
13
|
+
- [ExtRegExp](./docs/ExtRegExp.md) - The `ExtRegExp` class, which extends the built-in `RegExp` class with the ability to create custom flags that transform the source string of the regular expression before it is compiled.
|
|
14
|
+
- [constants](./docs/constants.md) - Constants provided by the library, such as predefined character classes and tokens.
|
|
15
|
+
- `util` - A directory containing documentation for various utility functions provided by the library.
|
|
16
|
+
- [clone](./docs/util/clone.md) - The `cloneRGXToken` function, which creates a clone of a given RGX token to a specified depth.
|
|
17
|
+
- [escapeRegex](./docs/util/escapeRegex.md) - The `escapeRegex` function, which escapes special regex characters in a given string and assures you that the result is valid Regex.
|
|
18
|
+
- [regexMatchAtPosition](./docs/util/regexMatchAtPosition.md) - The `regexMatchAtPosition` function and related functions, which attempt to match a given regular expression at a specific position in a string.
|
|
19
|
+
- [regexWithFlags](./docs/util/regexWithFlags.md) - The `regexWithFlags` function, which creates a new regular expression with the same source as a given regular expression but with different flags.
|
|
20
|
+
- `class` - A directory containing documentation for all classes in the library.
|
|
21
|
+
- [collection](./docs/class/collection.md) - The `RGXTokenCollection` class, which is a collection of tokens.
|
|
22
|
+
- [RGXError](./docs/class/RGXError.md) - Details on all custom error classes and the base `RGXError` class.
|
|
23
|
+
- [walker](./docs/class/walker.md) - Details on both `RGXWalker` and `RGXPart`, which are used for creating custom matchers that can validate partial matches and transform captured values with custom logic.
|
|
24
|
+
- `token` - A directory containing documentation for all token classes in the library, which are classes that represent specific types of tokens that can be used in regular expressions.
|
|
25
|
+
- [base](./docs/class/token/base.md) - The `RGXClassToken` class, which is the base class for all token classes in the library.
|
|
26
|
+
- [group](./docs/class/token/group.md) - The `RGXGroupToken` class, which represents a group of tokens that can be treated as a single unit in a regular expression.
|
|
27
|
+
- [repeat](./docs/class/token/repeat.md) - The `RGXRepeatToken` class, which represents a token that can be repeated a range of times in a regular expression. This also covers optional syntax.
|
|
28
|
+
- [union](./docs/class/token/union.md) - The `RGXClassUnionToken` class, which represents a union of tokens that can match any one of the tokens in the union.
|
|
29
|
+
- [subpattern](./docs/class/token/subpattern.md) - The `RGXSubpatternToken` class, which re-matching the content of a previous capturing group.
|
|
30
|
+
- [wrapper](./docs/class/token/wrapper.md) - The `RGXClassWrapperToken` class, which represents a class token that can wrap any arbitrary token (even non-class), giving you the benefit of the class API for any token.
|
|
31
|
+
- [to](./docs/class/token/to.md) - A utility for converting any token into a class token. Most tokens just get wrapped in a `RGXClassWrapperToken`, but some do not.
|
|
32
|
+
- `lookaround` - A directory containing documentation for lookahead and lookbehind tokens.
|
|
33
|
+
- [base](./docs/class/token/lookaround/base.md) - The `RGXLookaroundToken` class, which is the base class for lookahead and lookbehind tokens.
|
|
34
|
+
- [lookahead](./docs/class/token/lookaround/lookahead.md) - The `RGXLookaheadToken` class, which represents a lookahead assertion in a regular expression.
|
|
35
|
+
- [lookbehind](./docs/class/token/lookaround/lookbehind.md) - The `RGXLookbehindToken` class, which represents a lookbehind assertion in a regular expression.
|
|
1714
36
|
|
|
1715
37
|
## Peer Dependencies
|
|
1716
38
|
- `@ptolemy2002/immutability-utils` ^2.0.0
|