@ptolemy2002/rgx 1.1.1 → 2.0.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 +157 -14
- package/dist/errors.d.ts +21 -0
- package/dist/errors.js +50 -0
- package/dist/index.d.ts +15 -3
- package/dist/index.js +82 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,22 +4,73 @@ A library for easy construction and validation of regular expressions in TypeScr
|
|
|
4
4
|
## Type Reference
|
|
5
5
|
```typescript
|
|
6
6
|
import { Branded } from "@ptolemy2002/ts-brand-utils";
|
|
7
|
+
import { MaybeArray } from "@ptolemy2002/ts-utils";
|
|
7
8
|
|
|
8
9
|
type RGXNoOpToken = null | undefined;
|
|
9
10
|
type RGXLiteralToken = RegExp;
|
|
10
11
|
type RGXNativeToken = string | number | boolean | RGXNoOpToken;
|
|
11
|
-
type RGXConvertibleToken = { toRgx: () => RGXNativeToken |
|
|
12
|
-
type RGXToken = RGXNativeToken | RGXConvertibleToken | RGXToken[];
|
|
12
|
+
type RGXConvertibleToken = { toRgx: () => MaybeArray<RGXNativeToken | RGXLiteralToken> };
|
|
13
|
+
type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
|
|
13
14
|
|
|
14
|
-
const validRegexSymbol = Symbol('ValidRegex');
|
|
15
|
+
const validRegexSymbol = Symbol('rgx.ValidRegex');
|
|
15
16
|
type ValidRegexBrandSymbol = typeof validRegexSymbol;
|
|
16
17
|
type ValidRegexString = Branded<string, [ValidRegexBrandSymbol]>;
|
|
17
18
|
|
|
18
|
-
|
|
19
|
+
const validVanillaRegexFlagsSymbol = Symbol('rgx.ValidVanillaRegexFlags');
|
|
20
|
+
type ValidVanillaRegexFlagsBrandSymbol = typeof validVanillaRegexFlagsSymbol;
|
|
21
|
+
type ValidVanillaRegexFlags = Branded<string, [ValidVanillaRegexFlagsBrandSymbol]>;
|
|
22
|
+
|
|
23
|
+
type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | RGXTokenType[];
|
|
19
24
|
type RGXTokenFromType<T extends RGXTokenType> =
|
|
20
25
|
// ... see source for full definition
|
|
21
26
|
;
|
|
27
|
+
|
|
28
|
+
type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_VANILLA_REGEX_FLAGS';
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Classes
|
|
32
|
+
The library exports the following classes:
|
|
33
|
+
|
|
34
|
+
### RGXError
|
|
35
|
+
A custom error class for RGX-related errors. This can be used to throw specific errors related to RGX token validation or resolution.
|
|
36
|
+
|
|
37
|
+
#### Constructor
|
|
38
|
+
```typescript
|
|
39
|
+
constructor(message: string, code?: RGXErrorCode)
|
|
40
|
+
```
|
|
41
|
+
- `message` (`string`): The error message.
|
|
42
|
+
- `code` (`RGXErrorCode`, optional): An optional error code that can be used to categorize the error. If not provided, it defaults to 'UNKNOWN'.
|
|
43
|
+
|
|
44
|
+
### RGXInvalidTokenError extends RGXError
|
|
45
|
+
A specific error class for invalid RGX tokens. This error is thrown when a value fails validation as a specific RGX token type.
|
|
46
|
+
|
|
47
|
+
#### Constructor
|
|
48
|
+
```typescript
|
|
49
|
+
constructor(message: string, expected: string | null, got: unknown)
|
|
50
|
+
```
|
|
51
|
+
- `message` (`string`): The error message.
|
|
52
|
+
- `expected` (`string` | `null`): A description of the expected token type. If `null`, will use a default message indicating the expected types of any RGX token.
|
|
53
|
+
- `got` (`unknown`): The actual value that was received, which failed validation.
|
|
54
|
+
|
|
55
|
+
### RGXInvalidRegexStringError extends RGXError
|
|
56
|
+
A specific error class for invalid regex strings. This error is thrown when a string fails validation as a valid regex string.
|
|
57
|
+
|
|
58
|
+
#### Constructor
|
|
59
|
+
```typescript
|
|
60
|
+
constructor(message: string, got: string)
|
|
61
|
+
```
|
|
62
|
+
- `message` (`string`): The error message.
|
|
63
|
+
- `got` (`string`): The actual string that was received, which failed validation.
|
|
64
|
+
|
|
65
|
+
### RGXInvalidVanillaRegexFlagsError extends RGXError
|
|
66
|
+
A specific error class for invalid vanilla regex flags. This error is thrown when a string fails validation as valid vanilla regex flags.
|
|
67
|
+
|
|
68
|
+
#### Constructor
|
|
69
|
+
```typescript
|
|
70
|
+
constructor(message: string, got: string)
|
|
22
71
|
```
|
|
72
|
+
- `message` (`string`): The error message.
|
|
73
|
+
- `got` (`string`): The actual string that was received, which failed validation.
|
|
23
74
|
|
|
24
75
|
## Functions
|
|
25
76
|
The following functions are exported by the library:
|
|
@@ -37,6 +88,19 @@ Checks if the given value is a no-op token (`null` or `undefined`).
|
|
|
37
88
|
#### Returns
|
|
38
89
|
- `boolean`: `true` if the value is a no-op token, otherwise `false`.
|
|
39
90
|
|
|
91
|
+
### assertRGXNoOpToken
|
|
92
|
+
```typescript
|
|
93
|
+
function assertRGXNoOpToken(value: unknown): asserts value is RGXNoOpToken
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Asserts that the given value is a no-op token (`null` or `undefined`). If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
97
|
+
|
|
98
|
+
#### Parameters
|
|
99
|
+
- `value` (`unknown`): The value to assert.
|
|
100
|
+
|
|
101
|
+
#### Returns
|
|
102
|
+
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
103
|
+
|
|
40
104
|
### isRGXLiteralToken
|
|
41
105
|
```typescript
|
|
42
106
|
function isRGXLiteralToken(value: unknown): value is RGXLiteralToken
|
|
@@ -50,6 +114,19 @@ Checks if the given value is a literal token (a `RegExp` object).
|
|
|
50
114
|
#### Returns
|
|
51
115
|
- `boolean`: `true` if the value is a literal token, otherwise `false`.
|
|
52
116
|
|
|
117
|
+
### assertRGXLiteralToken
|
|
118
|
+
```typescript
|
|
119
|
+
function assertRGXLiteralToken(value: unknown): asserts value is RGXLiteralToken
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Asserts that the given value is a literal token (a `RegExp` object). If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
123
|
+
|
|
124
|
+
#### Parameters
|
|
125
|
+
- `value` (`unknown`): The value to assert.
|
|
126
|
+
|
|
127
|
+
#### Returns
|
|
128
|
+
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
129
|
+
|
|
53
130
|
### isRGXNativeToken
|
|
54
131
|
```typescript
|
|
55
132
|
function isRGXNativeToken(value: unknown): value is RGXNativeToken
|
|
@@ -63,12 +140,25 @@ Checks if the given value is a native token (string, number, boolean, or no-op).
|
|
|
63
140
|
#### Returns
|
|
64
141
|
- `boolean`: `true` if the value is a native token, otherwise `false`.
|
|
65
142
|
|
|
143
|
+
### assertRGXNativeToken
|
|
144
|
+
```typescript
|
|
145
|
+
function assertRGXNativeToken(value: unknown): asserts value is RGXNativeToken
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Asserts that the given value is a native token (string, number, boolean, or no-op). If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
149
|
+
|
|
150
|
+
#### Parameters
|
|
151
|
+
- `value` (`unknown`): The value to assert.
|
|
152
|
+
|
|
153
|
+
#### Returns
|
|
154
|
+
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
155
|
+
|
|
66
156
|
### isRGXConvertibleToken
|
|
67
157
|
```typescript
|
|
68
158
|
function isRGXConvertibleToken(value: unknown): value is RGXConvertibleToken
|
|
69
159
|
```
|
|
70
160
|
|
|
71
|
-
Checks if the given value is a convertible token (an object with a `toRgx` method). Validates that `toRgx` is callable and returns a valid RGX native token or array of native tokens.
|
|
161
|
+
Checks if the given value is a convertible token (an object with a `toRgx` method). Validates that `toRgx` is callable and returns a valid RGX native or literal token, or an array of native/literal tokens.
|
|
72
162
|
|
|
73
163
|
#### Parameters
|
|
74
164
|
- `value` (`unknown`): The value to check.
|
|
@@ -76,12 +166,24 @@ Checks if the given value is a convertible token (an object with a `toRgx` metho
|
|
|
76
166
|
#### Returns
|
|
77
167
|
- `boolean`: `true` if the value is a convertible token, otherwise `false`.
|
|
78
168
|
|
|
169
|
+
### assertRGXConvertibleToken
|
|
170
|
+
```typescript
|
|
171
|
+
function assertRGXConvertibleToken(value: unknown): asserts value is RGXConvertibleToken
|
|
172
|
+
```
|
|
173
|
+
Asserts that the given value is a convertible token (an object with a `toRgx` method). Validates that `toRgx` is callable and returns a valid RGX native or literal token, or an array of native/literal tokens. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
|
|
174
|
+
|
|
175
|
+
#### Parameters
|
|
176
|
+
- `value` (`unknown`): The value to assert.
|
|
177
|
+
|
|
178
|
+
#### Returns
|
|
179
|
+
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
180
|
+
|
|
79
181
|
### rgxTokenType
|
|
80
182
|
```typescript
|
|
81
183
|
function rgxTokenType(value: RGXToken): RGXTokenType
|
|
82
184
|
```
|
|
83
185
|
|
|
84
|
-
Determines the type of a given RGX token (`no-op`, `native`, `convertible`, or an array of the former).
|
|
186
|
+
Determines the type of a given RGX token (`no-op`, `literal`, `native`, `convertible`, or an array of the former).
|
|
85
187
|
|
|
86
188
|
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.
|
|
87
189
|
|
|
@@ -115,9 +217,9 @@ Does nothing at runtime, but performs a type assertion to the correct subset of
|
|
|
115
217
|
#### Returns
|
|
116
218
|
- `RGXTokenFromType<T>`: The input value, but with its type asserted to the corresponding token type based on the provided `RGXTokenType`.
|
|
117
219
|
|
|
118
|
-
###
|
|
220
|
+
### isValidRegexString
|
|
119
221
|
```typescript
|
|
120
|
-
function
|
|
222
|
+
function isValidRegexString(value: string): value is ValidRegexString
|
|
121
223
|
```
|
|
122
224
|
|
|
123
225
|
Checks if the given string is a valid regular expression by attempting to create a new `RegExp` object with it. If it succeeds, the string is branded as a `ValidRegexString`.
|
|
@@ -128,6 +230,43 @@ Checks if the given string is a valid regular expression by attempting to create
|
|
|
128
230
|
#### Returns
|
|
129
231
|
- `boolean`: `true` if the string is a valid regular expression, otherwise `false`.
|
|
130
232
|
|
|
233
|
+
### assertValidRegexString
|
|
234
|
+
```typescript
|
|
235
|
+
function assertValidRegexString(value: string): asserts value is ValidRegexString
|
|
236
|
+
```
|
|
237
|
+
Asserts that the given string is a valid regular expression by attempting to create a new `RegExp` object with it. If it succeeds, the string is branded as a `ValidRegexString`. If the assertion fails, an `RGXInvalidRegexStringError` will be thrown.
|
|
238
|
+
|
|
239
|
+
#### Parameters
|
|
240
|
+
- `value` (`string`): The string to assert.
|
|
241
|
+
|
|
242
|
+
#### Returns
|
|
243
|
+
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
244
|
+
|
|
245
|
+
### isValidVanillaRegexFlags
|
|
246
|
+
```typescript
|
|
247
|
+
function isValidVanillaRegexFlags(value: string): value is ValidVanillaRegexFlags
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
Checks if the given string is a valid combination of vanilla regex flags (g, i, m, s, u, y). Each flag can only appear once.
|
|
251
|
+
|
|
252
|
+
#### Parameters
|
|
253
|
+
- `value` (`string`): The string to check.
|
|
254
|
+
|
|
255
|
+
#### Returns
|
|
256
|
+
- `boolean`: `true` if the string is a valid combination of vanilla regex flags, otherwise `false`.
|
|
257
|
+
|
|
258
|
+
### assertValidVanillaRegexFlags
|
|
259
|
+
```typescript
|
|
260
|
+
function assertValidVanillaRegexFlags(value: string): asserts value is ValidVanillaRegexFlags
|
|
261
|
+
```
|
|
262
|
+
Asserts that the given string is a valid combination of vanilla regex flags (g, i, m, s, u, y). Each flag can only appear once. If the assertion fails, an `RGXInvalidVanillaRegexFlagsError` will be thrown.
|
|
263
|
+
|
|
264
|
+
#### Parameters
|
|
265
|
+
- `value` (`string`): The string to assert.
|
|
266
|
+
|
|
267
|
+
#### Returns
|
|
268
|
+
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
269
|
+
|
|
131
270
|
### escapeRegex
|
|
132
271
|
```typescript
|
|
133
272
|
function escapeRegex(value: string): ValidRegexString
|
|
@@ -169,30 +308,34 @@ A helper function that resolves an array of RGX tokens and concatenates their re
|
|
|
169
308
|
|
|
170
309
|
### rgx
|
|
171
310
|
```typescript
|
|
172
|
-
function rgx(strings: TemplateStringsArray, ...tokens: RGXToken[])
|
|
311
|
+
function rgx(flags?: string): (strings: TemplateStringsArray, ...tokens: RGXToken[]) =>RegExp
|
|
173
312
|
```
|
|
174
313
|
|
|
175
|
-
|
|
314
|
+
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.
|
|
176
315
|
|
|
177
316
|
Example usages:
|
|
178
317
|
```typescript
|
|
179
318
|
const beginning = /^/;
|
|
180
319
|
const end = /$/;
|
|
181
320
|
const word = /\w+/;
|
|
182
|
-
const pattern = rgx`${beginning}testing ${word}${end}`; // /^testing \w+$/ - matches the string "testing " followed by a word, anchored to the start and end of the string
|
|
321
|
+
const pattern = rgx()`${beginning}testing ${word}${end}`; // /^testing \w+$/ - matches the string "testing " followed by a word, anchored to the start and end of the string
|
|
183
322
|
|
|
184
323
|
const optionalDigit = /\d?/;
|
|
185
|
-
const pattern2 = rgx`${beginning}optional digit: ${optionalDigit}${end}`; // /^optional digit: \d?$/ - matches the string "optional digit: " followed by an optional digit, anchored to the start and end of the string
|
|
324
|
+
const pattern2 = rgx()`${beginning}optional digit: ${optionalDigit}${end}`; // /^optional digit: \d?$/ - matches the string "optional digit: " followed by an optional digit, anchored to the start and end of the string
|
|
186
325
|
|
|
187
|
-
const pattern3 = rgx`${beginning}value: ${[word, optionalDigit]}${end}`; // /^value: (?:\w+|\d?)$/ - matches the string "value: " followed by either a word or an optional digit, anchored to the start and end of the string
|
|
326
|
+
const pattern3 = rgx()`${beginning}value: ${[word, optionalDigit]}${end}`; // /^value: (?:\w+|\d?)$/ - matches the string "value: " followed by either a word or an optional digit, anchored to the start and end of the string
|
|
188
327
|
```
|
|
189
328
|
|
|
190
329
|
#### Parameters
|
|
330
|
+
**Direct**
|
|
331
|
+
- `flags` (`string`, optional): The regex flags to apply to the resulting `RegExp` object (e.g., 'g', 'i', 'm', etc.). If not provided, no flags will be applied. If provided and not valid vanilla regex flags, an `RGXError` with code `INVALID_VANILLA_REGEX_FLAGS` will be thrown.
|
|
332
|
+
|
|
333
|
+
**Template Tag**
|
|
191
334
|
- `strings` (`TemplateStringsArray`): The literal parts of the template string.
|
|
192
335
|
- `tokens` (`RGXToken[]`): The RGX tokens to be resolved and concatenated with the literal parts.
|
|
193
336
|
|
|
194
337
|
#### Returns
|
|
195
|
-
- `RegExp`:
|
|
338
|
+
- `(strings: TemplateStringsArray, ...tokens: RGXToken[]) => RegExp`: A template tag function that takes a template literal and returns a `RegExp` object constructed from the resolved tokens and literal parts.
|
|
196
339
|
|
|
197
340
|
## Peer Dependencies
|
|
198
341
|
- `@ptolemy2002/ts-brand-utils` ^1.0.0
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_VANILLA_REGEX_FLAGS';
|
|
2
|
+
export declare class RGXError extends Error {
|
|
3
|
+
code: RGXErrorCode;
|
|
4
|
+
constructor(message: string, code?: RGXErrorCode);
|
|
5
|
+
}
|
|
6
|
+
export declare class RGXInvalidTokenError extends RGXError {
|
|
7
|
+
expected: string;
|
|
8
|
+
got: unknown;
|
|
9
|
+
constructor(message: string, expected: string | null, got: unknown);
|
|
10
|
+
toString(): string;
|
|
11
|
+
}
|
|
12
|
+
export declare class RGXInvalidRegexStringError extends RGXError {
|
|
13
|
+
got: string;
|
|
14
|
+
constructor(message: string, got: string);
|
|
15
|
+
toString(): string;
|
|
16
|
+
}
|
|
17
|
+
export declare class RGXInvalidVanillaRegexFlagsError extends RGXError {
|
|
18
|
+
got: string;
|
|
19
|
+
constructor(message: string, got: string);
|
|
20
|
+
toString(): string;
|
|
21
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RGXInvalidVanillaRegexFlagsError = exports.RGXInvalidRegexStringError = exports.RGXInvalidTokenError = exports.RGXError = void 0;
|
|
4
|
+
class RGXError extends Error {
|
|
5
|
+
constructor(message, code) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.code = 'UNKNOWN';
|
|
8
|
+
this.name = 'RGXError';
|
|
9
|
+
if (code) {
|
|
10
|
+
this.code = code;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.RGXError = RGXError;
|
|
15
|
+
class RGXInvalidTokenError extends RGXError {
|
|
16
|
+
constructor(message, expected, got) {
|
|
17
|
+
super(message, 'INVALID_RGX_TOKEN');
|
|
18
|
+
this.expected = '[null, undefined, string, number, boolean, RegExp, convertible object, or array of native/literal tokens]';
|
|
19
|
+
this.name = 'RGXInvalidTokenError';
|
|
20
|
+
if (expected !== null)
|
|
21
|
+
this.expected = "[" + expected + "]";
|
|
22
|
+
this.got = got;
|
|
23
|
+
}
|
|
24
|
+
toString() {
|
|
25
|
+
return `${this.name}: ${this.message}; Expected: ${this.expected}; Got: ${JSON.stringify(this.got)}`;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.RGXInvalidTokenError = RGXInvalidTokenError;
|
|
29
|
+
class RGXInvalidRegexStringError extends RGXError {
|
|
30
|
+
constructor(message, got) {
|
|
31
|
+
super(message, 'INVALID_REGEX_STRING');
|
|
32
|
+
this.name = 'RGXInvalidRegexStringError';
|
|
33
|
+
this.got = got;
|
|
34
|
+
}
|
|
35
|
+
toString() {
|
|
36
|
+
return `${this.name}: ${this.message}; Got: ${JSON.stringify(this.got)}`;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.RGXInvalidRegexStringError = RGXInvalidRegexStringError;
|
|
40
|
+
class RGXInvalidVanillaRegexFlagsError extends RGXError {
|
|
41
|
+
constructor(message, got) {
|
|
42
|
+
super(message, 'INVALID_VANILLA_REGEX_FLAGS');
|
|
43
|
+
this.name = 'RGXInvalidVanillaRegexFlagsError';
|
|
44
|
+
this.got = got;
|
|
45
|
+
}
|
|
46
|
+
toString() {
|
|
47
|
+
return `${this.name}: ${this.message}; Got: ${JSON.stringify(this.got)}`;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.RGXInvalidVanillaRegexFlagsError = RGXInvalidVanillaRegexFlagsError;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { Branded } from "@ptolemy2002/ts-brand-utils";
|
|
2
|
+
import { MaybeArray } from "@ptolemy2002/ts-utils";
|
|
3
|
+
export * from "./errors";
|
|
2
4
|
export type RGXNoOpToken = null | undefined;
|
|
3
5
|
export type RGXLiteralToken = RegExp;
|
|
4
6
|
export type RGXNativeToken = string | number | boolean | RGXNoOpToken;
|
|
5
7
|
export type RGXConvertibleToken = {
|
|
6
|
-
toRgx: () => RGXNativeToken |
|
|
8
|
+
toRgx: () => MaybeArray<RGXNativeToken | RGXLiteralToken>;
|
|
7
9
|
};
|
|
8
10
|
export type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
|
|
9
11
|
export type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | RGXTokenType[];
|
|
@@ -13,14 +15,24 @@ export type RGXTokenFromType<T extends RGXTokenType> = T extends 'no-op' ? RGXNo
|
|
|
13
15
|
export declare const validRegexSymbol: unique symbol;
|
|
14
16
|
export type ValidRegexBrandSymbol = typeof validRegexSymbol;
|
|
15
17
|
export type ValidRegexString = Branded<string, [ValidRegexBrandSymbol]>;
|
|
18
|
+
export declare const validVanillaRegexFlagsSymbol: unique symbol;
|
|
19
|
+
export type ValidVanillaRegexFlagsBrandSymbol = typeof validVanillaRegexFlagsSymbol;
|
|
20
|
+
export type ValidVanillaRegexFlags = Branded<string, [ValidVanillaRegexFlagsBrandSymbol]>;
|
|
16
21
|
export declare function isRGXNoOpToken(value: unknown): value is RGXNoOpToken;
|
|
22
|
+
export declare function assertRGXNoOpToken(value: unknown): asserts value is RGXNoOpToken;
|
|
17
23
|
export declare function isRGXLiteralToken(value: unknown): value is RGXLiteralToken;
|
|
24
|
+
export declare function assertRGXLiteralToken(value: unknown): asserts value is RGXLiteralToken;
|
|
18
25
|
export declare function isRGXNativeToken(value: unknown): value is RGXNativeToken;
|
|
26
|
+
export declare function assertRGXNativeToken(value: unknown): asserts value is RGXNativeToken;
|
|
19
27
|
export declare function isRGXConvertibleToken(value: unknown): value is RGXConvertibleToken;
|
|
28
|
+
export declare function assertRGXConvertibleToken(value: unknown): asserts value is RGXConvertibleToken;
|
|
20
29
|
export declare function rgxTokenType(value: RGXToken): RGXTokenType;
|
|
21
30
|
export declare function rgxTokenFromType<T extends RGXTokenType>(type: T, value: RGXToken): RGXTokenFromType<T>;
|
|
22
|
-
export declare function
|
|
31
|
+
export declare function isValidRegexString(value: string): value is ValidRegexString;
|
|
32
|
+
export declare function assertValidRegexString(value: string): asserts value is ValidRegexString;
|
|
33
|
+
export declare function isValidVanillaRegexFlags(value: string): value is ValidVanillaRegexFlags;
|
|
34
|
+
export declare function assertValidVanillaRegexFlags(value: string): asserts value is ValidVanillaRegexFlags;
|
|
23
35
|
export declare function escapeRegex(value: string): ValidRegexString;
|
|
24
36
|
export declare function resolveRGXToken(token: RGXToken): ValidRegexString;
|
|
25
37
|
export declare function rgxConcat(tokens: RGXToken[]): ValidRegexString;
|
|
26
|
-
export default function rgx(strings: TemplateStringsArray, ...tokens: RGXToken[])
|
|
38
|
+
export default function rgx(flags?: string): (strings: TemplateStringsArray, ...tokens: RGXToken[]) => RegExp;
|
package/dist/index.js
CHANGED
|
@@ -1,44 +1,88 @@
|
|
|
1
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
|
+
};
|
|
2
16
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
17
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
18
|
};
|
|
5
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.validRegexSymbol = void 0;
|
|
20
|
+
exports.validVanillaRegexFlagsSymbol = exports.validRegexSymbol = void 0;
|
|
7
21
|
exports.isRGXNoOpToken = isRGXNoOpToken;
|
|
22
|
+
exports.assertRGXNoOpToken = assertRGXNoOpToken;
|
|
8
23
|
exports.isRGXLiteralToken = isRGXLiteralToken;
|
|
24
|
+
exports.assertRGXLiteralToken = assertRGXLiteralToken;
|
|
9
25
|
exports.isRGXNativeToken = isRGXNativeToken;
|
|
26
|
+
exports.assertRGXNativeToken = assertRGXNativeToken;
|
|
10
27
|
exports.isRGXConvertibleToken = isRGXConvertibleToken;
|
|
28
|
+
exports.assertRGXConvertibleToken = assertRGXConvertibleToken;
|
|
11
29
|
exports.rgxTokenType = rgxTokenType;
|
|
12
30
|
exports.rgxTokenFromType = rgxTokenFromType;
|
|
13
|
-
exports.
|
|
31
|
+
exports.isValidRegexString = isValidRegexString;
|
|
32
|
+
exports.assertValidRegexString = assertValidRegexString;
|
|
33
|
+
exports.isValidVanillaRegexFlags = isValidVanillaRegexFlags;
|
|
34
|
+
exports.assertValidVanillaRegexFlags = assertValidVanillaRegexFlags;
|
|
14
35
|
exports.escapeRegex = escapeRegex;
|
|
15
36
|
exports.resolveRGXToken = resolveRGXToken;
|
|
16
37
|
exports.rgxConcat = rgxConcat;
|
|
17
38
|
exports.default = rgx;
|
|
18
39
|
const is_callable_1 = __importDefault(require("is-callable"));
|
|
19
|
-
|
|
40
|
+
const errors_1 = require("./errors");
|
|
41
|
+
__exportStar(require("./errors"), exports);
|
|
42
|
+
exports.validRegexSymbol = Symbol('rgx.ValidRegex');
|
|
43
|
+
exports.validVanillaRegexFlagsSymbol = Symbol('rgx.ValidVanillaRegexFlags');
|
|
20
44
|
function isRGXNoOpToken(value) {
|
|
21
45
|
return value === null || value === undefined;
|
|
22
46
|
}
|
|
47
|
+
function assertRGXNoOpToken(value) {
|
|
48
|
+
if (!isRGXNoOpToken(value)) {
|
|
49
|
+
throw new errors_1.RGXInvalidTokenError(`Invalid no-op token`, 'null or undefined', value);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
23
52
|
function isRGXLiteralToken(value) {
|
|
24
53
|
return value instanceof RegExp;
|
|
25
54
|
}
|
|
55
|
+
function assertRGXLiteralToken(value) {
|
|
56
|
+
if (!isRGXLiteralToken(value)) {
|
|
57
|
+
throw new errors_1.RGXInvalidTokenError(`Invalid literal token`, 'RegExp', value);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
26
60
|
function isRGXNativeToken(value) {
|
|
27
61
|
return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || isRGXNoOpToken(value);
|
|
28
62
|
}
|
|
63
|
+
function assertRGXNativeToken(value) {
|
|
64
|
+
if (!isRGXNativeToken(value)) {
|
|
65
|
+
throw new errors_1.RGXInvalidTokenError(`Invalid native token`, 'string, number, boolean, null, or undefined', value);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
29
68
|
function isRGXConvertibleToken(value) {
|
|
30
69
|
if (typeof value === 'object' && value !== null && 'toRgx' in value) {
|
|
31
70
|
if ((0, is_callable_1.default)(value.toRgx)) {
|
|
32
71
|
const returnValue = value.toRgx();
|
|
33
72
|
if (Array.isArray(returnValue)) {
|
|
34
|
-
return returnValue.every(isRGXNativeToken);
|
|
73
|
+
return returnValue.every(value => isRGXNativeToken(value) || isRGXLiteralToken(value));
|
|
35
74
|
}
|
|
36
|
-
return isRGXNativeToken(returnValue);
|
|
75
|
+
return isRGXNativeToken(returnValue) || isRGXLiteralToken(returnValue);
|
|
37
76
|
}
|
|
38
77
|
return false;
|
|
39
78
|
}
|
|
40
79
|
return false;
|
|
41
80
|
}
|
|
81
|
+
function assertRGXConvertibleToken(value) {
|
|
82
|
+
if (!isRGXConvertibleToken(value)) {
|
|
83
|
+
throw new errors_1.RGXInvalidTokenError(`Invalid convertible token`, 'object with a toRgx method that returns a valid token', value);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
42
86
|
function rgxTokenType(value) {
|
|
43
87
|
if (isRGXNoOpToken(value))
|
|
44
88
|
return 'no-op';
|
|
@@ -52,14 +96,14 @@ function rgxTokenType(value) {
|
|
|
52
96
|
return value.map(rgxTokenType);
|
|
53
97
|
// Ignoring this line since it should be impossible to reach if the types are correct, but we need it to satisfy the return type
|
|
54
98
|
/* istanbul ignore next */
|
|
55
|
-
throw new
|
|
99
|
+
throw new errors_1.RGXInvalidTokenError(`Invalid RGX token: ${value}`, null, value);
|
|
56
100
|
}
|
|
57
101
|
function rgxTokenFromType(type, value) {
|
|
58
102
|
// Ignoring this line because the function is entirely a TypeScript utility that doesn't need to be tested at runtime.
|
|
59
103
|
/* istanbul ignore next */
|
|
60
104
|
return value;
|
|
61
105
|
}
|
|
62
|
-
function
|
|
106
|
+
function isValidRegexString(value) {
|
|
63
107
|
try {
|
|
64
108
|
new RegExp(value);
|
|
65
109
|
return true;
|
|
@@ -68,6 +112,24 @@ function isValidRegex(value) {
|
|
|
68
112
|
return false;
|
|
69
113
|
}
|
|
70
114
|
}
|
|
115
|
+
function assertValidRegexString(value) {
|
|
116
|
+
if (!isValidRegexString(value)) {
|
|
117
|
+
throw new errors_1.RGXInvalidRegexStringError(`Invalid regex string: ${value}`, value);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
function isValidVanillaRegexFlags(value) {
|
|
121
|
+
const patternMatch = /^[gimsuy]*$/.test(value);
|
|
122
|
+
if (!patternMatch)
|
|
123
|
+
return false;
|
|
124
|
+
// No repeated flags allowed
|
|
125
|
+
const flagsSet = new Set(value);
|
|
126
|
+
return flagsSet.size === value.length;
|
|
127
|
+
}
|
|
128
|
+
function assertValidVanillaRegexFlags(value) {
|
|
129
|
+
if (!isValidVanillaRegexFlags(value)) {
|
|
130
|
+
throw new errors_1.RGXInvalidVanillaRegexFlagsError(`Invalid vanilla regex flags: ${value}`, value);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
71
133
|
function escapeRegex(value) {
|
|
72
134
|
return value.replaceAll(/[\-\^\$.*+?^${}()|[\]\\]/g, '\\$&');
|
|
73
135
|
}
|
|
@@ -92,20 +154,23 @@ function resolveRGXToken(token) {
|
|
|
92
154
|
}
|
|
93
155
|
// Ignoring this line since it should be impossible to reach if the types are correct, but we need it to satisfy the return type
|
|
94
156
|
/* istanbul ignore next */
|
|
95
|
-
throw new
|
|
157
|
+
throw new errors_1.RGXInvalidTokenError(`Invalid RGX token: ${token}`, null, token);
|
|
96
158
|
}
|
|
97
159
|
// Wrapper for letting an array of tokens be resolved as a concatenation instead of a union.
|
|
98
160
|
function rgxConcat(tokens) {
|
|
99
161
|
return tokens.map(resolveRGXToken).join('');
|
|
100
162
|
}
|
|
101
|
-
function rgx(
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
pattern +=
|
|
163
|
+
function rgx(flags = '') {
|
|
164
|
+
assertValidVanillaRegexFlags(flags);
|
|
165
|
+
return (strings, ...tokens) => {
|
|
166
|
+
let pattern = '';
|
|
167
|
+
const resolvedTokens = tokens.map(resolveRGXToken);
|
|
168
|
+
for (let i = 0; i < strings.length; i++) {
|
|
169
|
+
pattern += strings[i];
|
|
170
|
+
if (i < resolvedTokens.length) {
|
|
171
|
+
pattern += resolvedTokens[i];
|
|
172
|
+
}
|
|
108
173
|
}
|
|
109
|
-
|
|
110
|
-
|
|
174
|
+
return new RegExp(pattern, flags);
|
|
175
|
+
};
|
|
111
176
|
}
|