@ptolemy2002/rgx 1.0.0 → 1.1.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 +28 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.js +13 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,6 +6,7 @@ A library for easy construction and validation of regular expressions in TypeScr
|
|
|
6
6
|
import { Branded } from "@ptolemy2002/ts-brand-utils";
|
|
7
7
|
|
|
8
8
|
type RGXNoOpToken = null | undefined;
|
|
9
|
+
type RGXLiteralToken = RegExp;
|
|
9
10
|
type RGXNativeToken = string | number | boolean | RGXNoOpToken;
|
|
10
11
|
type RGXConvertibleToken = { toRgx: () => RGXNativeToken | RGXNativeToken[] };
|
|
11
12
|
type RGXToken = RGXNativeToken | RGXConvertibleToken | RGXToken[];
|
|
@@ -36,6 +37,19 @@ Checks if the given value is a no-op token (`null` or `undefined`).
|
|
|
36
37
|
#### Returns
|
|
37
38
|
- `boolean`: `true` if the value is a no-op token, otherwise `false`.
|
|
38
39
|
|
|
40
|
+
### isRGXLiteralToken
|
|
41
|
+
```typescript
|
|
42
|
+
function isRGXLiteralToken(value: unknown): value is RGXLiteralToken
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Checks if the given value is a literal token (a `RegExp` object).
|
|
46
|
+
|
|
47
|
+
#### Parameters
|
|
48
|
+
- `value` (`unknown`): The value to check.
|
|
49
|
+
|
|
50
|
+
#### Returns
|
|
51
|
+
- `boolean`: `true` if the value is a literal token, otherwise `false`.
|
|
52
|
+
|
|
39
53
|
### isRGXNativeToken
|
|
40
54
|
```typescript
|
|
41
55
|
function isRGXNativeToken(value: unknown): value is RGXNativeToken
|
|
@@ -132,7 +146,7 @@ Escapes special regex characters in the given string and brands the result as a
|
|
|
132
146
|
function resolveRGXToken(token: RGXToken): string
|
|
133
147
|
```
|
|
134
148
|
|
|
135
|
-
Resolves an RGX token to a string. No-op tokens resolve to an empty string, 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).
|
|
149
|
+
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).
|
|
136
150
|
|
|
137
151
|
#### Parameters
|
|
138
152
|
- `token` (`RGXToken`): The RGX token to resolve.
|
|
@@ -140,6 +154,19 @@ Resolves an RGX token to a string. No-op tokens resolve to an empty string, nati
|
|
|
140
154
|
#### Returns
|
|
141
155
|
- `string`: The resolved string representation of the RGX token.
|
|
142
156
|
|
|
157
|
+
### rgxConcat
|
|
158
|
+
```typescript
|
|
159
|
+
function rgxConcat(tokens: RGXToken[]): string
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
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.
|
|
163
|
+
|
|
164
|
+
#### Parameters
|
|
165
|
+
- `tokens` (`RGXToken[]`): The array of RGX tokens to resolve and concatenate.
|
|
166
|
+
|
|
167
|
+
#### Returns
|
|
168
|
+
- `string`: The concatenated string representation of the resolved RGX tokens.
|
|
169
|
+
|
|
143
170
|
### rgx
|
|
144
171
|
```typescript
|
|
145
172
|
function rgx(strings: TemplateStringsArray, ...tokens: RGXToken[]): RegExp
|
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
import { Branded } from "@ptolemy2002/ts-brand-utils";
|
|
2
2
|
export type RGXNoOpToken = null | undefined;
|
|
3
|
+
export type RGXLiteralToken = RegExp;
|
|
3
4
|
export type RGXNativeToken = string | number | boolean | RGXNoOpToken;
|
|
4
5
|
export type RGXConvertibleToken = {
|
|
5
6
|
toRgx: () => RGXNativeToken | RGXNativeToken[];
|
|
6
7
|
};
|
|
7
|
-
export type RGXToken = RGXNativeToken | RGXConvertibleToken | RGXToken[];
|
|
8
|
-
export type RGXTokenType = 'no-op' | 'native' | 'convertible' | RGXTokenType[];
|
|
9
|
-
export type RGXTokenFromType<T extends RGXTokenType> = T extends 'no-op' ? RGXNoOpToken : T extends 'native' ? RGXNativeToken : T extends 'convertible' ? RGXConvertibleToken : T extends RGXTokenType[] ? {
|
|
8
|
+
export type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
|
|
9
|
+
export type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | RGXTokenType[];
|
|
10
|
+
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[] ? {
|
|
10
11
|
[K in keyof T]: T[K] extends RGXTokenType ? RGXTokenFromType<T[K]> : never;
|
|
11
12
|
} : never;
|
|
12
13
|
export declare const validRegexSymbol: unique symbol;
|
|
13
14
|
export type ValidRegexBrandSymbol = typeof validRegexSymbol;
|
|
14
15
|
export type ValidRegexString = Branded<string, [ValidRegexBrandSymbol]>;
|
|
15
16
|
export declare function isRGXNoOpToken(value: unknown): value is RGXNoOpToken;
|
|
17
|
+
export declare function isRGXLiteralToken(value: unknown): value is RGXLiteralToken;
|
|
16
18
|
export declare function isRGXNativeToken(value: unknown): value is RGXNativeToken;
|
|
17
19
|
export declare function isRGXConvertibleToken(value: unknown): value is RGXConvertibleToken;
|
|
18
20
|
export declare function rgxTokenType(value: RGXToken): RGXTokenType;
|
|
@@ -20,4 +22,5 @@ export declare function rgxTokenFromType<T extends RGXTokenType>(type: T, value:
|
|
|
20
22
|
export declare function isValidRegex(value: string): value is ValidRegexString;
|
|
21
23
|
export declare function escapeRegex(value: string): ValidRegexString;
|
|
22
24
|
export declare function resolveRGXToken(token: RGXToken): string;
|
|
25
|
+
export declare function rgxConcat(tokens: RGXToken[]): string;
|
|
23
26
|
export default function rgx(strings: TemplateStringsArray, ...tokens: RGXToken[]): RegExp;
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.validRegexSymbol = void 0;
|
|
7
7
|
exports.isRGXNoOpToken = isRGXNoOpToken;
|
|
8
|
+
exports.isRGXLiteralToken = isRGXLiteralToken;
|
|
8
9
|
exports.isRGXNativeToken = isRGXNativeToken;
|
|
9
10
|
exports.isRGXConvertibleToken = isRGXConvertibleToken;
|
|
10
11
|
exports.rgxTokenType = rgxTokenType;
|
|
@@ -12,12 +13,16 @@ exports.rgxTokenFromType = rgxTokenFromType;
|
|
|
12
13
|
exports.isValidRegex = isValidRegex;
|
|
13
14
|
exports.escapeRegex = escapeRegex;
|
|
14
15
|
exports.resolveRGXToken = resolveRGXToken;
|
|
16
|
+
exports.rgxConcat = rgxConcat;
|
|
15
17
|
exports.default = rgx;
|
|
16
18
|
const is_callable_1 = __importDefault(require("is-callable"));
|
|
17
19
|
exports.validRegexSymbol = Symbol('ValidRegex');
|
|
18
20
|
function isRGXNoOpToken(value) {
|
|
19
21
|
return value === null || value === undefined;
|
|
20
22
|
}
|
|
23
|
+
function isRGXLiteralToken(value) {
|
|
24
|
+
return value instanceof RegExp;
|
|
25
|
+
}
|
|
21
26
|
function isRGXNativeToken(value) {
|
|
22
27
|
return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || isRGXNoOpToken(value);
|
|
23
28
|
}
|
|
@@ -37,6 +42,8 @@ function isRGXConvertibleToken(value) {
|
|
|
37
42
|
function rgxTokenType(value) {
|
|
38
43
|
if (isRGXNoOpToken(value))
|
|
39
44
|
return 'no-op';
|
|
45
|
+
if (isRGXLiteralToken(value))
|
|
46
|
+
return 'literal';
|
|
40
47
|
if (isRGXNativeToken(value))
|
|
41
48
|
return 'native';
|
|
42
49
|
if (isRGXConvertibleToken(value))
|
|
@@ -67,6 +74,8 @@ function escapeRegex(value) {
|
|
|
67
74
|
function resolveRGXToken(token) {
|
|
68
75
|
if (isRGXNoOpToken(token))
|
|
69
76
|
return '';
|
|
77
|
+
if (isRGXLiteralToken(token))
|
|
78
|
+
return '(?:' + token.source + ')';
|
|
70
79
|
if (isRGXNativeToken(token))
|
|
71
80
|
return escapeRegex(String(token));
|
|
72
81
|
if (isRGXConvertibleToken(token)) {
|
|
@@ -85,6 +94,10 @@ function resolveRGXToken(token) {
|
|
|
85
94
|
/* istanbul ignore next */
|
|
86
95
|
throw new TypeError(`Invalid RGX token: ${token}`);
|
|
87
96
|
}
|
|
97
|
+
// Wrapper for letting an array of tokens be resolved as a concatenation instead of a union.
|
|
98
|
+
function rgxConcat(tokens) {
|
|
99
|
+
return tokens.map(resolveRGXToken).join('');
|
|
100
|
+
}
|
|
88
101
|
function rgx(strings, ...tokens) {
|
|
89
102
|
let pattern = '';
|
|
90
103
|
const resolvedTokens = tokens.map(resolveRGXToken);
|