@ptolemy2002/rgx 2.5.2 → 2.7.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 +85 -2
- package/dist/errors/invalidToken.js +2 -2
- package/dist/index.js +2 -11
- package/dist/internal/index.d.ts +1 -0
- package/dist/internal/index.js +1 -0
- package/dist/internal/taggedTemplateToArray.d.ts +1 -0
- package/dist/internal/taggedTemplateToArray.js +19 -0
- package/dist/typeGuards.d.ts +7 -1
- package/dist/typeGuards.js +55 -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 = RGXTokenTypeFlat | null | RGXTokenTypeGuardInput[];
|
|
27
|
+
type RGXTokenFromType<T extends RGXTokenTypeGuardInput> =
|
|
27
28
|
// ... see source for full definition
|
|
28
29
|
;
|
|
29
30
|
|
|
@@ -278,6 +279,30 @@ Asserts that the given value is a convertible token (an object with a `toRgx` me
|
|
|
278
279
|
#### Returns
|
|
279
280
|
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
280
281
|
|
|
282
|
+
### isRGXArrayToken
|
|
283
|
+
```typescript
|
|
284
|
+
function isRGXArrayToken(value: unknown): value is RGXToken[]
|
|
285
|
+
```
|
|
286
|
+
Checks if the given value is an array of RGX tokens. Validates that the value is an array and that every element is a valid RGX token (of any type, including nested arrays).
|
|
287
|
+
|
|
288
|
+
#### Parameters
|
|
289
|
+
- `value` (`unknown`): The value to check.
|
|
290
|
+
|
|
291
|
+
#### Returns
|
|
292
|
+
- `boolean`: `true` if the value is an array of RGX tokens, otherwise `false`.
|
|
293
|
+
|
|
294
|
+
### assertRGXArrayToken
|
|
295
|
+
```typescript
|
|
296
|
+
function assertRGXArrayToken(value: unknown): asserts value is RGXToken[]
|
|
297
|
+
```
|
|
298
|
+
Asserts that the given value is an array of RGX tokens. Validates that the value is an array and that every element is a valid RGX token (of any type, including nested arrays). If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
299
|
+
|
|
300
|
+
#### Parameters
|
|
301
|
+
- `value` (`unknown`): The value to assert.
|
|
302
|
+
|
|
303
|
+
#### Returns
|
|
304
|
+
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
305
|
+
|
|
281
306
|
### rgxTokenType
|
|
282
307
|
```typescript
|
|
283
308
|
function rgxTokenType(value: RGXToken): RGXTokenType
|
|
@@ -328,7 +353,7 @@ if (type === 'array') {
|
|
|
328
353
|
|
|
329
354
|
### rgxTokenFromType
|
|
330
355
|
```typescript
|
|
331
|
-
function rgxTokenFromType<T extends
|
|
356
|
+
function rgxTokenFromType<T extends RGXTokenTypeGuardInput>(type: T, value: RGXToken): RGXTokenFromType<T>
|
|
332
357
|
```
|
|
333
358
|
|
|
334
359
|
Does nothing at runtime, but performs a type assertion to the correct subset of `RGXToken` based on the provided `RGXTokenType`.
|
|
@@ -340,6 +365,64 @@ Does nothing at runtime, but performs a type assertion to the correct subset of
|
|
|
340
365
|
#### Returns
|
|
341
366
|
- `RGXTokenFromType<T>`: The input value, but with its type asserted to the corresponding token type based on the provided `RGXTokenType`.
|
|
342
367
|
|
|
368
|
+
### rgxTokenTypeToFlat
|
|
369
|
+
```typescript
|
|
370
|
+
function rgxTokenTypeToFlat(type: RGXTokenType): RGXTokenTypeFlat
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
Converts an `RGXTokenType` to its flat equivalent `RGXTokenTypeFlat`. If the type is an array, it returns `'array'`; otherwise, it returns the type as-is.
|
|
374
|
+
|
|
375
|
+
#### Parameters
|
|
376
|
+
- `type` (`RGXTokenType`): The RGX token type to convert.
|
|
377
|
+
|
|
378
|
+
#### Returns
|
|
379
|
+
- `RGXTokenTypeFlat`: The flat equivalent of the provided token type.
|
|
380
|
+
|
|
381
|
+
### rgxTokenTypeGuardInputToFlat
|
|
382
|
+
```typescript
|
|
383
|
+
function rgxTokenTypeGuardInputToFlat(type: RGXTokenTypeGuardInput): RGXTokenTypeFlat | null
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
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.
|
|
387
|
+
|
|
388
|
+
#### Parameters
|
|
389
|
+
- `type` (`RGXTokenTypeGuardInput`): The type guard input to convert.
|
|
390
|
+
|
|
391
|
+
#### Returns
|
|
392
|
+
- `RGXTokenTypeFlat | null`: The flat equivalent of the provided type guard input, or `null` if the input is `null`.
|
|
393
|
+
|
|
394
|
+
### isRGXToken
|
|
395
|
+
```typescript
|
|
396
|
+
function isRGXToken<T extends RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): value is RGXTokenFromType<T>
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
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.
|
|
400
|
+
|
|
401
|
+
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.
|
|
402
|
+
|
|
403
|
+
#### Parameters
|
|
404
|
+
- `value` (`unknown`): The value to check.
|
|
405
|
+
- `type` (`T`, optional): The token type to check against. Defaults to `null`, which checks against all token types.
|
|
406
|
+
- `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`.
|
|
407
|
+
|
|
408
|
+
#### Returns
|
|
409
|
+
- `boolean`: `true` if the value is a valid RGX token matching the specified type, otherwise `false`.
|
|
410
|
+
|
|
411
|
+
### assertRGXToken
|
|
412
|
+
```typescript
|
|
413
|
+
function assertRGXToken<T extends RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): asserts value is RGXTokenFromType<T>
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
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.
|
|
417
|
+
|
|
418
|
+
#### Parameters
|
|
419
|
+
- `value` (`unknown`): The value to assert.
|
|
420
|
+
- `type` (`T`, optional): The token type to assert against. Defaults to `null`, which checks against all token types.
|
|
421
|
+
- `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`.
|
|
422
|
+
|
|
423
|
+
#### Returns
|
|
424
|
+
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
425
|
+
|
|
343
426
|
### isValidRegexString
|
|
344
427
|
```typescript
|
|
345
428
|
function isValidRegexString(value: string): value is ValidRegexString
|
|
@@ -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/index.js
CHANGED
|
@@ -41,6 +41,7 @@ exports.default = rgx;
|
|
|
41
41
|
const tg = __importStar(require("./typeGuards"));
|
|
42
42
|
const class_1 = require("./class");
|
|
43
43
|
const concat_1 = require("./concat");
|
|
44
|
+
const internal_1 = require("./internal");
|
|
44
45
|
__exportStar(require("./errors"), exports);
|
|
45
46
|
__exportStar(require("./types"), exports);
|
|
46
47
|
__exportStar(require("./typeGuards"), exports);
|
|
@@ -50,16 +51,6 @@ __exportStar(require("./resolve"), exports);
|
|
|
50
51
|
__exportStar(require("./concat"), exports);
|
|
51
52
|
// Call this for certain class methods to work correctly
|
|
52
53
|
(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]);
|
|
60
|
-
}
|
|
61
|
-
return tokenArray;
|
|
62
|
-
}
|
|
63
54
|
function rgxa(tokens, flags = '') {
|
|
64
55
|
tg.assertValidVanillaRegexFlags(flags);
|
|
65
56
|
const pattern = (0, concat_1.rgxConcat)(tokens);
|
|
@@ -68,6 +59,6 @@ function rgxa(tokens, flags = '') {
|
|
|
68
59
|
function rgx(flags = '') {
|
|
69
60
|
tg.assertValidVanillaRegexFlags(flags);
|
|
70
61
|
return (strings, ...tokens) => {
|
|
71
|
-
return rgxa(
|
|
62
|
+
return rgxa((0, internal_1.taggedTemplateToArray)(strings, tokens), flags);
|
|
72
63
|
};
|
|
73
64
|
}
|
package/dist/internal/index.d.ts
CHANGED
package/dist/internal/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function taggedTemplateToArray<T>(strings: TemplateStringsArray, tokens: T[]): (string | T)[];
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.taggedTemplateToArray = taggedTemplateToArray;
|
|
4
|
+
function taggedTemplateToArray(strings, tokens) {
|
|
5
|
+
function isNullOrUndefined(value) {
|
|
6
|
+
return value === null || value === undefined;
|
|
7
|
+
}
|
|
8
|
+
const array = [];
|
|
9
|
+
for (let i = 0; i < Math.max(strings.length, tokens.length); i++) {
|
|
10
|
+
const string = strings[i];
|
|
11
|
+
const token = tokens[i];
|
|
12
|
+
// Strings always come before tokens
|
|
13
|
+
if (!isNullOrUndefined(string))
|
|
14
|
+
array.push(string);
|
|
15
|
+
if (!isNullOrUndefined(token))
|
|
16
|
+
array.push(token);
|
|
17
|
+
}
|
|
18
|
+
return array;
|
|
19
|
+
}
|
package/dist/typeGuards.d.ts
CHANGED
|
@@ -7,9 +7,15 @@ 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 isRGXArrayToken(value: unknown): value is t.RGXToken[];
|
|
11
|
+
export declare function assertRGXArrayToken(value: unknown): asserts value is t.RGXToken[];
|
|
10
12
|
export declare function rgxTokenTypeFlat(value: t.RGXToken): t.RGXTokenTypeFlat;
|
|
11
13
|
export declare function rgxTokenType(value: t.RGXToken): t.RGXTokenType;
|
|
12
|
-
export declare function rgxTokenFromType<T extends t.
|
|
14
|
+
export declare function rgxTokenFromType<T extends t.RGXTokenTypeGuardInput>(type: T, value: t.RGXToken): t.RGXTokenFromType<T>;
|
|
15
|
+
export declare function rgxTokenTypeToFlat(type: t.RGXTokenType): t.RGXTokenTypeFlat;
|
|
16
|
+
export declare function rgxTokenTypeGuardInputToFlat(type: t.RGXTokenTypeGuardInput): t.RGXTokenTypeFlat | null;
|
|
17
|
+
export declare function isRGXToken<T extends t.RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): value is t.RGXTokenFromType<T>;
|
|
18
|
+
export declare function assertRGXToken<T extends t.RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): asserts value is t.RGXTokenFromType<T>;
|
|
13
19
|
export declare function isValidRegexString(value: string): value is t.ValidRegexString;
|
|
14
20
|
export declare function assertValidRegexString(value: string): asserts value is t.ValidRegexString;
|
|
15
21
|
export declare function isValidVanillaRegexFlags(value: string): value is t.ValidVanillaRegexFlags;
|
package/dist/typeGuards.js
CHANGED
|
@@ -44,9 +44,15 @@ exports.isRGXNativeToken = isRGXNativeToken;
|
|
|
44
44
|
exports.assertRGXNativeToken = assertRGXNativeToken;
|
|
45
45
|
exports.isRGXConvertibleToken = isRGXConvertibleToken;
|
|
46
46
|
exports.assertRGXConvertibleToken = assertRGXConvertibleToken;
|
|
47
|
+
exports.isRGXArrayToken = isRGXArrayToken;
|
|
48
|
+
exports.assertRGXArrayToken = assertRGXArrayToken;
|
|
47
49
|
exports.rgxTokenTypeFlat = rgxTokenTypeFlat;
|
|
48
50
|
exports.rgxTokenType = rgxTokenType;
|
|
49
51
|
exports.rgxTokenFromType = rgxTokenFromType;
|
|
52
|
+
exports.rgxTokenTypeToFlat = rgxTokenTypeToFlat;
|
|
53
|
+
exports.rgxTokenTypeGuardInputToFlat = rgxTokenTypeGuardInputToFlat;
|
|
54
|
+
exports.isRGXToken = isRGXToken;
|
|
55
|
+
exports.assertRGXToken = assertRGXToken;
|
|
50
56
|
exports.isValidRegexString = isValidRegexString;
|
|
51
57
|
exports.assertValidRegexString = assertValidRegexString;
|
|
52
58
|
exports.isValidVanillaRegexFlags = isValidVanillaRegexFlags;
|
|
@@ -95,6 +101,16 @@ function assertRGXConvertibleToken(value) {
|
|
|
95
101
|
throw new e.RGXInvalidTokenError(`Invalid convertible token`, { type: "tokenType", values: ['convertible'] }, value);
|
|
96
102
|
}
|
|
97
103
|
}
|
|
104
|
+
function isRGXArrayToken(value) {
|
|
105
|
+
return Array.isArray(value) && value.every(item => isRGXNoOpToken(item) || isRGXLiteralToken(item) ||
|
|
106
|
+
isRGXNativeToken(item) ||
|
|
107
|
+
isRGXConvertibleToken(item) || isRGXArrayToken(item));
|
|
108
|
+
}
|
|
109
|
+
function assertRGXArrayToken(value) {
|
|
110
|
+
if (!isRGXArrayToken(value)) {
|
|
111
|
+
throw new e.RGXInvalidTokenError("Invalid array token", { type: "tokenType", values: ['array'] }, value);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
98
114
|
function rgxTokenTypeFlat(value) {
|
|
99
115
|
if (isRGXNoOpToken(value))
|
|
100
116
|
return 'no-op';
|
|
@@ -125,6 +141,45 @@ function rgxTokenFromType(type, value) {
|
|
|
125
141
|
/* istanbul ignore next */
|
|
126
142
|
return value;
|
|
127
143
|
}
|
|
144
|
+
function rgxTokenTypeToFlat(type) {
|
|
145
|
+
return Array.isArray(type) ? 'array' : type;
|
|
146
|
+
}
|
|
147
|
+
function rgxTokenTypeGuardInputToFlat(type) {
|
|
148
|
+
if (type === null)
|
|
149
|
+
return null;
|
|
150
|
+
if (Array.isArray(type))
|
|
151
|
+
return 'array';
|
|
152
|
+
return type;
|
|
153
|
+
}
|
|
154
|
+
function isRGXToken(value, type = null, matchLength = true) {
|
|
155
|
+
function typeMatches(s) {
|
|
156
|
+
return type === null || type === s;
|
|
157
|
+
}
|
|
158
|
+
if (typeMatches('no-op') && isRGXNoOpToken(value))
|
|
159
|
+
return true;
|
|
160
|
+
if (typeMatches('literal') && isRGXLiteralToken(value))
|
|
161
|
+
return true;
|
|
162
|
+
if (typeMatches('native') && isRGXNativeToken(value))
|
|
163
|
+
return true;
|
|
164
|
+
if (typeMatches('convertible') && isRGXConvertibleToken(value))
|
|
165
|
+
return true;
|
|
166
|
+
if (typeMatches('array') && isRGXArrayToken(value))
|
|
167
|
+
return true;
|
|
168
|
+
if (Array.isArray(type) && Array.isArray(value) && (!matchLength || type.length === value.length)) {
|
|
169
|
+
// This will always be false.
|
|
170
|
+
if (value.length < type.length)
|
|
171
|
+
return false;
|
|
172
|
+
// @ts-ignore Excessively deep type is not a problem here.
|
|
173
|
+
return value.every((item, i) => isRGXToken(item, type[i] ?? null));
|
|
174
|
+
}
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
function assertRGXToken(value, type = null, matchLength = true) {
|
|
178
|
+
if (!isRGXToken(value, type, matchLength)) {
|
|
179
|
+
const flatType = rgxTokenTypeGuardInputToFlat(type);
|
|
180
|
+
throw new e.RGXInvalidTokenError("Invalid RGX token", flatType === null ? null : { type: "tokenType", values: [flatType] }, value);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
128
183
|
function isValidRegexString(value) {
|
|
129
184
|
try {
|
|
130
185
|
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 = 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;
|