@ptolemy2002/rgx 2.5.1 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +75 -15
- package/dist/errors/invalidToken.js +2 -2
- package/dist/typeGuards.d.ts +4 -0
- package/dist/typeGuards.js +45 -0
- package/dist/types.d.ts +3 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,7 +23,8 @@ type ValidVanillaRegexFlags = Branded<string, [ValidVanillaRegexFlagsBrandSymbol
|
|
|
23
23
|
|
|
24
24
|
type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | RGXTokenType[];
|
|
25
25
|
type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
|
|
26
|
-
type
|
|
26
|
+
type RGXTokenTypeGuardInput = Exclude<RGXTokenType, RGXTokenType[]> | RGXTokenTypeFlat | null | RGXTokenTypeGuardInput[];
|
|
27
|
+
type RGXTokenFromType<T extends RGXTokenTypeGuardInput> =
|
|
27
28
|
// ... see source for full definition
|
|
28
29
|
;
|
|
29
30
|
|
|
@@ -112,7 +113,7 @@ constructor(functionality: string, message?: string | null)
|
|
|
112
113
|
- `functionality` (`string`): The description of the unimplemented functionality.
|
|
113
114
|
|
|
114
115
|
#### Methods
|
|
115
|
-
- `toString()
|
|
116
|
+
- `toString() => string`: Returns a formatted string indicating the unimplemented functionality and any additional message.
|
|
116
117
|
|
|
117
118
|
### RGXTokenCollection
|
|
118
119
|
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.
|
|
@@ -129,11 +130,12 @@ constructor(tokens: RGXTokenCollectionInput = [], mode: RGXTokenCollectionMode =
|
|
|
129
130
|
#### Properties
|
|
130
131
|
- `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.
|
|
131
132
|
- `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.
|
|
132
|
-
- `toRgx()
|
|
133
|
-
- `getTokens()
|
|
134
|
-
- `
|
|
135
|
-
- `
|
|
136
|
-
- `
|
|
133
|
+
- `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.
|
|
134
|
+
- `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.
|
|
135
|
+
- `toArray() => RGXToken[]`: An alias for `getTokens()`, provided for convenience.
|
|
136
|
+
- `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.
|
|
137
|
+
- `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.
|
|
138
|
+
- `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.
|
|
137
139
|
|
|
138
140
|
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[]`).
|
|
139
141
|
|
|
@@ -141,14 +143,14 @@ Standard array properties and methods like `length`, `push`, `pop`, etc. are imp
|
|
|
141
143
|
An abstract base class for creating custom RGX token classes. Subclasses must implement the `toRgx()` method, which returns a value compatible with `RGXConvertibleTokenOutput`.
|
|
142
144
|
|
|
143
145
|
#### Abstract Methods
|
|
144
|
-
- `toRgx()
|
|
146
|
+
- `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
147
|
|
|
146
148
|
#### Properties
|
|
147
149
|
- `isGroup` (`boolean`): Returns `false` by default. Subclasses can override this to indicate whether the token represents a group.
|
|
148
150
|
|
|
149
151
|
#### Methods
|
|
150
|
-
- `or(...others: RGXTokenCollectionInput[])
|
|
151
|
-
- `resolve()
|
|
152
|
+
- `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.
|
|
153
|
+
- `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
154
|
|
|
153
155
|
### RGXClassUnionToken extends RGXClassToken
|
|
154
156
|
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.
|
|
@@ -166,10 +168,10 @@ constructor(tokens: RGXTokenCollectionInput = [])
|
|
|
166
168
|
- `isGroup` (`boolean`): Returns `true`, indicating this token represents a group.
|
|
167
169
|
|
|
168
170
|
#### Methods
|
|
169
|
-
- `add(token: RGXToken, pos?: RGXUnionInsertionPosition)
|
|
170
|
-
- `concat(pos?: RGXUnionInsertionPosition, ...others: RGXTokenCollectionInput[])
|
|
171
|
-
- `cleanTokens()
|
|
172
|
-
- `toRgx()
|
|
171
|
+
- `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.
|
|
172
|
+
- `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.
|
|
173
|
+
- `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`.
|
|
174
|
+
- `toRgx() => RegExp`: Resolves the union by calling `toRgx()` on the internal `RGXTokenCollection`, returning a `RegExp`.
|
|
173
175
|
|
|
174
176
|
## Functions
|
|
175
177
|
The following functions are exported by the library:
|
|
@@ -339,6 +341,64 @@ Does nothing at runtime, but performs a type assertion to the correct subset of
|
|
|
339
341
|
#### Returns
|
|
340
342
|
- `RGXTokenFromType<T>`: The input value, but with its type asserted to the corresponding token type based on the provided `RGXTokenType`.
|
|
341
343
|
|
|
344
|
+
### rgxTokenTypeToFlat
|
|
345
|
+
```typescript
|
|
346
|
+
function rgxTokenTypeToFlat(type: RGXTokenType): RGXTokenTypeFlat
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
Converts an `RGXTokenType` to its flat equivalent `RGXTokenTypeFlat`. If the type is an array, it returns `'array'`; otherwise, it returns the type as-is.
|
|
350
|
+
|
|
351
|
+
#### Parameters
|
|
352
|
+
- `type` (`RGXTokenType`): The RGX token type to convert.
|
|
353
|
+
|
|
354
|
+
#### Returns
|
|
355
|
+
- `RGXTokenTypeFlat`: The flat equivalent of the provided token type.
|
|
356
|
+
|
|
357
|
+
### rgxTokenTypeGuardInputToFlat
|
|
358
|
+
```typescript
|
|
359
|
+
function rgxTokenTypeGuardInputToFlat(type: RGXTokenTypeGuardInput): RGXTokenTypeFlat | null
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
Converts an `RGXTokenTypeGuardInput` to its flat equivalent. If the type is `null`, it returns `null`; if it is an array, it returns `'array'`; otherwise, it returns the type as-is.
|
|
363
|
+
|
|
364
|
+
#### Parameters
|
|
365
|
+
- `type` (`RGXTokenTypeGuardInput`): The type guard input to convert.
|
|
366
|
+
|
|
367
|
+
#### Returns
|
|
368
|
+
- `RGXTokenTypeFlat | null`: The flat equivalent of the provided type guard input, or `null` if the input is `null`.
|
|
369
|
+
|
|
370
|
+
### isRGXToken
|
|
371
|
+
```typescript
|
|
372
|
+
function isRGXToken<T extends RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): value is RGXTokenFromType<T>
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
Checks if the given value is a valid RGX token, optionally narrowed to a specific token type. When `type` is `null` (the default), it checks against all token types. When `type` is a specific token type string, it checks only against that type.
|
|
376
|
+
|
|
377
|
+
When `type` is an array, it checks that every element of the value array is a valid RGX token matching the corresponding type in the `type` array. If `matchLength` is `true` (the default), it also requires that the value array has the same length as the type array; if `false`, it allows the value array to be longer than the type array, as long as all elements up to the length of the type array match and all elements after that are still valid RGX tokens of any type.
|
|
378
|
+
|
|
379
|
+
#### Parameters
|
|
380
|
+
- `value` (`unknown`): The value to check.
|
|
381
|
+
- `type` (`T`, optional): The token type to check against. Defaults to `null`, which checks against all token types.
|
|
382
|
+
- `matchLength` (`boolean`, optional): When `type` is an array, whether to require that the value array has the same length as the type array. Defaults to `true`.
|
|
383
|
+
|
|
384
|
+
#### Returns
|
|
385
|
+
- `boolean`: `true` if the value is a valid RGX token matching the specified type, otherwise `false`.
|
|
386
|
+
|
|
387
|
+
### assertRGXToken
|
|
388
|
+
```typescript
|
|
389
|
+
function assertRGXToken<T extends RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): asserts value is RGXTokenFromType<T>
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
Asserts that the given value is a valid RGX token, optionally narrowed to a specific token type. Uses the same logic as `isRGXToken`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
393
|
+
|
|
394
|
+
#### Parameters
|
|
395
|
+
- `value` (`unknown`): The value to assert.
|
|
396
|
+
- `type` (`T`, optional): The token type to assert against. Defaults to `null`, which checks against all token types.
|
|
397
|
+
- `matchLength` (`boolean`, optional): When `type` is an array, whether to require that the value array has the same length as the type array. Defaults to `true`.
|
|
398
|
+
|
|
399
|
+
#### Returns
|
|
400
|
+
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
401
|
+
|
|
342
402
|
### isValidRegexString
|
|
343
403
|
```typescript
|
|
344
404
|
function isValidRegexString(value: string): value is ValidRegexString
|
|
@@ -432,7 +492,7 @@ A helper function that resolves an array of RGX tokens and concatenates their re
|
|
|
432
492
|
|
|
433
493
|
### rgx
|
|
434
494
|
```typescript
|
|
435
|
-
function rgx(flags?: string): (strings: TemplateStringsArray, ...tokens: RGXToken[]) =>RegExp
|
|
495
|
+
function rgx(flags?: string): (strings: TemplateStringsArray, ...tokens: RGXToken[]) => RegExp
|
|
436
496
|
```
|
|
437
497
|
|
|
438
498
|
Creates and returns a template tag function that constructs a `RegExp` 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.
|
|
@@ -7,8 +7,8 @@ const tokenExpectationMap = {
|
|
|
7
7
|
'no-op': ['null', 'undefined'],
|
|
8
8
|
'literal': ['RegExp'],
|
|
9
9
|
'native': ['string', 'number', 'boolean', 'null', 'undefined'],
|
|
10
|
-
'convertible': ['object with a toRgx method that returns a valid token'],
|
|
11
|
-
'array': ['array of native/literal tokens'],
|
|
10
|
+
'convertible': ['object with a toRgx method that returns a valid native/literal token or an array of valid native/literal tokens'],
|
|
11
|
+
'array': ['array of native/literal/convertible tokens'],
|
|
12
12
|
};
|
|
13
13
|
class RGXInvalidTokenError extends errors_1.RGXError {
|
|
14
14
|
setExpected(expected) {
|
package/dist/typeGuards.d.ts
CHANGED
|
@@ -10,6 +10,10 @@ export declare function assertRGXConvertibleToken(value: unknown): asserts value
|
|
|
10
10
|
export declare function rgxTokenTypeFlat(value: t.RGXToken): t.RGXTokenTypeFlat;
|
|
11
11
|
export declare function rgxTokenType(value: t.RGXToken): t.RGXTokenType;
|
|
12
12
|
export declare function rgxTokenFromType<T extends t.RGXTokenType | t.RGXTokenTypeFlat>(type: T, value: t.RGXToken): t.RGXTokenFromType<T>;
|
|
13
|
+
export declare function rgxTokenTypeToFlat(type: t.RGXTokenType): t.RGXTokenTypeFlat;
|
|
14
|
+
export declare function rgxTokenTypeGuardInputToFlat(type: t.RGXTokenTypeGuardInput): t.RGXTokenTypeFlat | null;
|
|
15
|
+
export declare function isRGXToken<T extends t.RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): value is t.RGXTokenFromType<T>;
|
|
16
|
+
export declare function assertRGXToken<T extends t.RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): asserts value is t.RGXTokenFromType<T>;
|
|
13
17
|
export declare function isValidRegexString(value: string): value is t.ValidRegexString;
|
|
14
18
|
export declare function assertValidRegexString(value: string): asserts value is t.ValidRegexString;
|
|
15
19
|
export declare function isValidVanillaRegexFlags(value: string): value is t.ValidVanillaRegexFlags;
|
package/dist/typeGuards.js
CHANGED
|
@@ -47,6 +47,10 @@ exports.assertRGXConvertibleToken = assertRGXConvertibleToken;
|
|
|
47
47
|
exports.rgxTokenTypeFlat = rgxTokenTypeFlat;
|
|
48
48
|
exports.rgxTokenType = rgxTokenType;
|
|
49
49
|
exports.rgxTokenFromType = rgxTokenFromType;
|
|
50
|
+
exports.rgxTokenTypeToFlat = rgxTokenTypeToFlat;
|
|
51
|
+
exports.rgxTokenTypeGuardInputToFlat = rgxTokenTypeGuardInputToFlat;
|
|
52
|
+
exports.isRGXToken = isRGXToken;
|
|
53
|
+
exports.assertRGXToken = assertRGXToken;
|
|
50
54
|
exports.isValidRegexString = isValidRegexString;
|
|
51
55
|
exports.assertValidRegexString = assertValidRegexString;
|
|
52
56
|
exports.isValidVanillaRegexFlags = isValidVanillaRegexFlags;
|
|
@@ -125,6 +129,47 @@ function rgxTokenFromType(type, value) {
|
|
|
125
129
|
/* istanbul ignore next */
|
|
126
130
|
return value;
|
|
127
131
|
}
|
|
132
|
+
function rgxTokenTypeToFlat(type) {
|
|
133
|
+
return Array.isArray(type) ? 'array' : type;
|
|
134
|
+
}
|
|
135
|
+
function rgxTokenTypeGuardInputToFlat(type) {
|
|
136
|
+
if (type === null)
|
|
137
|
+
return null;
|
|
138
|
+
if (Array.isArray(type))
|
|
139
|
+
return 'array';
|
|
140
|
+
return type;
|
|
141
|
+
}
|
|
142
|
+
function isRGXToken(value, type = null, matchLength = true) {
|
|
143
|
+
function typeMatches(s) {
|
|
144
|
+
return type === null || type === s;
|
|
145
|
+
}
|
|
146
|
+
if (typeMatches('no-op') && isRGXNoOpToken(value))
|
|
147
|
+
return true;
|
|
148
|
+
if (typeMatches('literal') && isRGXLiteralToken(value))
|
|
149
|
+
return true;
|
|
150
|
+
if (typeMatches('native') && isRGXNativeToken(value))
|
|
151
|
+
return true;
|
|
152
|
+
if (typeMatches('convertible') && isRGXConvertibleToken(value))
|
|
153
|
+
return true;
|
|
154
|
+
if (typeMatches('array') && Array.isArray(value)) {
|
|
155
|
+
// @ts-ignore Excessively deep type is not a problem here.
|
|
156
|
+
return value.every(item => isRGXToken(item, null));
|
|
157
|
+
}
|
|
158
|
+
if (Array.isArray(type) && Array.isArray(value) && (!matchLength || type.length === value.length)) {
|
|
159
|
+
// This will always be false.
|
|
160
|
+
if (value.length < type.length)
|
|
161
|
+
return false;
|
|
162
|
+
// @ts-ignore Excessively deep type is not a problem here.
|
|
163
|
+
return value.every((item, i) => isRGXToken(item, type[i] ?? null));
|
|
164
|
+
}
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
function assertRGXToken(value, type = null, matchLength = true) {
|
|
168
|
+
if (!isRGXToken(value, type, matchLength)) {
|
|
169
|
+
const flatType = rgxTokenTypeGuardInputToFlat(type);
|
|
170
|
+
throw new e.RGXInvalidTokenError("Invalid RGX token", flatType === null ? null : { type: "tokenType", values: [flatType] }, value);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
128
173
|
function isValidRegexString(value) {
|
|
129
174
|
try {
|
|
130
175
|
new RegExp(value);
|
package/dist/types.d.ts
CHANGED
|
@@ -10,8 +10,9 @@ export type RGXConvertibleToken = {
|
|
|
10
10
|
export type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
|
|
11
11
|
export type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | RGXTokenType[];
|
|
12
12
|
export type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
|
|
13
|
-
export type
|
|
14
|
-
|
|
13
|
+
export type RGXTokenTypeGuardInput = Exclude<RGXTokenType, RGXTokenType[]> | RGXTokenTypeFlat | null | RGXTokenTypeGuardInput[];
|
|
14
|
+
export type RGXTokenFromType<T extends RGXTokenTypeGuardInput> = T extends null ? RGXToken : T extends 'no-op' ? RGXNoOpToken : T extends 'literal' ? RGXLiteralToken : T extends 'native' ? RGXNativeToken : T extends 'convertible' ? RGXConvertibleToken : T extends 'array' ? RGXToken[] : T extends RGXTokenTypeGuardInput[] ? {
|
|
15
|
+
[K in keyof T]: T[K] extends RGXTokenTypeGuardInput ? RGXTokenFromType<T[K]> : never;
|
|
15
16
|
} : never;
|
|
16
17
|
export declare const validRegexSymbol: unique symbol;
|
|
17
18
|
export type ValidRegexBrandSymbol = typeof validRegexSymbol;
|