@ptolemy2002/rgx 2.6.0 → 2.7.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 +32 -8
- 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 +5 -3
- package/dist/typeGuards.js +16 -11
- package/dist/types.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,7 +23,7 @@ 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 RGXTokenTypeGuardInput =
|
|
26
|
+
type RGXTokenTypeGuardInput = RGXTokenTypeFlat | null | RGXTokenTypeGuardInput[];
|
|
27
27
|
type RGXTokenFromType<T extends RGXTokenTypeGuardInput> =
|
|
28
28
|
// ... see source for full definition
|
|
29
29
|
;
|
|
@@ -279,12 +279,36 @@ Asserts that the given value is a convertible token (an object with a `toRgx` me
|
|
|
279
279
|
#### Returns
|
|
280
280
|
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
281
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
|
+
|
|
282
306
|
### rgxTokenType
|
|
283
307
|
```typescript
|
|
284
|
-
function rgxTokenType(value:
|
|
308
|
+
function rgxTokenType(value: unknown): RGXTokenType
|
|
285
309
|
```
|
|
286
310
|
|
|
287
|
-
Determines the type of a given RGX token (`no-op`, `literal`, `native`, `convertible`, or an array of the former).
|
|
311
|
+
Determines the type of a given RGX token value (`no-op`, `literal`, `native`, `convertible`, or an array of the former) or throws an error if the value is not a valid RGX token.
|
|
288
312
|
|
|
289
313
|
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.
|
|
290
314
|
|
|
@@ -299,16 +323,16 @@ if (type === 'native') {
|
|
|
299
323
|
```
|
|
300
324
|
|
|
301
325
|
#### Parameters
|
|
302
|
-
- `value` (`
|
|
326
|
+
- `value` (`unknown`): The value to check.
|
|
303
327
|
|
|
304
328
|
#### Returns
|
|
305
329
|
- `RGXTokenType`: The type of the RGX token.
|
|
306
330
|
|
|
307
331
|
### rgxTokenTypeFlat
|
|
308
332
|
```typescript
|
|
309
|
-
function rgxTokenTypeFlat(value:
|
|
333
|
+
function rgxTokenTypeFlat(value: unknown): RGXTokenTypeFlat
|
|
310
334
|
```
|
|
311
|
-
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.
|
|
335
|
+
Determines the flat type of a given RGX token value (`no-op`, `literal`, `native`, `convertible`, 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.
|
|
312
336
|
|
|
313
337
|
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.
|
|
314
338
|
|
|
@@ -322,14 +346,14 @@ if (type === 'array') {
|
|
|
322
346
|
```
|
|
323
347
|
|
|
324
348
|
#### Parameters
|
|
325
|
-
- `value` (`
|
|
349
|
+
- `value` (`unknown`): The value to check.
|
|
326
350
|
|
|
327
351
|
#### Returns
|
|
328
352
|
- `RGXTokenTypeFlat`: The flat type of the RGX token.
|
|
329
353
|
|
|
330
354
|
### rgxTokenFromType
|
|
331
355
|
```typescript
|
|
332
|
-
function rgxTokenFromType<T extends
|
|
356
|
+
function rgxTokenFromType<T extends RGXTokenTypeGuardInput>(type: T, value: RGXToken): RGXTokenFromType<T>
|
|
333
357
|
```
|
|
334
358
|
|
|
335
359
|
Does nothing at runtime, but performs a type assertion to the correct subset of `RGXToken` based on the provided `RGXTokenType`.
|
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,11 @@ 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
|
|
11
|
-
export declare function
|
|
12
|
-
export declare function
|
|
10
|
+
export declare function isRGXArrayToken(value: unknown): value is t.RGXToken[];
|
|
11
|
+
export declare function assertRGXArrayToken(value: unknown): asserts value is t.RGXToken[];
|
|
12
|
+
export declare function rgxTokenTypeFlat(value: unknown): t.RGXTokenTypeFlat;
|
|
13
|
+
export declare function rgxTokenType(value: unknown): t.RGXTokenType;
|
|
14
|
+
export declare function rgxTokenFromType<T extends t.RGXTokenTypeGuardInput>(type: T, value: t.RGXToken): t.RGXTokenFromType<T>;
|
|
13
15
|
export declare function rgxTokenTypeToFlat(type: t.RGXTokenType): t.RGXTokenTypeFlat;
|
|
14
16
|
export declare function rgxTokenTypeGuardInputToFlat(type: t.RGXTokenTypeGuardInput): t.RGXTokenTypeFlat | null;
|
|
15
17
|
export declare function isRGXToken<T extends t.RGXTokenTypeGuardInput = null>(value: unknown, type?: T, matchLength?: boolean): value is t.RGXTokenFromType<T>;
|
package/dist/typeGuards.js
CHANGED
|
@@ -44,6 +44,8 @@ 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;
|
|
@@ -99,6 +101,16 @@ function assertRGXConvertibleToken(value) {
|
|
|
99
101
|
throw new e.RGXInvalidTokenError(`Invalid convertible token`, { type: "tokenType", values: ['convertible'] }, value);
|
|
100
102
|
}
|
|
101
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
|
+
}
|
|
102
114
|
function rgxTokenTypeFlat(value) {
|
|
103
115
|
if (isRGXNoOpToken(value))
|
|
104
116
|
return 'no-op';
|
|
@@ -108,21 +120,16 @@ function rgxTokenTypeFlat(value) {
|
|
|
108
120
|
return 'native';
|
|
109
121
|
if (isRGXConvertibleToken(value))
|
|
110
122
|
return 'convertible';
|
|
111
|
-
if (
|
|
123
|
+
if (isRGXArrayToken(value))
|
|
112
124
|
return 'array';
|
|
113
|
-
// Ignoring this line since it should be impossible to reach if the types are correct, but we need it to satisfy the return type
|
|
114
|
-
/* istanbul ignore next */
|
|
115
125
|
throw new e.RGXInvalidTokenError("Invalid RGX token", null, value);
|
|
116
126
|
}
|
|
117
127
|
function rgxTokenType(value) {
|
|
118
128
|
const flatType = rgxTokenTypeFlat(value);
|
|
119
129
|
if (flatType !== 'array')
|
|
120
130
|
return flatType;
|
|
121
|
-
|
|
131
|
+
else
|
|
122
132
|
return value.map(rgxTokenType);
|
|
123
|
-
// Ignoring this line since it should be impossible to reach if the types are correct, but we need it to satisfy the return type
|
|
124
|
-
/* istanbul ignore next */
|
|
125
|
-
throw new e.RGXInvalidTokenError("Invalid RGX token", null, value);
|
|
126
133
|
}
|
|
127
134
|
function rgxTokenFromType(type, value) {
|
|
128
135
|
// Ignoring this line because the function is entirely a TypeScript utility that doesn't need to be tested at runtime.
|
|
@@ -151,10 +158,8 @@ function isRGXToken(value, type = null, matchLength = true) {
|
|
|
151
158
|
return true;
|
|
152
159
|
if (typeMatches('convertible') && isRGXConvertibleToken(value))
|
|
153
160
|
return true;
|
|
154
|
-
if (typeMatches('array') &&
|
|
155
|
-
|
|
156
|
-
return value.every(item => isRGXToken(item, null));
|
|
157
|
-
}
|
|
161
|
+
if (typeMatches('array') && isRGXArrayToken(value))
|
|
162
|
+
return true;
|
|
158
163
|
if (Array.isArray(type) && Array.isArray(value) && (!matchLength || type.length === value.length)) {
|
|
159
164
|
// This will always be false.
|
|
160
165
|
if (value.length < type.length)
|
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 RGXTokenTypeGuardInput =
|
|
13
|
+
export type RGXTokenTypeGuardInput = RGXTokenTypeFlat | null | RGXTokenTypeGuardInput[];
|
|
14
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
15
|
[K in keyof T]: T[K] extends RGXTokenTypeGuardInput ? RGXTokenFromType<T[K]> : never;
|
|
16
16
|
} : never;
|