@ptolemy2002/rgx 5.4.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 +23 -4
- package/dist/constants.d.ts +1 -1
- package/dist/constants.js +5 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -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
|
@@ -950,11 +950,13 @@ A helper function that resolves an array of RGX tokens and concatenates their re
|
|
|
950
950
|
|
|
951
951
|
### rgx
|
|
952
952
|
```typescript
|
|
953
|
-
function rgx(flags?: string): (strings: TemplateStringsArray, ...tokens: RGXToken[]) => ExtRegExp
|
|
953
|
+
function rgx(flags?: string, multiline?: boolean): (strings: TemplateStringsArray, ...tokens: RGXToken[]) => ExtRegExp
|
|
954
954
|
```
|
|
955
955
|
|
|
956
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.
|
|
957
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
|
+
|
|
958
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)`.
|
|
959
961
|
|
|
960
962
|
Example usages:
|
|
@@ -971,11 +973,26 @@ const pattern3 = rgx()`${beginning}value: ${[word, optionalDigit]}${end}`; // /^
|
|
|
971
973
|
|
|
972
974
|
const caseInsensitiveWord = /hello/i;
|
|
973
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
|
|
974
990
|
```
|
|
975
991
|
|
|
976
992
|
#### Parameters
|
|
977
993
|
**Direct**
|
|
978
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.
|
|
979
996
|
|
|
980
997
|
**Template Tag**
|
|
981
998
|
- `strings` (`TemplateStringsArray`): The literal parts of the template string.
|
|
@@ -1350,14 +1367,14 @@ Asserts that an RGX constant with the given name does not exist. If the assertio
|
|
|
1350
1367
|
function defineRGXConstant(name: string, value: RGXToken): RGXToken
|
|
1351
1368
|
```
|
|
1352
1369
|
|
|
1353
|
-
Defines a new RGX constant with the given name and value. Throws an `RGXConstantConflictError` if a constant with the same name already exists.
|
|
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.
|
|
1354
1371
|
|
|
1355
1372
|
#### Parameters
|
|
1356
1373
|
- `name` (`string`): The name for the constant.
|
|
1357
|
-
- `value` (`RGXToken`): The token value to associate with the name.
|
|
1374
|
+
- `value` (`RGXToken`): The token value to associate with the name. Native tokens are automatically wrapped in `RGXClassWrapperToken`.
|
|
1358
1375
|
|
|
1359
1376
|
#### Returns
|
|
1360
|
-
- `RGXToken`: The value
|
|
1377
|
+
- `RGXToken`: The stored value (after wrapping, if applicable).
|
|
1361
1378
|
|
|
1362
1379
|
### rgxConstant
|
|
1363
1380
|
```typescript
|
|
@@ -1389,6 +1406,8 @@ Deletes an existing RGX constant by name. Throws an `RGXInvalidConstantKeyError`
|
|
|
1389
1406
|
The library defines the following built-in constants, which are available immediately after import. Each can be retrieved via `rgxConstant(name)`.
|
|
1390
1407
|
|
|
1391
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
|
+
|
|
1392
1411
|
| Name | Resolves To | Description |
|
|
1393
1412
|
| --- | --- | --- |
|
|
1394
1413
|
| `"newline"` | `\n` | Newline character |
|
package/dist/constants.d.ts
CHANGED
|
@@ -3,6 +3,6 @@ export declare function listRGXConstants(): string[];
|
|
|
3
3
|
export declare function hasRGXConstant(name: string): boolean;
|
|
4
4
|
export declare function assertHasRGXConstant(name: string): void;
|
|
5
5
|
export declare function assertNotHasRGXConstant(name: string): void;
|
|
6
|
-
export declare function defineRGXConstant(name: string, value: RGXToken): RGXToken;
|
|
6
|
+
export declare function defineRGXConstant(name: string, value: RGXToken): RegExp | import("./types").RGXConvertibleToken | RGXToken[];
|
|
7
7
|
export declare function rgxConstant(name: string): RGXToken;
|
|
8
8
|
export declare function deleteRGXConstant(name: string): void;
|
package/dist/constants.js
CHANGED
|
@@ -7,7 +7,9 @@ exports.assertNotHasRGXConstant = assertNotHasRGXConstant;
|
|
|
7
7
|
exports.defineRGXConstant = defineRGXConstant;
|
|
8
8
|
exports.rgxConstant = rgxConstant;
|
|
9
9
|
exports.deleteRGXConstant = deleteRGXConstant;
|
|
10
|
+
const class_1 = require("./class");
|
|
10
11
|
const errors_1 = require("./errors");
|
|
12
|
+
const typeGuards_1 = require("./typeGuards");
|
|
11
13
|
const rgxConstants = {};
|
|
12
14
|
function listRGXConstants() {
|
|
13
15
|
return Object.keys(rgxConstants);
|
|
@@ -27,8 +29,9 @@ function assertNotHasRGXConstant(name) {
|
|
|
27
29
|
}
|
|
28
30
|
function defineRGXConstant(name, value) {
|
|
29
31
|
assertNotHasRGXConstant(name);
|
|
30
|
-
|
|
31
|
-
|
|
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];
|
|
32
35
|
}
|
|
33
36
|
function rgxConstant(name) {
|
|
34
37
|
assertHasRGXConstant(name);
|
package/dist/index.d.ts
CHANGED
|
@@ -13,4 +13,4 @@ export * from "./flag-transformer";
|
|
|
13
13
|
export * from "./clone";
|
|
14
14
|
export * from "./constants";
|
|
15
15
|
export declare function rgxa(tokens: t.RGXToken[], flags?: string): ExtRegExp;
|
|
16
|
-
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
|
@@ -43,9 +43,9 @@ function rgxa(tokens, flags = '') {
|
|
|
43
43
|
const pattern = (0, concat_1.rgxConcat)(tokens, true, flags);
|
|
44
44
|
return (0, ExtRegExp_1.extRegExp)(pattern, flags);
|
|
45
45
|
}
|
|
46
|
-
function rgx(flags = '') {
|
|
46
|
+
function rgx(flags = '', multiline = true) {
|
|
47
47
|
(0, ExtRegExp_1.assertValidRegexFlags)(flags);
|
|
48
48
|
return (strings, ...tokens) => {
|
|
49
|
-
return rgxa((0, internal_1.taggedTemplateToArray)(strings, tokens), flags);
|
|
49
|
+
return rgxa((0, internal_1.taggedTemplateToArray)(strings, tokens, multiline), flags);
|
|
50
50
|
};
|
|
51
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
|
}
|