@ptolemy2002/rgx 5.3.0 → 5.5.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 +182 -3
- package/dist/class/base.d.ts +1 -1
- package/dist/class/base.js +1 -1
- package/dist/class/init.js +2 -2
- package/dist/constants.d.ts +8 -0
- package/dist/constants.js +187 -0
- package/dist/errors/base.d.ts +1 -1
- package/dist/errors/constantConflict.d.ts +6 -0
- package/dist/errors/constantConflict.js +15 -0
- package/dist/errors/index.d.ts +2 -0
- package/dist/errors/index.js +2 -0
- package/dist/errors/invalidConstantKey.d.ts +6 -0
- package/dist/errors/invalidConstantKey.js +15 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +3 -2
- package/dist/internal/taggedTemplateToArray.d.ts +1 -1
- package/dist/internal/taggedTemplateToArray.js +11 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -54,7 +54,7 @@ type RGXTokenFromType<T extends RGXTokenTypeGuardInput> =
|
|
|
54
54
|
// ... see source for full definition
|
|
55
55
|
;
|
|
56
56
|
|
|
57
|
-
type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_REGEX_FLAGS' | 'INVALID_VANILLA_REGEX_FLAGS' | 'NOT_IMPLEMENTED' | 'NOT_SUPPORTED' | 'INVALID_IDENTIFIER' | 'OUT_OF_BOUNDS' | 'INVALID_FLAG_TRANSFORMER_KEY' | 'FLAG_TRANSFORMER_CONFLICT' | 'INSERTION_REJECTED';
|
|
57
|
+
type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_REGEX_FLAGS' | 'INVALID_VANILLA_REGEX_FLAGS' | 'NOT_IMPLEMENTED' | 'NOT_SUPPORTED' | 'INVALID_IDENTIFIER' | 'OUT_OF_BOUNDS' | 'INVALID_FLAG_TRANSFORMER_KEY' | 'FLAG_TRANSFORMER_CONFLICT' | 'CONSTANT_CONFLICT' | 'INVALID_CONSTANT_KEY' | 'INSERTION_REJECTED';
|
|
58
58
|
|
|
59
59
|
type RangeObject = {
|
|
60
60
|
min?: number | null;
|
|
@@ -218,6 +218,32 @@ constructor(message: string, got: string)
|
|
|
218
218
|
#### Properties
|
|
219
219
|
- `got` (`string`): The conflicting key string.
|
|
220
220
|
|
|
221
|
+
### RGXInvalidConstantKeyError extends RGXError
|
|
222
|
+
A specific error class for invalid constant keys. This error is thrown when attempting to access or assert an RGX constant with a name that does not exist. The error code is set to `INVALID_CONSTANT_KEY` on instantiation.
|
|
223
|
+
|
|
224
|
+
#### Constructor
|
|
225
|
+
```typescript
|
|
226
|
+
constructor(message: string, got: string)
|
|
227
|
+
```
|
|
228
|
+
- `message` (`string`): The error message.
|
|
229
|
+
- `got` (`string`): The constant name that was not found.
|
|
230
|
+
|
|
231
|
+
#### Properties
|
|
232
|
+
- `got` (`string`): The constant name that was not found.
|
|
233
|
+
|
|
234
|
+
### RGXConstantConflictError extends RGXError
|
|
235
|
+
A specific error class for constant name conflicts. This error is thrown when attempting to define an RGX constant with a name that is already in use. The error code is set to `CONSTANT_CONFLICT` on instantiation.
|
|
236
|
+
|
|
237
|
+
#### Constructor
|
|
238
|
+
```typescript
|
|
239
|
+
constructor(message: string, got: string)
|
|
240
|
+
```
|
|
241
|
+
- `message` (`string`): The error message.
|
|
242
|
+
- `got` (`string`): The conflicting constant name.
|
|
243
|
+
|
|
244
|
+
#### Properties
|
|
245
|
+
- `got` (`string`): The conflicting constant name.
|
|
246
|
+
|
|
221
247
|
### RGXInsertionRejectedError extends RGXError
|
|
222
248
|
A specific error class for token insertion rejection. This error is thrown when a convertible token's `rgxAcceptInsertion` method returns `false` or a string (rejection reason) during pattern construction via `rgx`, `rgxa`, or `rgxConcat`. The error code is set to `INSERTION_REJECTED` on instantiation.
|
|
223
249
|
|
|
@@ -305,7 +331,7 @@ An abstract base class for creating custom RGX token classes. Subclasses must im
|
|
|
305
331
|
#### Methods
|
|
306
332
|
- `rgxAcceptInsertion(tokens: RGXToken[], flags: ValidRegexFlags) => string | boolean`: Called during pattern construction (via `rgx`, `rgxa`, or `rgxConcat`) to allow the token to reject its own insertion based on the surrounding tokens and the pattern's flags. Returns `true` to accept insertion (the default), `false` to reject with no reason, or a string to reject with a reason message. When a token rejects insertion, an `RGXInsertionRejectedError` is thrown. Subclasses can override this to enforce constraints such as requiring certain flags to be present.
|
|
307
333
|
- `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.
|
|
308
|
-
- `group(args?: RGXGroupTokenArgs) => RGXGroupToken`: Wraps this token in an `RGXGroupToken` with the provided arguments. The `args` parameter defaults to `{}`, which creates a capturing group with no name. This is a convenience method that creates a new `RGXGroupToken` with `this` as the
|
|
334
|
+
- `group(args?: RGXGroupTokenArgs, ...others: RGXTokenCollectionInput[]) => RGXGroupToken`: Wraps this token in an `RGXGroupToken` with the provided arguments. The `args` parameter defaults to `{}`, which creates a capturing group with no name. Any additional `others` are included in the group alongside `this`. This is a convenience method that creates a new `RGXGroupToken` with `this` and the `others` as the tokens.
|
|
309
335
|
- `repeat(min?: number, max?: number | null, lazy?: boolean) => RGXRepeatToken`: Wraps this token in an `RGXRepeatToken` with the given repetition bounds. `min` defaults to `1`, `max` defaults to `min`, `lazy` defaults to `false`. Pass `null` for `max` to allow unlimited repetitions. When `lazy` is `true`, the resulting quantifier will be non-greedy. This is a convenience method that creates a new `RGXRepeatToken` with `this` as the token. Throws `RGXNotSupportedError` if called on a token with `rgxIsRepeatable` set to `false` (e.g., `RGXLookaroundToken`).
|
|
310
336
|
- `optional(lazy?: boolean) => RGXRepeatToken`: Shorthand for `repeat(0, 1, lazy)`. Wraps this token in an `RGXRepeatToken` that matches the token zero or one times. `lazy` defaults to `false`. Throws `RGXNotSupportedError` if called on a token with `rgxIsRepeatable` set to `false` (e.g., `RGXLookaroundToken`).
|
|
311
337
|
- `asLookahead(positive?: boolean) => RGXLookaheadToken`: Wraps this token in an `RGXLookaheadToken`. `positive` defaults to `true`. If this token is already an `RGXLookaheadToken`, it is returned as-is without re-wrapping.
|
|
@@ -924,11 +950,13 @@ A helper function that resolves an array of RGX tokens and concatenates their re
|
|
|
924
950
|
|
|
925
951
|
### rgx
|
|
926
952
|
```typescript
|
|
927
|
-
function rgx(flags?: string): (strings: TemplateStringsArray, ...tokens: RGXToken[]) => ExtRegExp
|
|
953
|
+
function rgx(flags?: string, multiline?: boolean): (strings: TemplateStringsArray, ...tokens: RGXToken[]) => ExtRegExp
|
|
928
954
|
```
|
|
929
955
|
|
|
930
956
|
Creates and returns a template tag function that constructs an `ExtRegExp` 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. Before constructing the pattern, any convertible token that defines `rgxAcceptInsertion` is checked; if it returns `false` or a string, an `RGXInsertionRejectedError` is thrown with details about the reason and exactly where the rejection occurred.
|
|
931
957
|
|
|
958
|
+
When `multiline` is `true` (the default), the literal string parts of the template are processed to strip newlines, trim leading whitespace from each line, and remove empty lines, then joined together. This allows you to write regex patterns across multiple lines in the source code for readability without the newlines and indentation becoming part of the pattern. Only the literal string parts between tokens are affected — interpolated values (tokens) are preserved as-is, including string tokens passed via `${"..."}`. When `multiline` is `false`, all literal string parts are preserved exactly as written, including newlines and whitespace.
|
|
959
|
+
|
|
932
960
|
The provided `flags` are passed as `currentFlags` to the resolver, enabling inline modifier groups for any `RegExp` literal tokens whose localizable flags (`i`, `m`, `s`) differ from the parent flags. For example, embedding `/foo/i` in a no-flag context produces `(?i:foo)`, while embedding `/bar/` in an `i`-flag context produces `(?-i:bar)`.
|
|
933
961
|
|
|
934
962
|
Example usages:
|
|
@@ -945,11 +973,26 @@ const pattern3 = rgx()`${beginning}value: ${[word, optionalDigit]}${end}`; // /^
|
|
|
945
973
|
|
|
946
974
|
const caseInsensitiveWord = /hello/i;
|
|
947
975
|
const pattern4 = rgx()`${beginning}${caseInsensitiveWord} world${end}`; // /^(?i:hello) world$/ - "hello" matches case-insensitively via an inline modifier group, while " world" remains case-sensitive
|
|
976
|
+
|
|
977
|
+
// Multiline template for readability (multiline is true by default):
|
|
978
|
+
const pattern5 = rgx()`
|
|
979
|
+
${beginning}
|
|
980
|
+
testing ${word}
|
|
981
|
+
${end}
|
|
982
|
+
`; // /^testing \w+$/ - same as pattern, but written across multiple lines
|
|
983
|
+
|
|
984
|
+
// Preserving literal newlines with multiline mode:
|
|
985
|
+
const pattern6 = rgx()`
|
|
986
|
+
foo
|
|
987
|
+
bar${rgxConstant("newline")}
|
|
988
|
+
baz
|
|
989
|
+
`; // /foobar\nbaz/ - the constant newline is preserved, but template newlines are stripped
|
|
948
990
|
```
|
|
949
991
|
|
|
950
992
|
#### Parameters
|
|
951
993
|
**Direct**
|
|
952
994
|
- `flags` (`string`, optional): The regex flags to apply to the resulting `ExtRegExp` object (e.g., 'g', 'i', 'm', or custom registered flags). If not provided, no flags will be applied. If provided and not valid regex flags (vanilla or registered custom), an `RGXInvalidRegexFlagsError` will be thrown.
|
|
995
|
+
- `multiline` (`boolean`, optional): Whether to strip newlines and trim leading whitespace from the literal string parts of the template. Defaults to `true`. When `true`, each literal string part is split by newlines, each line has its leading whitespace trimmed, empty lines are removed, and the remaining lines are joined together. Interpolated tokens (including string tokens via `${"..."}`) are not affected. When `false`, literal string parts are preserved exactly as written.
|
|
953
996
|
|
|
954
997
|
**Template Tag**
|
|
955
998
|
- `strings` (`TemplateStringsArray`): The literal parts of the template string.
|
|
@@ -1270,6 +1313,142 @@ Creates a clone of the given RGX token to the given depth, provided that the tok
|
|
|
1270
1313
|
#### Returns
|
|
1271
1314
|
- `T`: The cloned token.
|
|
1272
1315
|
|
|
1316
|
+
### listRGXConstants
|
|
1317
|
+
```typescript
|
|
1318
|
+
function listRGXConstants(): string[]
|
|
1319
|
+
```
|
|
1320
|
+
|
|
1321
|
+
Returns the names of all currently defined RGX constants.
|
|
1322
|
+
|
|
1323
|
+
#### Returns
|
|
1324
|
+
- `string[]`: An array of constant names.
|
|
1325
|
+
|
|
1326
|
+
### hasRGXConstant
|
|
1327
|
+
```typescript
|
|
1328
|
+
function hasRGXConstant(name: string): boolean
|
|
1329
|
+
```
|
|
1330
|
+
|
|
1331
|
+
Checks if an RGX constant with the given name exists.
|
|
1332
|
+
|
|
1333
|
+
#### Parameters
|
|
1334
|
+
- `name` (`string`): The constant name to check.
|
|
1335
|
+
|
|
1336
|
+
#### Returns
|
|
1337
|
+
- `boolean`: `true` if the constant exists, otherwise `false`.
|
|
1338
|
+
|
|
1339
|
+
### assertHasRGXConstant
|
|
1340
|
+
```typescript
|
|
1341
|
+
function assertHasRGXConstant(name: string): void
|
|
1342
|
+
```
|
|
1343
|
+
|
|
1344
|
+
Asserts that an RGX constant with the given name exists. If the assertion fails, an `RGXInvalidConstantKeyError` will be thrown.
|
|
1345
|
+
|
|
1346
|
+
#### Parameters
|
|
1347
|
+
- `name` (`string`): The constant name to assert.
|
|
1348
|
+
|
|
1349
|
+
#### Returns
|
|
1350
|
+
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
1351
|
+
|
|
1352
|
+
### assertNotHasRGXConstant
|
|
1353
|
+
```typescript
|
|
1354
|
+
function assertNotHasRGXConstant(name: string): void
|
|
1355
|
+
```
|
|
1356
|
+
|
|
1357
|
+
Asserts that an RGX constant with the given name does not exist. If the assertion fails, an `RGXConstantConflictError` will be thrown.
|
|
1358
|
+
|
|
1359
|
+
#### Parameters
|
|
1360
|
+
- `name` (`string`): The constant name to assert.
|
|
1361
|
+
|
|
1362
|
+
#### Returns
|
|
1363
|
+
- `void`: This function does not return a value, but will throw an error if the assertion fails.
|
|
1364
|
+
|
|
1365
|
+
### defineRGXConstant
|
|
1366
|
+
```typescript
|
|
1367
|
+
function defineRGXConstant(name: string, value: RGXToken): RGXToken
|
|
1368
|
+
```
|
|
1369
|
+
|
|
1370
|
+
Defines a new RGX constant with the given name and value. If the value is a native token (string, number, boolean, or no-op), it is automatically wrapped in an `RGXClassWrapperToken` before being stored. This ensures that native-valued constants are not stripped by multiline template processing in `rgx`, since only the literal string parts of the template are affected by multiline mode. Throws an `RGXConstantConflictError` if a constant with the same name already exists.
|
|
1371
|
+
|
|
1372
|
+
#### Parameters
|
|
1373
|
+
- `name` (`string`): The name for the constant.
|
|
1374
|
+
- `value` (`RGXToken`): The token value to associate with the name. Native tokens are automatically wrapped in `RGXClassWrapperToken`.
|
|
1375
|
+
|
|
1376
|
+
#### Returns
|
|
1377
|
+
- `RGXToken`: The stored value (after wrapping, if applicable).
|
|
1378
|
+
|
|
1379
|
+
### rgxConstant
|
|
1380
|
+
```typescript
|
|
1381
|
+
function rgxConstant(name: string): RGXToken
|
|
1382
|
+
```
|
|
1383
|
+
|
|
1384
|
+
Retrieves the value of an RGX constant by name. Throws an `RGXInvalidConstantKeyError` if no constant with the given name exists.
|
|
1385
|
+
|
|
1386
|
+
#### Parameters
|
|
1387
|
+
- `name` (`string`): The constant name to retrieve.
|
|
1388
|
+
|
|
1389
|
+
#### Returns
|
|
1390
|
+
- `RGXToken`: The token value associated with the constant name.
|
|
1391
|
+
|
|
1392
|
+
### deleteRGXConstant
|
|
1393
|
+
```typescript
|
|
1394
|
+
function deleteRGXConstant(name: string): void
|
|
1395
|
+
```
|
|
1396
|
+
|
|
1397
|
+
Deletes an existing RGX constant by name. Throws an `RGXInvalidConstantKeyError` if no constant with the given name exists.
|
|
1398
|
+
|
|
1399
|
+
#### Parameters
|
|
1400
|
+
- `name` (`string`): The constant name to delete.
|
|
1401
|
+
|
|
1402
|
+
#### Returns
|
|
1403
|
+
- `void`: This function does not return a value, but will throw an error if the constant does not exist.
|
|
1404
|
+
|
|
1405
|
+
## Built-in Constants
|
|
1406
|
+
The library defines the following built-in constants, which are available immediately after import. Each can be retrieved via `rgxConstant(name)`.
|
|
1407
|
+
|
|
1408
|
+
### Control Characters
|
|
1409
|
+
Since these are defined as native tokens (strings), they are automatically wrapped in `RGXClassWrapperToken` by `defineRGXConstant`, ensuring they are preserved in multiline mode.
|
|
1410
|
+
|
|
1411
|
+
| Name | Resolves To | Description |
|
|
1412
|
+
| --- | --- | --- |
|
|
1413
|
+
| `"newline"` | `\n` | Newline character |
|
|
1414
|
+
| `"carriage-return"` | `\r` | Carriage return character |
|
|
1415
|
+
| `"tab"` | `\t` | Tab character |
|
|
1416
|
+
| `"null"` | `\0` | Null character |
|
|
1417
|
+
| `"form-feed"` | `\f` | Form feed character |
|
|
1418
|
+
|
|
1419
|
+
### Special Characters
|
|
1420
|
+
| Name | Resolves To | Description |
|
|
1421
|
+
| --- | --- | --- |
|
|
1422
|
+
| `"any"` | `.` | Matches any single character (except newline by default) |
|
|
1423
|
+
| `"start"` | `^` | Start of string anchor |
|
|
1424
|
+
| `"end"` | `$` | End of string anchor |
|
|
1425
|
+
| `"word-bound"` | `\b` | Word boundary |
|
|
1426
|
+
| `"non-word-bound"` | `\B` | Non-word boundary |
|
|
1427
|
+
| `"word-bound-start"` | `(?<=\W)(?=\w)` | Start of a word |
|
|
1428
|
+
| `"word-bound-end"` | `(?<=\w)(?=\W)` | End of a word |
|
|
1429
|
+
|
|
1430
|
+
### Character Sets
|
|
1431
|
+
| Name | Resolves To | Description |
|
|
1432
|
+
| --- | --- | --- |
|
|
1433
|
+
| `"letter"` | `[a-zA-Z]` | Any letter (uppercase or lowercase) |
|
|
1434
|
+
| `"lowercase-letter"` | `[a-z]` | Any lowercase letter |
|
|
1435
|
+
| `"uppercase-letter"` | `[A-Z]` | Any uppercase letter |
|
|
1436
|
+
| `"non-letter"` | `[^a-zA-Z]` | Any character that is not a letter |
|
|
1437
|
+
| `"alphanumeric"` | `[a-zA-Z0-9]` | Any letter or digit |
|
|
1438
|
+
| `"non-alphanumeric"` | `[^a-zA-Z0-9]` | Any character that is not a letter or digit |
|
|
1439
|
+
|
|
1440
|
+
### Predefined Character Sets
|
|
1441
|
+
| Name | Resolves To | Description |
|
|
1442
|
+
| --- | --- | --- |
|
|
1443
|
+
| `"digit"` | `\d` | Any digit |
|
|
1444
|
+
| `"non-digit"` | `\D` | Any non-digit |
|
|
1445
|
+
| `"whitespace"` | `\s` | Any whitespace character |
|
|
1446
|
+
| `"non-whitespace"` | `\S` | Any non-whitespace character |
|
|
1447
|
+
| `"vertical-whitespace"` | `\v` | Vertical whitespace character |
|
|
1448
|
+
| `"word-char"` | `\w` | Any word character (letter, digit, or underscore) |
|
|
1449
|
+
| `"non-word-char"` | `\W` | Any non-word character |
|
|
1450
|
+
| `"backspace"` | `[\b]` | Backspace character |
|
|
1451
|
+
|
|
1273
1452
|
## Peer Dependencies
|
|
1274
1453
|
- `@ptolemy2002/immutability-utils` ^2.0.0
|
|
1275
1454
|
- `@ptolemy2002/js-utils` ^3.2.2
|
package/dist/class/base.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export declare abstract class RGXClassToken implements RGXConvertibleToken {
|
|
|
16
16
|
get rgxIsRepeatable(): boolean;
|
|
17
17
|
get rgxGroupWrap(): boolean;
|
|
18
18
|
or(...others: RGXTokenCollectionInput[]): RGXClassUnionToken;
|
|
19
|
-
group(args?: RGXGroupTokenArgs): RGXGroupToken;
|
|
19
|
+
group(args?: RGXGroupTokenArgs, ...others: RGXTokenCollectionInput[]): RGXGroupToken;
|
|
20
20
|
repeat(min?: number, max?: number | null, lazy?: boolean): RGXRepeatToken;
|
|
21
21
|
optional(lazy?: boolean): RGXRepeatToken;
|
|
22
22
|
asLookahead(positive?: boolean): RGXLookaheadToken;
|
package/dist/class/base.js
CHANGED
|
@@ -19,7 +19,7 @@ class RGXClassToken {
|
|
|
19
19
|
or(...others) {
|
|
20
20
|
throw new errors_1.RGXNotImplementedError('RGXClassToken.or(...others)', 'call rgxClassInit() first.');
|
|
21
21
|
}
|
|
22
|
-
group(args = {}) {
|
|
22
|
+
group(args = {}, ...others) {
|
|
23
23
|
throw new errors_1.RGXNotImplementedError('RGXClassToken.group(args)', 'call rgxClassInit() first.');
|
|
24
24
|
}
|
|
25
25
|
repeat(min = 1, max = min, lazy = false) {
|
package/dist/class/init.js
CHANGED
|
@@ -25,8 +25,8 @@ function rgxClassInit() {
|
|
|
25
25
|
return new union_1.RGXClassUnionToken([this, ...filteredOthers]);
|
|
26
26
|
}
|
|
27
27
|
};
|
|
28
|
-
base_1.RGXClassToken.prototype.group = function (args = {}) {
|
|
29
|
-
return new group_1.RGXGroupToken(args, [this]);
|
|
28
|
+
base_1.RGXClassToken.prototype.group = function (args = {}, ...others) {
|
|
29
|
+
return new group_1.RGXGroupToken(args, [this, ...others]);
|
|
30
30
|
};
|
|
31
31
|
base_1.RGXClassToken.prototype.repeat = function (min = 1, max = min, lazy = false) {
|
|
32
32
|
if (lookaround_1.RGXLookaroundToken.check(this))
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { RGXToken } from "./types";
|
|
2
|
+
export declare function listRGXConstants(): string[];
|
|
3
|
+
export declare function hasRGXConstant(name: string): boolean;
|
|
4
|
+
export declare function assertHasRGXConstant(name: string): void;
|
|
5
|
+
export declare function assertNotHasRGXConstant(name: string): void;
|
|
6
|
+
export declare function defineRGXConstant(name: string, value: RGXToken): RegExp | import("./types").RGXConvertibleToken | RGXToken[];
|
|
7
|
+
export declare function rgxConstant(name: string): RGXToken;
|
|
8
|
+
export declare function deleteRGXConstant(name: string): void;
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.listRGXConstants = listRGXConstants;
|
|
4
|
+
exports.hasRGXConstant = hasRGXConstant;
|
|
5
|
+
exports.assertHasRGXConstant = assertHasRGXConstant;
|
|
6
|
+
exports.assertNotHasRGXConstant = assertNotHasRGXConstant;
|
|
7
|
+
exports.defineRGXConstant = defineRGXConstant;
|
|
8
|
+
exports.rgxConstant = rgxConstant;
|
|
9
|
+
exports.deleteRGXConstant = deleteRGXConstant;
|
|
10
|
+
const class_1 = require("./class");
|
|
11
|
+
const errors_1 = require("./errors");
|
|
12
|
+
const typeGuards_1 = require("./typeGuards");
|
|
13
|
+
const rgxConstants = {};
|
|
14
|
+
function listRGXConstants() {
|
|
15
|
+
return Object.keys(rgxConstants);
|
|
16
|
+
}
|
|
17
|
+
function hasRGXConstant(name) {
|
|
18
|
+
return name in rgxConstants;
|
|
19
|
+
}
|
|
20
|
+
function assertHasRGXConstant(name) {
|
|
21
|
+
if (!hasRGXConstant(name)) {
|
|
22
|
+
throw new errors_1.RGXInvalidConstantKeyError("Constant with name not found.", name);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function assertNotHasRGXConstant(name) {
|
|
26
|
+
if (hasRGXConstant(name)) {
|
|
27
|
+
throw new errors_1.RGXConstantConflictError("Constant with name already defined.", name);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function defineRGXConstant(name, value) {
|
|
31
|
+
assertNotHasRGXConstant(name);
|
|
32
|
+
// Not strings themselves so that they aren't removed in multiline mode.
|
|
33
|
+
rgxConstants[name] = (0, typeGuards_1.isRGXNativeToken)(value) ? (0, class_1.rgxClassWrapper)(value) : value;
|
|
34
|
+
return rgxConstants[name];
|
|
35
|
+
}
|
|
36
|
+
function rgxConstant(name) {
|
|
37
|
+
assertHasRGXConstant(name);
|
|
38
|
+
return rgxConstants[name];
|
|
39
|
+
}
|
|
40
|
+
function deleteRGXConstant(name) {
|
|
41
|
+
assertHasRGXConstant(name);
|
|
42
|
+
delete rgxConstants[name];
|
|
43
|
+
}
|
|
44
|
+
// Control Characters
|
|
45
|
+
defineRGXConstant("newline", "\n");
|
|
46
|
+
defineRGXConstant("carriage-return", "\r");
|
|
47
|
+
defineRGXConstant("tab", "\t");
|
|
48
|
+
defineRGXConstant("null", "\0");
|
|
49
|
+
defineRGXConstant("form-feed", "\f");
|
|
50
|
+
// Special Characters
|
|
51
|
+
defineRGXConstant("any", {
|
|
52
|
+
rgxGroupWrap: false,
|
|
53
|
+
toRgx() {
|
|
54
|
+
return /./;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
defineRGXConstant("start", {
|
|
58
|
+
rgxGroupWrap: false,
|
|
59
|
+
toRgx() {
|
|
60
|
+
return /^/;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
defineRGXConstant("end", {
|
|
64
|
+
rgxGroupWrap: false,
|
|
65
|
+
toRgx() {
|
|
66
|
+
return /$/;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
defineRGXConstant("word-bound", {
|
|
70
|
+
rgxGroupWrap: false,
|
|
71
|
+
toRgx() {
|
|
72
|
+
return /\b/;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
defineRGXConstant("non-word-bound", {
|
|
76
|
+
rgxGroupWrap: false,
|
|
77
|
+
toRgx() {
|
|
78
|
+
return /\B/;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
defineRGXConstant("word-bound-start", {
|
|
82
|
+
rgxGroupWrap: false,
|
|
83
|
+
toRgx() {
|
|
84
|
+
// Make sure there is a non-word character before and a word character after
|
|
85
|
+
return /(?<=\W)(?=\w)/;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
defineRGXConstant("word-bound-end", {
|
|
89
|
+
rgxGroupWrap: false,
|
|
90
|
+
toRgx() {
|
|
91
|
+
// Make sure there is a word character before and a non-word character after
|
|
92
|
+
return /(?<=\w)(?=\W)/;
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
// Character Sets
|
|
96
|
+
defineRGXConstant("letter", {
|
|
97
|
+
rgxIsGroup: true,
|
|
98
|
+
rgxGroupWrap: false,
|
|
99
|
+
toRgx() {
|
|
100
|
+
return /[a-zA-Z]/;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
defineRGXConstant("lowercase-letter", {
|
|
104
|
+
rgxIsGroup: true,
|
|
105
|
+
rgxGroupWrap: false,
|
|
106
|
+
toRgx() {
|
|
107
|
+
return /[a-z]/;
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
defineRGXConstant("uppercase-letter", {
|
|
111
|
+
rgxIsGroup: true,
|
|
112
|
+
rgxGroupWrap: false,
|
|
113
|
+
toRgx() {
|
|
114
|
+
return /[A-Z]/;
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
defineRGXConstant("non-letter", {
|
|
118
|
+
rgxIsGroup: true,
|
|
119
|
+
rgxGroupWrap: false,
|
|
120
|
+
toRgx() {
|
|
121
|
+
return /[^a-zA-Z]/;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
defineRGXConstant("alphanumeric", {
|
|
125
|
+
rgxIsGroup: true,
|
|
126
|
+
rgxGroupWrap: false,
|
|
127
|
+
toRgx() {
|
|
128
|
+
return /[a-zA-Z0-9]/;
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
defineRGXConstant("non-alphanumeric", {
|
|
132
|
+
rgxIsGroup: true,
|
|
133
|
+
rgxGroupWrap: false,
|
|
134
|
+
toRgx() {
|
|
135
|
+
return /[^a-zA-Z0-9]/;
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
// Predefined Character Sets
|
|
139
|
+
defineRGXConstant("digit", {
|
|
140
|
+
rgxGroupWrap: false,
|
|
141
|
+
toRgx() {
|
|
142
|
+
return /\d/;
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
defineRGXConstant("non-digit", {
|
|
146
|
+
rgxGroupWrap: false,
|
|
147
|
+
toRgx() {
|
|
148
|
+
return /\D/;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
defineRGXConstant("whitespace", {
|
|
152
|
+
rgxGroupWrap: false,
|
|
153
|
+
toRgx() {
|
|
154
|
+
return /\s/;
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
defineRGXConstant("non-whitespace", {
|
|
158
|
+
rgxGroupWrap: false,
|
|
159
|
+
toRgx() {
|
|
160
|
+
return /\S/;
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
defineRGXConstant("vertical-whitespace", {
|
|
164
|
+
rgxGroupWrap: false,
|
|
165
|
+
toRgx() {
|
|
166
|
+
return /\v/;
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
defineRGXConstant("word-char", {
|
|
170
|
+
rgxGroupWrap: false,
|
|
171
|
+
toRgx() {
|
|
172
|
+
return /\w/;
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
defineRGXConstant("non-word-char", {
|
|
176
|
+
rgxGroupWrap: false,
|
|
177
|
+
toRgx() {
|
|
178
|
+
return /\W/;
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
defineRGXConstant("backspace", {
|
|
182
|
+
rgxIsGroup: true,
|
|
183
|
+
rgxGroupWrap: false,
|
|
184
|
+
toRgx() {
|
|
185
|
+
return /[\b]/;
|
|
186
|
+
}
|
|
187
|
+
});
|
package/dist/errors/base.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_REGEX_FLAGS' | 'INVALID_VANILLA_REGEX_FLAGS' | 'NOT_IMPLEMENTED' | 'NOT_SUPPORTED' | 'INVALID_IDENTIFIER' | 'OUT_OF_BOUNDS' | 'INVALID_FLAG_TRANSFORMER_KEY' | 'FLAG_TRANSFORMER_CONFLICT' | 'INSERTION_REJECTED';
|
|
1
|
+
export type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_REGEX_FLAGS' | 'INVALID_VANILLA_REGEX_FLAGS' | 'NOT_IMPLEMENTED' | 'NOT_SUPPORTED' | 'INVALID_IDENTIFIER' | 'OUT_OF_BOUNDS' | 'INVALID_FLAG_TRANSFORMER_KEY' | 'FLAG_TRANSFORMER_CONFLICT' | 'CONSTANT_CONFLICT' | 'INVALID_CONSTANT_KEY' | 'INSERTION_REJECTED';
|
|
2
2
|
export declare class RGXError extends Error {
|
|
3
3
|
_message: string;
|
|
4
4
|
code: RGXErrorCode;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RGXConstantConflictError = void 0;
|
|
4
|
+
const errors_1 = require("./");
|
|
5
|
+
class RGXConstantConflictError extends errors_1.RGXError {
|
|
6
|
+
constructor(message, got) {
|
|
7
|
+
super(message, 'CONSTANT_CONFLICT');
|
|
8
|
+
this.name = 'RGXConstantConflictError';
|
|
9
|
+
this.got = got;
|
|
10
|
+
}
|
|
11
|
+
calcMessage(message) {
|
|
12
|
+
return `${message}; Got: ${JSON.stringify(this.got)}`;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.RGXConstantConflictError = RGXConstantConflictError;
|
package/dist/errors/index.d.ts
CHANGED
package/dist/errors/index.js
CHANGED
|
@@ -26,3 +26,5 @@ __exportStar(require("./invalidFlagTransformerKey"), exports);
|
|
|
26
26
|
__exportStar(require("./flagTransformerConflict"), exports);
|
|
27
27
|
__exportStar(require("./notSupported"), exports);
|
|
28
28
|
__exportStar(require("./insertionRejected"), exports);
|
|
29
|
+
__exportStar(require("./constantConflict"), exports);
|
|
30
|
+
__exportStar(require("./invalidConstantKey"), exports);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RGXInvalidConstantKeyError = void 0;
|
|
4
|
+
const errors_1 = require("./");
|
|
5
|
+
class RGXInvalidConstantKeyError extends errors_1.RGXError {
|
|
6
|
+
constructor(message, got) {
|
|
7
|
+
super(message, 'INVALID_CONSTANT_KEY');
|
|
8
|
+
this.name = 'RGXInvalidConstantKeyError';
|
|
9
|
+
this.got = got;
|
|
10
|
+
}
|
|
11
|
+
calcMessage(message) {
|
|
12
|
+
return `${message}; Got: ${JSON.stringify(this.got)}`;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.RGXInvalidConstantKeyError = RGXInvalidConstantKeyError;
|
package/dist/index.d.ts
CHANGED
|
@@ -11,5 +11,6 @@ export * from "./utils";
|
|
|
11
11
|
export * from "./ExtRegExp";
|
|
12
12
|
export * from "./flag-transformer";
|
|
13
13
|
export * from "./clone";
|
|
14
|
+
export * from "./constants";
|
|
14
15
|
export declare function rgxa(tokens: t.RGXToken[], flags?: string): ExtRegExp;
|
|
15
|
-
export default function rgx(flags?: string): (strings: TemplateStringsArray, ...tokens: t.RGXToken[]) => ExtRegExp;
|
|
16
|
+
export default function rgx(flags?: string, multiline?: boolean): (strings: TemplateStringsArray, ...tokens: t.RGXToken[]) => ExtRegExp;
|
package/dist/index.js
CHANGED
|
@@ -32,6 +32,7 @@ __exportStar(require("./utils"), exports);
|
|
|
32
32
|
__exportStar(require("./ExtRegExp"), exports);
|
|
33
33
|
__exportStar(require("./flag-transformer"), exports);
|
|
34
34
|
__exportStar(require("./clone"), exports);
|
|
35
|
+
__exportStar(require("./constants"), exports);
|
|
35
36
|
// Call this for certain class methods to work correctly
|
|
36
37
|
(0, class_1.rgxClassInit)();
|
|
37
38
|
// Call this for our custom flags to work correctly
|
|
@@ -42,9 +43,9 @@ function rgxa(tokens, flags = '') {
|
|
|
42
43
|
const pattern = (0, concat_1.rgxConcat)(tokens, true, flags);
|
|
43
44
|
return (0, ExtRegExp_1.extRegExp)(pattern, flags);
|
|
44
45
|
}
|
|
45
|
-
function rgx(flags = '') {
|
|
46
|
+
function rgx(flags = '', multiline = true) {
|
|
46
47
|
(0, ExtRegExp_1.assertValidRegexFlags)(flags);
|
|
47
48
|
return (strings, ...tokens) => {
|
|
48
|
-
return rgxa((0, internal_1.taggedTemplateToArray)(strings, tokens), flags);
|
|
49
|
+
return rgxa((0, internal_1.taggedTemplateToArray)(strings, tokens, multiline), flags);
|
|
49
50
|
};
|
|
50
51
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function taggedTemplateToArray<T>(strings: TemplateStringsArray, tokens: T[]): (string | T)[];
|
|
1
|
+
export declare function taggedTemplateToArray<T>(strings: TemplateStringsArray, tokens: T[], multiline: boolean): (string | T)[];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.taggedTemplateToArray = taggedTemplateToArray;
|
|
4
|
-
function taggedTemplateToArray(strings, tokens) {
|
|
4
|
+
function taggedTemplateToArray(strings, tokens, multiline) {
|
|
5
5
|
function isNullOrUndefined(value) {
|
|
6
6
|
return value === null || value === undefined;
|
|
7
7
|
}
|
|
@@ -10,8 +10,16 @@ function taggedTemplateToArray(strings, tokens) {
|
|
|
10
10
|
const string = strings[i];
|
|
11
11
|
const token = tokens[i];
|
|
12
12
|
// Strings always come before tokens
|
|
13
|
-
if (!isNullOrUndefined(string))
|
|
14
|
-
|
|
13
|
+
if (!isNullOrUndefined(string)) {
|
|
14
|
+
if (!multiline) {
|
|
15
|
+
array.push(string);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
// Remove all empty lines and trim whitespace from the start of each line.
|
|
19
|
+
let lines = string.split("\n").map(line => line.trimStart()).filter(line => line.length > 0).join("");
|
|
20
|
+
array.push(lines);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
15
23
|
if (!isNullOrUndefined(token))
|
|
16
24
|
array.push(token);
|
|
17
25
|
}
|