@ptolemy2002/rgx 2.4.0 → 2.5.1
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 +121 -10
- package/dist/class/base.d.ts +9 -0
- package/dist/class/base.js +17 -0
- package/dist/class/index.d.ts +3 -0
- package/dist/class/index.js +19 -0
- package/dist/class/init.d.ts +1 -0
- package/dist/class/init.js +22 -0
- package/dist/class/union.d.ts +16 -0
- package/dist/class/union.js +89 -0
- package/dist/collection.d.ts +6 -5
- package/dist/collection.js +18 -5
- package/dist/concat.d.ts +2 -0
- package/dist/concat.js +8 -0
- package/dist/errors/base.d.ts +1 -1
- package/dist/errors/index.d.ts +1 -0
- package/dist/errors/index.js +1 -0
- package/dist/errors/notImplemented.d.ts +6 -0
- package/dist/errors/notImplemented.js +19 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +17 -41
- package/dist/resolve.d.ts +3 -0
- package/dist/resolve.js +76 -0
- package/dist/typeGuards.d.ts +2 -1
- package/dist/typeGuards.js +12 -1
- package/dist/types.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,11 +23,11 @@ type ValidVanillaRegexFlags = Branded<string, [ValidVanillaRegexFlagsBrandSymbol
|
|
|
23
23
|
|
|
24
24
|
type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | RGXTokenType[];
|
|
25
25
|
type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
|
|
26
|
-
type RGXTokenFromType<T extends RGXTokenType> =
|
|
26
|
+
type RGXTokenFromType<T extends RGXTokenType | RGXTokenTypeFlat> =
|
|
27
27
|
// ... see source for full definition
|
|
28
28
|
;
|
|
29
29
|
|
|
30
|
-
type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_VANILLA_REGEX_FLAGS';
|
|
30
|
+
type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_VANILLA_REGEX_FLAGS' | 'NOT_IMPLEMENTED';
|
|
31
31
|
type ExpectedTokenType = {
|
|
32
32
|
type: "tokenType";
|
|
33
33
|
values: RGXTokenTypeFlat[];
|
|
@@ -36,6 +36,9 @@ type ExpectedTokenType = {
|
|
|
36
36
|
values: string[];
|
|
37
37
|
};
|
|
38
38
|
type RGXTokenCollectionMode = 'union' | 'concat';
|
|
39
|
+
type RGXTokenCollectionInput = RGXToken | RGXTokenCollection;
|
|
40
|
+
|
|
41
|
+
type RGXUnionInsertionPosition = 'prefix' | 'suffix';
|
|
39
42
|
```
|
|
40
43
|
|
|
41
44
|
## Classes
|
|
@@ -95,22 +98,38 @@ constructor(message: string, got: string)
|
|
|
95
98
|
#### Properties
|
|
96
99
|
- `got` (`string`): The actual string that was received, which failed validation.
|
|
97
100
|
|
|
101
|
+
### RGXNotImplementedError extends RGXError
|
|
102
|
+
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.
|
|
103
|
+
|
|
104
|
+
#### Constructor
|
|
105
|
+
```typescript
|
|
106
|
+
constructor(functionality: string, message?: string | null)
|
|
107
|
+
```
|
|
108
|
+
- `functionality` (`string`): A description of the functionality that is not yet implemented.
|
|
109
|
+
- `message` (`string | null`, optional): An optional additional message providing more context. Defaults to `null`.
|
|
110
|
+
|
|
111
|
+
#### Properties
|
|
112
|
+
- `functionality` (`string`): The description of the unimplemented functionality.
|
|
113
|
+
|
|
114
|
+
#### Methods
|
|
115
|
+
- `toString()` (`() => string`): Returns a formatted string indicating the unimplemented functionality and any additional message.
|
|
116
|
+
|
|
98
117
|
### RGXTokenCollection
|
|
99
|
-
A class representing a collection of RGX tokens. This
|
|
118
|
+
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.
|
|
100
119
|
|
|
101
120
|
A function `rgxTokenCollection` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
|
|
102
121
|
|
|
103
122
|
#### Constructor
|
|
104
123
|
```typescript
|
|
105
|
-
constructor(tokens:
|
|
124
|
+
constructor(tokens: RGXTokenCollectionInput = [], mode: RGXTokenCollectionMode = 'concat')
|
|
106
125
|
```
|
|
107
|
-
- `tokens` (`
|
|
126
|
+
- `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.
|
|
108
127
|
- `mode` (`RGXTokenCollectionMode`, optional): The mode of the collection, either 'union' or 'concat'. Defaults to 'concat'.
|
|
109
128
|
|
|
110
129
|
#### Properties
|
|
111
130
|
- `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.
|
|
112
131
|
- `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.
|
|
113
|
-
- `toRgx()` (`() =>
|
|
132
|
+
- `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.
|
|
114
133
|
- `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.
|
|
115
134
|
- `clone()` (`() => 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.
|
|
116
135
|
- `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.
|
|
@@ -118,6 +137,40 @@ constructor(tokens: RGXToken[] = [], mode: RGXTokenCollectionMode = 'concat')
|
|
|
118
137
|
|
|
119
138
|
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[]`).
|
|
120
139
|
|
|
140
|
+
### RGXClassToken (abstract)
|
|
141
|
+
An abstract base class for creating custom RGX token classes. Subclasses must implement the `toRgx()` method, which returns a value compatible with `RGXConvertibleTokenOutput`.
|
|
142
|
+
|
|
143
|
+
#### Abstract Methods
|
|
144
|
+
- `toRgx()` (`() => RGXConvertibleTokenOutput`): Must be implemented by subclasses to return the token's regex representation as a native/literal token or array of native/literal tokens.
|
|
145
|
+
|
|
146
|
+
#### Properties
|
|
147
|
+
- `isGroup` (`boolean`): Returns `false` by default. Subclasses can override this to indicate whether the token represents a group.
|
|
148
|
+
|
|
149
|
+
#### Methods
|
|
150
|
+
- `or(...others: RGXTokenCollectionInput[])` (`(...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.
|
|
151
|
+
- `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`.
|
|
152
|
+
|
|
153
|
+
### RGXClassUnionToken extends RGXClassToken
|
|
154
|
+
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.
|
|
155
|
+
|
|
156
|
+
A function `rgxClassUnion` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
|
|
157
|
+
|
|
158
|
+
#### Constructor
|
|
159
|
+
```typescript
|
|
160
|
+
constructor(tokens: RGXTokenCollectionInput = [])
|
|
161
|
+
```
|
|
162
|
+
- `tokens` (`RGXTokenCollectionInput`, optional): The tokens to include in the union. Internally stored as an `RGXTokenCollection` in 'union' mode. Defaults to an empty array.
|
|
163
|
+
|
|
164
|
+
#### Properties
|
|
165
|
+
- `tokens` (`RGXTokenCollection`): The internal collection of tokens managed in 'union' mode.
|
|
166
|
+
- `isGroup` (`boolean`): Returns `true`, indicating this token represents a group.
|
|
167
|
+
|
|
168
|
+
#### Methods
|
|
169
|
+
- `add(token: RGXToken, pos?: RGXUnionInsertionPosition)` (`(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.
|
|
170
|
+
- `concat(pos?: RGXUnionInsertionPosition, ...others: RGXTokenCollectionInput[])` (`(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.
|
|
171
|
+
- `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`.
|
|
172
|
+
- `toRgx()` (`() => RegExp`): Resolves the union by calling `toRgx()` on the internal `RGXTokenCollection`, returning a `RegExp`.
|
|
173
|
+
|
|
121
174
|
## Functions
|
|
122
175
|
The following functions are exported by the library:
|
|
123
176
|
|
|
@@ -249,9 +302,32 @@ if (type === 'native') {
|
|
|
249
302
|
#### Returns
|
|
250
303
|
- `RGXTokenType`: The type of the RGX token.
|
|
251
304
|
|
|
305
|
+
### rgxTokenTypeFlat
|
|
306
|
+
```typescript
|
|
307
|
+
function rgxTokenTypeFlat(value: RGXToken): RGXTokenTypeFlat
|
|
308
|
+
```
|
|
309
|
+
Determines the flat type of a given RGX token (`no-op`, `literal`, `native`, `convertible`, or `array`). The `array` type represents any array of RGX tokens, regardless of the types of the individual tokens within the array.
|
|
310
|
+
|
|
311
|
+
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.
|
|
312
|
+
|
|
313
|
+
```typescript
|
|
314
|
+
const token: RGXToken = ...;
|
|
315
|
+
const type = rgxTokenTypeFlat(token);
|
|
316
|
+
if (type === 'array') {
|
|
317
|
+
const narrowedToken1 = token as RGXTokenFromType<typeof type>; // narrowedToken is RGXToken[]
|
|
318
|
+
const narrowedToken2 = rgxTokenFromType(type, token); // same as above
|
|
319
|
+
}
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
#### Parameters
|
|
323
|
+
- `value` (`RGXToken`): The RGX token to check.
|
|
324
|
+
|
|
325
|
+
#### Returns
|
|
326
|
+
- `RGXTokenTypeFlat`: The flat type of the RGX token.
|
|
327
|
+
|
|
252
328
|
### rgxTokenFromType
|
|
253
329
|
```typescript
|
|
254
|
-
function rgxTokenFromType<T extends RGXTokenType>(type: T, value: RGXToken): RGXTokenFromType<T>
|
|
330
|
+
function rgxTokenFromType<T extends RGXTokenType | RGXTokenTypeFlat>(type: T, value: RGXToken): RGXTokenFromType<T>
|
|
255
331
|
```
|
|
256
332
|
|
|
257
333
|
Does nothing at runtime, but performs a type assertion to the correct subset of `RGXToken` based on the provided `RGXTokenType`.
|
|
@@ -328,26 +404,28 @@ Escapes special regex characters in the given string and brands the result as a
|
|
|
328
404
|
|
|
329
405
|
### resolveRGXToken
|
|
330
406
|
```typescript
|
|
331
|
-
function resolveRGXToken(token: RGXToken): ValidRegexString
|
|
407
|
+
function resolveRGXToken(token: RGXToken, groupWrap?: boolean): ValidRegexString
|
|
332
408
|
```
|
|
333
409
|
|
|
334
|
-
Resolves an RGX token to a string. No-op tokens resolve to an empty string, literal tokens are included as-is (wrapped in a non-capturing group), native tokens are converted to strings and escaped, convertible tokens are converted using their `toRgx` method and then resolved recursively, and arrays of tokens are resolved as unions of their resolved elements (placed in a non-capturing group).
|
|
410
|
+
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`).
|
|
335
411
|
|
|
336
412
|
#### Parameters
|
|
337
413
|
- `token` (`RGXToken`): The RGX token to resolve.
|
|
414
|
+
- `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. The `groupWrap` preference is passed through to recursive calls for convertible tokens, but array union elements always use `groupWrap=true` internally.
|
|
338
415
|
|
|
339
416
|
#### Returns
|
|
340
417
|
- `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.
|
|
341
418
|
|
|
342
419
|
### rgxConcat
|
|
343
420
|
```typescript
|
|
344
|
-
function rgxConcat(tokens: RGXToken[]): ValidRegexString
|
|
421
|
+
function rgxConcat(tokens: RGXToken[], groupWrap?: boolean): ValidRegexString
|
|
345
422
|
```
|
|
346
423
|
|
|
347
424
|
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.
|
|
348
425
|
|
|
349
426
|
#### Parameters
|
|
350
427
|
- `tokens` (`RGXToken[]`): The array of RGX tokens to resolve and concatenate.
|
|
428
|
+
- `groupWrap` (`boolean`, optional): Whether to wrap individual resolved tokens in non-capturing groups. Passed through to `resolveRGXToken`. Defaults to `true`.
|
|
351
429
|
|
|
352
430
|
#### Returns
|
|
353
431
|
- `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.
|
|
@@ -396,6 +474,39 @@ As an alternative to using the `rgx` template tag, you can directly call `rgxa`
|
|
|
396
474
|
#### Returns
|
|
397
475
|
- `RegExp`: A `RegExp` object constructed from the resolved tokens and the provided flags.
|
|
398
476
|
|
|
477
|
+
### expandRgxUnionTokens
|
|
478
|
+
```typescript
|
|
479
|
+
function expandRgxUnionTokens(...tokens: RGXTokenCollectionInput[]): RGXTokenCollection
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
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.
|
|
483
|
+
|
|
484
|
+
#### Parameters
|
|
485
|
+
- `tokens` (`...RGXTokenCollectionInput[]`): The tokens to expand.
|
|
486
|
+
|
|
487
|
+
#### Returns
|
|
488
|
+
- `RGXTokenCollection`: A flat collection containing all expanded tokens.
|
|
489
|
+
|
|
490
|
+
### removeRgxUnionDuplicates
|
|
491
|
+
```typescript
|
|
492
|
+
function removeRgxUnionDuplicates(...tokens: RGXTokenCollectionInput[]): RGXTokenCollection
|
|
493
|
+
```
|
|
494
|
+
|
|
495
|
+
Removes duplicate tokens from the provided list using `Set` equality and returns a new `RGXTokenCollection` in union mode containing only the unique tokens.
|
|
496
|
+
|
|
497
|
+
#### Parameters
|
|
498
|
+
- `tokens` (`...RGXTokenCollectionInput[]`): The tokens to deduplicate.
|
|
499
|
+
|
|
500
|
+
#### Returns
|
|
501
|
+
- `RGXTokenCollection`: A union-mode collection with duplicates removed.
|
|
502
|
+
|
|
503
|
+
### rgxClassInit
|
|
504
|
+
```typescript
|
|
505
|
+
function rgxClassInit(): void
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
Initializes internal method patches required for `RGXClassToken` subclass methods (such as `or`) to work correctly. This function is called automatically when importing from the main module entry point, so you typically do not need to call it yourself. It only needs to be called manually if you import directly from sub-modules.
|
|
509
|
+
|
|
399
510
|
## Peer Dependencies
|
|
400
511
|
- `@ptolemy2002/immutability-utils` ^2.0.0
|
|
401
512
|
- `@ptolemy2002/js-utils` ^3.2.2
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { RGXConvertibleTokenOutput, ValidRegexString } from "../types";
|
|
2
|
+
import { RGXTokenCollectionInput } from "../collection";
|
|
3
|
+
import type { RGXClassUnionToken } from "./union";
|
|
4
|
+
export declare abstract class RGXClassToken {
|
|
5
|
+
abstract toRgx(): RGXConvertibleTokenOutput;
|
|
6
|
+
get isGroup(): boolean;
|
|
7
|
+
or(...others: RGXTokenCollectionInput[]): RGXClassUnionToken;
|
|
8
|
+
resolve(): ValidRegexString;
|
|
9
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RGXClassToken = void 0;
|
|
4
|
+
const errors_1 = require("../errors");
|
|
5
|
+
const resolve_1 = require("../resolve");
|
|
6
|
+
class RGXClassToken {
|
|
7
|
+
get isGroup() {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
or(...others) {
|
|
11
|
+
throw new errors_1.RGXNotImplementedError('RGXClassToken.or(...others)', 'call rgxClassInit() first.');
|
|
12
|
+
}
|
|
13
|
+
resolve() {
|
|
14
|
+
return (0, resolve_1.resolveRGXToken)(this);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.RGXClassToken = RGXClassToken;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./init"), exports);
|
|
18
|
+
__exportStar(require("./base"), exports);
|
|
19
|
+
__exportStar(require("./union"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function rgxClassInit(): void;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.rgxClassInit = rgxClassInit;
|
|
4
|
+
const base_1 = require("./base");
|
|
5
|
+
const union_1 = require("./union");
|
|
6
|
+
function rgxClassInit() {
|
|
7
|
+
// Patch RGXClassToken here, Since classes like RGXClassUnionToken are instances of RGXClassToken
|
|
8
|
+
// themselves. If we tried to import RGXClassUnionToken in base.ts, it would cause a circular dependency.
|
|
9
|
+
base_1.RGXClassToken.prototype.or = function (...others) {
|
|
10
|
+
if (others.length === 0)
|
|
11
|
+
return new union_1.RGXClassUnionToken([this]);
|
|
12
|
+
const expandedOthers = (0, union_1.expandRgxUnionTokens)(...others);
|
|
13
|
+
// Remove any instances of this token itself
|
|
14
|
+
const filteredOthers = expandedOthers.tokens.filter(token => token !== this);
|
|
15
|
+
if (this instanceof union_1.RGXClassUnionToken) {
|
|
16
|
+
return new union_1.RGXClassUnionToken([...this.tokens, ...filteredOthers]);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return new union_1.RGXClassUnionToken([this, ...filteredOthers]);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { RGXToken } from "../types";
|
|
2
|
+
import { RGXTokenCollection, RGXTokenCollectionInput } from "../collection";
|
|
3
|
+
import { RGXClassToken } from "./base";
|
|
4
|
+
export type RGXUnionInsertionPosition = 'prefix' | 'suffix';
|
|
5
|
+
export declare class RGXClassUnionToken extends RGXClassToken {
|
|
6
|
+
tokens: RGXTokenCollection;
|
|
7
|
+
get isGroup(): boolean;
|
|
8
|
+
constructor(tokens?: RGXTokenCollectionInput);
|
|
9
|
+
cleanTokens(): this;
|
|
10
|
+
add(token: RGXToken, pos?: RGXUnionInsertionPosition): this;
|
|
11
|
+
concat(pos?: RGXUnionInsertionPosition, ...others: RGXTokenCollectionInput[]): this;
|
|
12
|
+
toRgx(): RegExp;
|
|
13
|
+
}
|
|
14
|
+
export declare function expandRgxUnionTokens(...tokens: RGXTokenCollectionInput[]): RGXTokenCollection;
|
|
15
|
+
export declare function removeRgxUnionDuplicates(...tokens: RGXTokenCollectionInput[]): RGXTokenCollection;
|
|
16
|
+
export declare const rgxClassUnion: (tokens?: RGXTokenCollectionInput) => RGXClassUnionToken;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.rgxClassUnion = exports.RGXClassUnionToken = void 0;
|
|
4
|
+
exports.expandRgxUnionTokens = expandRgxUnionTokens;
|
|
5
|
+
exports.removeRgxUnionDuplicates = removeRgxUnionDuplicates;
|
|
6
|
+
const internal_1 = require("../internal");
|
|
7
|
+
const collection_1 = require("../collection");
|
|
8
|
+
const base_1 = require("./base");
|
|
9
|
+
class RGXClassUnionToken extends base_1.RGXClassToken {
|
|
10
|
+
get isGroup() {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
constructor(tokens = []) {
|
|
14
|
+
super();
|
|
15
|
+
if (tokens instanceof collection_1.RGXTokenCollection && tokens.mode === 'concat')
|
|
16
|
+
this.tokens = new collection_1.RGXTokenCollection([tokens], 'union');
|
|
17
|
+
else
|
|
18
|
+
this.tokens = new collection_1.RGXTokenCollection(tokens, 'union');
|
|
19
|
+
this.cleanTokens();
|
|
20
|
+
}
|
|
21
|
+
cleanTokens() {
|
|
22
|
+
this.tokens = removeRgxUnionDuplicates(...expandRgxUnionTokens(...this.tokens));
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
add(token, pos = 'suffix') {
|
|
26
|
+
if (token instanceof collection_1.RGXTokenCollection && token.mode === 'union')
|
|
27
|
+
return this.concat(pos, ...token);
|
|
28
|
+
if (token instanceof RGXClassUnionToken)
|
|
29
|
+
return this.concat(pos, ...token.tokens);
|
|
30
|
+
if (pos === 'prefix') {
|
|
31
|
+
this.tokens.unshift(token);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
this.tokens.push(token);
|
|
35
|
+
}
|
|
36
|
+
return this.cleanTokens();
|
|
37
|
+
}
|
|
38
|
+
concat(pos = 'suffix', ...others) {
|
|
39
|
+
if (pos === 'suffix') {
|
|
40
|
+
this.tokens = this.tokens.clone().concat(...others);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
this.tokens = new collection_1.RGXTokenCollection([...others, ...this.tokens], 'union');
|
|
44
|
+
}
|
|
45
|
+
return this.cleanTokens();
|
|
46
|
+
}
|
|
47
|
+
toRgx() {
|
|
48
|
+
return this.tokens.toRgx();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.RGXClassUnionToken = RGXClassUnionToken;
|
|
52
|
+
function expandRgxUnionTokens(...tokens) {
|
|
53
|
+
const result = new collection_1.RGXTokenCollection();
|
|
54
|
+
for (const token of tokens) {
|
|
55
|
+
if (token instanceof collection_1.RGXTokenCollection && token.mode === 'union') {
|
|
56
|
+
result.push(...expandRgxUnionTokens(...token));
|
|
57
|
+
}
|
|
58
|
+
else if (Array.isArray(token)) {
|
|
59
|
+
result.push(...expandRgxUnionTokens(...token));
|
|
60
|
+
}
|
|
61
|
+
else if (token instanceof RGXClassUnionToken) {
|
|
62
|
+
result.push(...expandRgxUnionTokens(...token.tokens));
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
result.push(token);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
function removeRgxUnionDuplicates(...tokens) {
|
|
71
|
+
let uniqueTokens = [...new Set(tokens)];
|
|
72
|
+
// Handle RegExp objects separately since they are not considered equal even if they have the same pattern and flags
|
|
73
|
+
const seenRegexes = new Set();
|
|
74
|
+
uniqueTokens = uniqueTokens.filter(token => {
|
|
75
|
+
if (token instanceof RegExp) {
|
|
76
|
+
const regexString = token.toString();
|
|
77
|
+
if (seenRegexes.has(regexString)) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
seenRegexes.add(regexString);
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return true;
|
|
86
|
+
});
|
|
87
|
+
return new collection_1.RGXTokenCollection(uniqueTokens, 'union');
|
|
88
|
+
}
|
|
89
|
+
exports.rgxClassUnion = (0, internal_1.createConstructFunction)(RGXClassUnionToken);
|
package/dist/collection.d.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { RGXToken
|
|
1
|
+
import { RGXToken } from "./types";
|
|
2
2
|
import { CloneDepth } from "@ptolemy2002/immutability-utils";
|
|
3
3
|
import { Collection } from "@ptolemy2002/ts-utils";
|
|
4
4
|
export type RGXTokenCollectionMode = 'union' | 'concat';
|
|
5
|
+
export type RGXTokenCollectionInput = RGXToken | RGXTokenCollection;
|
|
5
6
|
export declare class RGXTokenCollection implements Collection<RGXToken> {
|
|
6
7
|
mode: RGXTokenCollectionMode;
|
|
7
8
|
tokens: RGXToken[];
|
|
8
|
-
constructor(tokens?:
|
|
9
|
-
toRgx():
|
|
9
|
+
constructor(tokens?: RGXTokenCollectionInput, mode?: RGXTokenCollectionMode);
|
|
10
|
+
toRgx(): RegExp;
|
|
10
11
|
getTokens(): RGXToken[];
|
|
11
12
|
clone(depth?: CloneDepth): RGXTokenCollection;
|
|
12
13
|
asConcat(): RGXTokenCollection;
|
|
@@ -28,7 +29,7 @@ export declare class RGXTokenCollection implements Collection<RGXToken> {
|
|
|
28
29
|
flat(depth?: number): RGXTokenCollection;
|
|
29
30
|
flatMap<T>(callback: (token: RGXToken, index: number, array: RGXTokenCollection) => T | T[], depth?: number): T[];
|
|
30
31
|
slice(start?: number, end?: number): RGXTokenCollection;
|
|
31
|
-
concat(...others: (RGXToken |
|
|
32
|
+
concat(...others: (RGXToken | RGXTokenCollection)[]): RGXTokenCollection;
|
|
32
33
|
push(...tokens: RGXToken[]): void;
|
|
33
34
|
pop(): RGXToken | undefined;
|
|
34
35
|
shift(): RGXToken | undefined;
|
|
@@ -42,4 +43,4 @@ export declare class RGXTokenCollection implements Collection<RGXToken> {
|
|
|
42
43
|
keys(): IterableIterator<number>;
|
|
43
44
|
values(): IterableIterator<RGXToken>;
|
|
44
45
|
}
|
|
45
|
-
export declare const rgxTokenCollection: (tokens?:
|
|
46
|
+
export declare const rgxTokenCollection: (tokens?: RGXTokenCollectionInput, mode?: RGXTokenCollectionMode | undefined) => RGXTokenCollection;
|
package/dist/collection.js
CHANGED
|
@@ -1,22 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.rgxTokenCollection = exports.RGXTokenCollection = void 0;
|
|
4
|
-
const
|
|
4
|
+
const resolve_1 = require("./resolve");
|
|
5
|
+
const concat_1 = require("./concat");
|
|
5
6
|
const immutability_utils_1 = require("@ptolemy2002/immutability-utils");
|
|
6
7
|
const internal_1 = require("./internal");
|
|
7
8
|
class RGXTokenCollection {
|
|
8
9
|
constructor(tokens = [], mode = 'concat') {
|
|
9
10
|
this.tokens = [];
|
|
10
|
-
|
|
11
|
+
if (tokens instanceof RGXTokenCollection) {
|
|
12
|
+
this.tokens = tokens.tokens;
|
|
13
|
+
}
|
|
14
|
+
else if (Array.isArray(tokens)) {
|
|
15
|
+
this.tokens = tokens;
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
this.tokens = [tokens];
|
|
19
|
+
}
|
|
11
20
|
this.mode = mode;
|
|
12
21
|
}
|
|
13
22
|
toRgx() {
|
|
23
|
+
let pattern;
|
|
14
24
|
if (this.mode === 'union') {
|
|
15
|
-
|
|
25
|
+
// The RegExp will already be wrapped with resolveRGXToken,
|
|
26
|
+
// so we don't need to wrap it again here.
|
|
27
|
+
pattern = (0, resolve_1.resolveRGXToken)(this.tokens, false);
|
|
16
28
|
}
|
|
17
29
|
else {
|
|
18
|
-
|
|
30
|
+
pattern = (0, concat_1.rgxConcat)(this.tokens);
|
|
19
31
|
}
|
|
32
|
+
return new RegExp(pattern);
|
|
20
33
|
}
|
|
21
34
|
getTokens() {
|
|
22
35
|
return (0, immutability_utils_1.extClone)(this.tokens, "max");
|
|
@@ -101,7 +114,7 @@ class RGXTokenCollection {
|
|
|
101
114
|
}
|
|
102
115
|
concat(...others) {
|
|
103
116
|
return (0, immutability_utils_1.immutableMut)(this, clone => {
|
|
104
|
-
const arrays = others.map(o => o instanceof RGXTokenCollection ? o.tokens : Array.isArray(o) ? o : [o]);
|
|
117
|
+
const arrays = others.map(o => o instanceof RGXTokenCollection && o.mode === this.mode ? o.tokens : Array.isArray(o) ? o : [o]);
|
|
105
118
|
clone.tokens = clone.tokens.concat(...arrays.flat());
|
|
106
119
|
});
|
|
107
120
|
}
|
package/dist/concat.d.ts
ADDED
package/dist/concat.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.rgxConcat = rgxConcat;
|
|
4
|
+
const resolve_1 = require("./resolve");
|
|
5
|
+
// Wrapper for letting an array of tokens be resolved as a concatenation instead of a union.
|
|
6
|
+
function rgxConcat(tokens, groupWrap = true) {
|
|
7
|
+
return tokens.map(t => (0, resolve_1.resolveRGXToken)(t, groupWrap)).join('');
|
|
8
|
+
}
|
package/dist/errors/base.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_VANILLA_REGEX_FLAGS';
|
|
1
|
+
export type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_VANILLA_REGEX_FLAGS' | 'NOT_IMPLEMENTED';
|
|
2
2
|
export declare class RGXError extends Error {
|
|
3
3
|
code: RGXErrorCode;
|
|
4
4
|
constructor(message: string, code?: RGXErrorCode);
|
package/dist/errors/index.d.ts
CHANGED
package/dist/errors/index.js
CHANGED
|
@@ -18,3 +18,4 @@ __exportStar(require("./base"), exports);
|
|
|
18
18
|
__exportStar(require("./invalidToken"), exports);
|
|
19
19
|
__exportStar(require("./invalidRegexString"), exports);
|
|
20
20
|
__exportStar(require("./invalidVanillaRegexFlags"), exports);
|
|
21
|
+
__exportStar(require("./notImplemented"), exports);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RGXNotImplementedError = void 0;
|
|
4
|
+
const base_1 = require("./base");
|
|
5
|
+
class RGXNotImplementedError extends base_1.RGXError {
|
|
6
|
+
constructor(functionality, message = null) {
|
|
7
|
+
super(message || "", "NOT_IMPLEMENTED");
|
|
8
|
+
this.functionality = functionality;
|
|
9
|
+
this.name = "RGXNotImplementedError";
|
|
10
|
+
}
|
|
11
|
+
toString() {
|
|
12
|
+
const result = `${this.name}: ${this.functionality} is not implemented yet.`;
|
|
13
|
+
if (this.message)
|
|
14
|
+
return result + ` Additional info: ${this.message}`;
|
|
15
|
+
else
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.RGXNotImplementedError = RGXNotImplementedError;
|
package/dist/index.d.ts
CHANGED
|
@@ -3,8 +3,8 @@ export * from "./errors";
|
|
|
3
3
|
export * from "./types";
|
|
4
4
|
export * from "./typeGuards";
|
|
5
5
|
export * from "./collection";
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export
|
|
6
|
+
export * from "./class";
|
|
7
|
+
export * from "./resolve";
|
|
8
|
+
export * from "./concat";
|
|
9
9
|
export declare function rgxa(tokens: t.RGXToken[], flags?: string): RegExp;
|
|
10
10
|
export default function rgx(flags?: string): (strings: TemplateStringsArray, ...tokens: t.RGXToken[]) => RegExp;
|
package/dist/index.js
CHANGED
|
@@ -36,62 +36,38 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
36
36
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.escapeRegex = escapeRegex;
|
|
40
|
-
exports.resolveRGXToken = resolveRGXToken;
|
|
41
|
-
exports.rgxConcat = rgxConcat;
|
|
42
39
|
exports.rgxa = rgxa;
|
|
43
40
|
exports.default = rgx;
|
|
44
|
-
const e = __importStar(require("./errors"));
|
|
45
41
|
const tg = __importStar(require("./typeGuards"));
|
|
42
|
+
const class_1 = require("./class");
|
|
43
|
+
const concat_1 = require("./concat");
|
|
46
44
|
__exportStar(require("./errors"), exports);
|
|
47
45
|
__exportStar(require("./types"), exports);
|
|
48
46
|
__exportStar(require("./typeGuards"), exports);
|
|
49
47
|
__exportStar(require("./collection"), exports);
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
// Interpret arrays as unions
|
|
64
|
-
if (Array.isArray(token)) {
|
|
65
|
-
if (token.length === 0)
|
|
66
|
-
return '';
|
|
67
|
-
if (token.length > 1) {
|
|
68
|
-
return '(?:' + token.map(resolveRGXToken).join('|') + ')';
|
|
69
|
-
}
|
|
70
|
-
return resolveRGXToken(token[0]);
|
|
48
|
+
__exportStar(require("./class"), exports);
|
|
49
|
+
__exportStar(require("./resolve"), exports);
|
|
50
|
+
__exportStar(require("./concat"), exports);
|
|
51
|
+
// Call this for certain class methods to work correctly
|
|
52
|
+
(0, class_1.rgxClassInit)();
|
|
53
|
+
function taggedTemplateToTokenArray(strings, tokens) {
|
|
54
|
+
const tokenArray = [];
|
|
55
|
+
for (let i = 0; i < strings.length; i++) {
|
|
56
|
+
if (strings[i])
|
|
57
|
+
tokenArray.push(strings[i]);
|
|
58
|
+
if (i < tokens.length)
|
|
59
|
+
tokenArray.push(tokens[i]);
|
|
71
60
|
}
|
|
72
|
-
|
|
73
|
-
/* istanbul ignore next */
|
|
74
|
-
throw new e.RGXInvalidTokenError(`Invalid RGX token: ${token}`, null, token);
|
|
75
|
-
}
|
|
76
|
-
// Wrapper for letting an array of tokens be resolved as a concatenation instead of a union.
|
|
77
|
-
function rgxConcat(tokens) {
|
|
78
|
-
return tokens.map(resolveRGXToken).join('');
|
|
61
|
+
return tokenArray;
|
|
79
62
|
}
|
|
80
63
|
function rgxa(tokens, flags = '') {
|
|
81
64
|
tg.assertValidVanillaRegexFlags(flags);
|
|
82
|
-
const pattern = rgxConcat(tokens);
|
|
65
|
+
const pattern = (0, concat_1.rgxConcat)(tokens);
|
|
83
66
|
return new RegExp(pattern, flags);
|
|
84
67
|
}
|
|
85
68
|
function rgx(flags = '') {
|
|
86
69
|
tg.assertValidVanillaRegexFlags(flags);
|
|
87
70
|
return (strings, ...tokens) => {
|
|
88
|
-
|
|
89
|
-
for (let i = 0; i < strings.length; i++) {
|
|
90
|
-
if (strings[i])
|
|
91
|
-
tokenArray.push(strings[i]);
|
|
92
|
-
if (i < tokens.length)
|
|
93
|
-
tokenArray.push(tokens[i]);
|
|
94
|
-
}
|
|
95
|
-
return rgxa(tokenArray, flags);
|
|
71
|
+
return rgxa(taggedTemplateToTokenArray(strings, tokens), flags);
|
|
96
72
|
};
|
|
97
73
|
}
|
package/dist/resolve.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.escapeRegex = escapeRegex;
|
|
37
|
+
exports.resolveRGXToken = resolveRGXToken;
|
|
38
|
+
const class_1 = require("./class");
|
|
39
|
+
const e = __importStar(require("./errors"));
|
|
40
|
+
const tg = __importStar(require("./typeGuards"));
|
|
41
|
+
function escapeRegex(value) {
|
|
42
|
+
return value.replaceAll(/[\-\^\$.*+?^${}()|[\]\\]/g, '\\$&');
|
|
43
|
+
}
|
|
44
|
+
function resolveRGXToken(token, groupWrap = true) {
|
|
45
|
+
if (tg.isRGXNoOpToken(token))
|
|
46
|
+
return '';
|
|
47
|
+
if (tg.isRGXLiteralToken(token)) {
|
|
48
|
+
if (groupWrap)
|
|
49
|
+
return '(?:' + token.source + ')';
|
|
50
|
+
else
|
|
51
|
+
return token.source;
|
|
52
|
+
}
|
|
53
|
+
if (tg.isRGXNativeToken(token))
|
|
54
|
+
return escapeRegex(String(token));
|
|
55
|
+
if (tg.isRGXConvertibleToken(token)) {
|
|
56
|
+
return resolveRGXToken(token.toRgx(), groupWrap);
|
|
57
|
+
}
|
|
58
|
+
// Interpret arrays as unions
|
|
59
|
+
if (Array.isArray(token)) {
|
|
60
|
+
if (token.length === 0)
|
|
61
|
+
return '';
|
|
62
|
+
if (token.length > 1) {
|
|
63
|
+
// Remove duplicates
|
|
64
|
+
token = [...(0, class_1.removeRgxUnionDuplicates)(...token)];
|
|
65
|
+
// Don't preserve group wrapping preference for the recursive calls
|
|
66
|
+
if (groupWrap)
|
|
67
|
+
return '(?:' + token.map(t => resolveRGXToken(t, true)).join('|') + ')';
|
|
68
|
+
else
|
|
69
|
+
return token.map(t => resolveRGXToken(t, true)).join('|');
|
|
70
|
+
}
|
|
71
|
+
return resolveRGXToken(token[0]);
|
|
72
|
+
}
|
|
73
|
+
// Ignoring this line since it should be impossible to reach if the types are correct, but we need it to satisfy the return type
|
|
74
|
+
/* istanbul ignore next */
|
|
75
|
+
throw new e.RGXInvalidTokenError(`Invalid RGX token: ${token}`, null, token);
|
|
76
|
+
}
|
package/dist/typeGuards.d.ts
CHANGED
|
@@ -7,8 +7,9 @@ export declare function isRGXNativeToken(value: unknown): value is t.RGXNativeTo
|
|
|
7
7
|
export declare function assertRGXNativeToken(value: unknown): asserts value is t.RGXNativeToken;
|
|
8
8
|
export declare function isRGXConvertibleToken(value: unknown): value is t.RGXConvertibleToken;
|
|
9
9
|
export declare function assertRGXConvertibleToken(value: unknown): asserts value is t.RGXConvertibleToken;
|
|
10
|
+
export declare function rgxTokenTypeFlat(value: t.RGXToken): t.RGXTokenTypeFlat;
|
|
10
11
|
export declare function rgxTokenType(value: t.RGXToken): t.RGXTokenType;
|
|
11
|
-
export declare function rgxTokenFromType<T extends t.RGXTokenType>(type: T, value: t.RGXToken): t.RGXTokenFromType<T>;
|
|
12
|
+
export declare function rgxTokenFromType<T extends t.RGXTokenType | t.RGXTokenTypeFlat>(type: T, value: t.RGXToken): t.RGXTokenFromType<T>;
|
|
12
13
|
export declare function isValidRegexString(value: string): value is t.ValidRegexString;
|
|
13
14
|
export declare function assertValidRegexString(value: string): asserts value is t.ValidRegexString;
|
|
14
15
|
export declare function isValidVanillaRegexFlags(value: string): value is t.ValidVanillaRegexFlags;
|
package/dist/typeGuards.js
CHANGED
|
@@ -44,6 +44,7 @@ exports.isRGXNativeToken = isRGXNativeToken;
|
|
|
44
44
|
exports.assertRGXNativeToken = assertRGXNativeToken;
|
|
45
45
|
exports.isRGXConvertibleToken = isRGXConvertibleToken;
|
|
46
46
|
exports.assertRGXConvertibleToken = assertRGXConvertibleToken;
|
|
47
|
+
exports.rgxTokenTypeFlat = rgxTokenTypeFlat;
|
|
47
48
|
exports.rgxTokenType = rgxTokenType;
|
|
48
49
|
exports.rgxTokenFromType = rgxTokenFromType;
|
|
49
50
|
exports.isValidRegexString = isValidRegexString;
|
|
@@ -94,7 +95,7 @@ function assertRGXConvertibleToken(value) {
|
|
|
94
95
|
throw new e.RGXInvalidTokenError(`Invalid convertible token`, { type: "tokenType", values: ['convertible'] }, value);
|
|
95
96
|
}
|
|
96
97
|
}
|
|
97
|
-
function
|
|
98
|
+
function rgxTokenTypeFlat(value) {
|
|
98
99
|
if (isRGXNoOpToken(value))
|
|
99
100
|
return 'no-op';
|
|
100
101
|
if (isRGXLiteralToken(value))
|
|
@@ -104,6 +105,16 @@ function rgxTokenType(value) {
|
|
|
104
105
|
if (isRGXConvertibleToken(value))
|
|
105
106
|
return 'convertible';
|
|
106
107
|
if (Array.isArray(value))
|
|
108
|
+
return 'array';
|
|
109
|
+
// Ignoring this line since it should be impossible to reach if the types are correct, but we need it to satisfy the return type
|
|
110
|
+
/* istanbul ignore next */
|
|
111
|
+
throw new e.RGXInvalidTokenError("Invalid RGX token", null, value);
|
|
112
|
+
}
|
|
113
|
+
function rgxTokenType(value) {
|
|
114
|
+
const flatType = rgxTokenTypeFlat(value);
|
|
115
|
+
if (flatType !== 'array')
|
|
116
|
+
return flatType;
|
|
117
|
+
if (flatType === 'array')
|
|
107
118
|
return value.map(rgxTokenType);
|
|
108
119
|
// Ignoring this line since it should be impossible to reach if the types are correct, but we need it to satisfy the return type
|
|
109
120
|
/* istanbul ignore next */
|
package/dist/types.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export type RGXConvertibleToken = {
|
|
|
10
10
|
export type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
|
|
11
11
|
export type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | RGXTokenType[];
|
|
12
12
|
export type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
|
|
13
|
-
export type RGXTokenFromType<T extends RGXTokenType> = T extends 'no-op' ? RGXNoOpToken : T extends 'literal' ? RGXLiteralToken : T extends 'native' ? RGXNativeToken : T extends 'convertible' ? RGXConvertibleToken : T extends RGXTokenType[] ? {
|
|
13
|
+
export type RGXTokenFromType<T extends RGXTokenType | RGXTokenTypeFlat> = T extends 'no-op' ? RGXNoOpToken : T extends 'literal' ? RGXLiteralToken : T extends 'native' ? RGXNativeToken : T extends 'convertible' ? RGXConvertibleToken : T extends 'array' ? RGXToken[] : T extends RGXTokenType[] ? {
|
|
14
14
|
[K in keyof T]: T[K] extends RGXTokenType ? RGXTokenFromType<T[K]> : never;
|
|
15
15
|
} : never;
|
|
16
16
|
export declare const validRegexSymbol: unique symbol;
|