@ptolemy2002/rgx 4.8.0 → 4.10.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 CHANGED
@@ -419,6 +419,30 @@ A function `rgxLookbehind` is provided with the same parameters as this class' c
419
419
  - `reverse() => RGXLookaheadToken`: Returns a new `RGXLookaheadToken` with the same tokens and positivity.
420
420
  - `toRgx() => RegExp`: Resolves the lookbehind to a `RegExp`. Positive lookbehinds produce `(?<=...)` and negative lookbehinds produce `(?<!...)`.
421
421
 
422
+ ### RGXClassWrapperToken extends RGXClassToken
423
+ A class that wraps any `RGXToken` as an `RGXClassToken`, giving you access to the extended API class tokens provide. It delegates `isGroup` and `isRepeatable` to the wrapped token where possible.
424
+
425
+ A function `rgxClassWrapper` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
426
+
427
+ #### Static Properties
428
+ - `check(value: unknown): value is RGXClassWrapperToken`: A type guard that checks if the given value is an instance of `RGXClassWrapperToken`.
429
+ - `assert(value: unknown): asserts value is RGXClassWrapperToken`: An assertion that checks if the given value is an instance of `RGXClassWrapperToken`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
430
+
431
+ #### Constructor
432
+ ```typescript
433
+ constructor(token: RGXToken)
434
+ ```
435
+ - `token` (`RGXToken`): The token to wrap.
436
+
437
+ #### Properties
438
+ - `token` (`RGXToken`): The wrapped token.
439
+ - `isGroup` (`boolean`): Delegates to the wrapped token's group status via `isRGXGroupedToken`. Returns `true` if the wrapped token is a grouped token, otherwise `false`.
440
+ - `isRepeatable` (`boolean`): If the wrapped token is an `RGXClassToken`, delegates to its `isRepeatable` property. Otherwise, returns `true`.
441
+
442
+ #### Methods
443
+ - `unwrap() => RGXToken`: Returns the original wrapped token.
444
+ - `toRgx() => RGXToken`: Returns the original wrapped token (alias for `unwrap()`).
445
+
422
446
  ### ExtRegExp extends RegExp
423
447
  A subclass of `RegExp` that supports custom flag transformers in addition to the standard vanilla regex flags (g, i, m, s, u, y). When constructed, custom flags are extracted, their corresponding transformers are applied to the pattern and vanilla flags, and the resulting transformed `RegExp` is created. The `flags` getter returns both the vanilla flags and any custom flags.
424
448
 
@@ -816,24 +840,27 @@ Escapes special regex characters in the given string and brands the result as a
816
840
 
817
841
  ### resolveRGXToken
818
842
  ```typescript
819
- function resolveRGXToken(token: RGXToken, groupWrap?: boolean, topLevel?: boolean): ValidRegexString
843
+ function resolveRGXToken(token: RGXToken, groupWrap?: boolean, topLevel?: boolean, currentFlags?: string): ValidRegexString
820
844
  ```
821
845
 
822
846
  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 when `groupWrap` is `true`), 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 (repeats removed, placed in a non-capturing group when `groupWrap` is `true`).
823
847
 
848
+ For literal tokens (`RegExp` instances), if the token's flags differ from `currentFlags` in any of the localizable flags (`i`, `m`, `s`), the token is wrapped in an inline modifier group (e.g., `(?i:...)`, `(?-i:...)`, `(?ms-i:...)`) instead of a plain non-capturing group. Non-localizable flags (such as `g`, `u`, `y`, `d`, `v`) are ignored when computing the diff. When an inline modifier group is used, it always wraps the token regardless of the `groupWrap` setting, since the modifier group itself serves as a group.
849
+
824
850
  For convertible tokens, if the token has an `rgxGroupWrap` property, that value always takes precedence. If `rgxGroupWrap` is not present, the behavior depends on whether the call is top-level: at the top level, the `groupWrap` parameter is passed through; in recursive calls, it falls back to `true` regardless of the `groupWrap` parameter. This ensures that the caller's `groupWrap` preference only affects the outermost convertible token and does not leak into deeply nested resolution.
825
851
 
826
852
  #### Parameters
827
853
  - `token` (`RGXToken`): The RGX token to resolve.
828
- - `groupWrap` (`boolean`, optional): Whether to wrap literal tokens and array unions in non-capturing groups (`(?:...)`). Defaults to `true`. When `false`, literals use their raw source and array unions omit the wrapping group. For convertible tokens, the token's `rgxGroupWrap` property always takes precedence; otherwise, this value is only passed through at the top level (in recursive calls it falls back to `true`). Array union elements always use `groupWrap=true` internally.
854
+ - `groupWrap` (`boolean`, optional): Whether to wrap literal tokens and array unions in non-capturing groups (`(?:...)`). Defaults to `true`. When `false`, literals use their raw source and array unions omit the wrapping group. For convertible tokens, the token's `rgxGroupWrap` property always takes precedence; otherwise, this value is only passed through at the top level (in recursive calls it falls back to `true`). Array union elements always use `groupWrap=true` internally. Note that when a literal token requires an inline modifier group due to a localizable flag diff, it is always wrapped regardless of this setting.
829
855
  - `topLevel` (`boolean`, optional): Tracks whether the current call is the initial (top-level) invocation. Defaults to `true`. **Warning**: This parameter is intended for internal use by the resolver's own recursion. External callers should not set this parameter, as doing so may produce unexpected wrapping behavior.
856
+ - `currentFlags` (`string`, optional): The flags of the current regex context, used to compute inline modifier groups for literal tokens. Defaults to `''`. When a literal token's localizable flags (`i`, `m`, `s`) differ from this value, the resolver wraps the token in an inline modifier group that adds or removes the differing flags locally.
830
857
 
831
858
  #### Returns
832
859
  - `ValidRegexString`: The resolved string representation of the RGX token. This is guaranteed to be a valid regex string, as convertible tokens are validated to only produce valid regex strings or arrays of valid regex strings.
833
860
 
834
861
  ### rgxConcat
835
862
  ```typescript
836
- function rgxConcat(tokens: RGXToken[], groupWrap?: boolean): ValidRegexString
863
+ function rgxConcat(tokens: RGXToken[], groupWrap?: boolean, currentFlags?: string): ValidRegexString
837
864
  ```
838
865
 
839
866
  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.
@@ -841,6 +868,7 @@ A helper function that resolves an array of RGX tokens and concatenates their re
841
868
  #### Parameters
842
869
  - `tokens` (`RGXToken[]`): The array of RGX tokens to resolve and concatenate.
843
870
  - `groupWrap` (`boolean`, optional): Whether to wrap individual resolved tokens in non-capturing groups. Passed through to `resolveRGXToken`. Defaults to `true`.
871
+ - `currentFlags` (`string`, optional): The flags of the current regex context, passed through to `resolveRGXToken` as its `currentFlags` parameter. Used to compute inline modifier groups for literal tokens whose localizable flags differ. Defaults to `''`.
844
872
 
845
873
  #### Returns
846
874
  - `ValidRegexString`: The concatenated string representation of the resolved RGX tokens. This is guaranteed to be a valid regex string, as it is composed of the resolved forms of RGX tokens, which are all valid regex strings.
@@ -852,6 +880,8 @@ function rgx(flags?: string): (strings: TemplateStringsArray, ...tokens: RGXToke
852
880
 
853
881
  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.
854
882
 
883
+ 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)`.
884
+
855
885
  Example usages:
856
886
  ```typescript
857
887
  const beginning = /^/;
@@ -863,6 +893,9 @@ const optionalDigit = /\d?/;
863
893
  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
864
894
 
865
895
  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
896
+
897
+ const caseInsensitiveWord = /hello/i;
898
+ const pattern4 = rgx()`${beginning}${caseInsensitiveWord} world${end}`; // /^(?i:hello) world$/ - "hello" matches case-insensitively via an inline modifier group, while " world" remains case-sensitive
866
899
  ```
867
900
 
868
901
  #### Parameters
@@ -880,7 +913,7 @@ const pattern3 = rgx()`${beginning}value: ${[word, optionalDigit]}${end}`; // /^
880
913
  ```typescript
881
914
  function rgxa(tokens: RGXToken[], flags?: string): ExtRegExp
882
915
  ```
883
- As an alternative to using the `rgx` template tag, you can directly call `rgxa` with an array of RGX tokens and optional flags to get an `ExtRegExp` object. This is useful in cases where you don't want to use a template literal.
916
+ As an alternative to using the `rgx` template tag, you can directly call `rgxa` with an array of RGX tokens and optional flags to get an `ExtRegExp` object. This is useful in cases where you don't want to use a template literal. Like `rgx`, the provided `flags` are passed as `currentFlags` to the resolver, enabling inline modifier groups for `RegExp` literal tokens whose localizable flags differ.
884
917
 
885
918
  #### Parameters
886
919
  - `tokens` (`RGXToken[]`): The RGX tokens to be resolved and concatenated to form the regex pattern.
@@ -922,6 +955,22 @@ function rgxClassInit(): void
922
955
 
923
956
  Initializes internal method patches required for `RGXClassToken` subclass methods (such as `or`, `group`, `repeat`, `asLookahead`, and `asLookbehind`) to work correctly. This function is called automatically when importing from the main module entry point, so you typically do not need to call it yourself. It only needs to be called manually if you import directly from sub-modules.
924
957
 
958
+ ### toRGXClassToken
959
+ ```typescript
960
+ function toRGXClassToken(token: RGXToken): RGXClassToken
961
+ ```
962
+
963
+ Converts any `RGXToken` into an appropriate `RGXClassToken` subclass, giving you access to the extended API that class tokens provide. Tokens that are already class tokens are returned as-is. Array tokens and `RGXTokenCollection` instances in union mode are converted to `RGXClassUnionToken`. `RGXTokenCollection` instances in concat mode are converted to a non-capturing `RGXGroupToken`. All other tokens are wrapped in an `RGXClassWrapperToken`.
964
+
965
+ #### Parameters
966
+ - `token` (`RGXToken`): The token to convert.
967
+
968
+ #### Returns
969
+ - `RGXClassToken`: The corresponding class token:
970
+ - `RGXClassUnionToken` for array tokens and union-mode `RGXTokenCollection` instances.
971
+ - `RGXGroupToken` (non-capturing) for concat-mode `RGXTokenCollection` instances.
972
+ - `RGXClassWrapperToken` for all other tokens.
973
+
925
974
  ### isInRange
926
975
  ```typescript
927
976
  function isInRange(value: number, { min, max, inclusiveLeft, inclusiveRight }?: RangeObject): boolean
@@ -6,3 +6,5 @@ export * from "./repeat";
6
6
  export * from "./lookaround";
7
7
  export * from "./lookahead";
8
8
  export * from "./lookbehind";
9
+ export * from "./wrapper";
10
+ export * from "./toRGXClassToken";
@@ -22,3 +22,5 @@ __exportStar(require("./repeat"), exports);
22
22
  __exportStar(require("./lookaround"), exports);
23
23
  __exportStar(require("./lookahead"), exports);
24
24
  __exportStar(require("./lookbehind"), exports);
25
+ __exportStar(require("./wrapper"), exports);
26
+ __exportStar(require("./toRGXClassToken"), exports);
@@ -0,0 +1,3 @@
1
+ import { RGXToken } from "../types";
2
+ import { RGXClassToken } from "./base";
3
+ export declare function toRGXClassToken(token: RGXToken): RGXClassToken;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toRGXClassToken = toRGXClassToken;
4
+ const base_1 = require("./base");
5
+ const typeGuards_1 = require("../typeGuards");
6
+ const union_1 = require("./union");
7
+ const collection_1 = require("../collection");
8
+ const group_1 = require("./group");
9
+ const wrapper_1 = require("./wrapper");
10
+ function toRGXClassToken(token) {
11
+ if (base_1.RGXClassToken.check(token))
12
+ return token;
13
+ if ((0, typeGuards_1.isRGXArrayToken)(token))
14
+ return new union_1.RGXClassUnionToken(token);
15
+ if (collection_1.RGXTokenCollection.check(token) && token.mode === 'union')
16
+ return new union_1.RGXClassUnionToken(token.tokens);
17
+ if (collection_1.RGXTokenCollection.check(token) && token.mode === 'concat')
18
+ return new group_1.RGXGroupToken({ capturing: false }, token);
19
+ return new wrapper_1.RGXClassWrapperToken(token);
20
+ }
@@ -0,0 +1,13 @@
1
+ import { RGXToken } from "../types";
2
+ import { RGXClassToken } from "./base";
3
+ export declare class RGXClassWrapperToken extends RGXClassToken {
4
+ token: RGXToken;
5
+ static check: (value: unknown) => value is RGXClassWrapperToken;
6
+ static assert: (value: unknown) => asserts value is RGXClassWrapperToken;
7
+ constructor(token: RGXToken);
8
+ get isGroup(): boolean;
9
+ get isRepeatable(): boolean;
10
+ unwrap(): RGXToken;
11
+ toRgx(): RGXToken;
12
+ }
13
+ export declare const rgxClassWrapper: (token: RGXToken) => RGXClassWrapperToken;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rgxClassWrapper = exports.RGXClassWrapperToken = void 0;
4
+ const base_1 = require("./base");
5
+ const typeGuards_1 = require("../typeGuards");
6
+ const internal_1 = require("../internal");
7
+ class RGXClassWrapperToken extends base_1.RGXClassToken {
8
+ constructor(token) {
9
+ super();
10
+ this.token = token;
11
+ }
12
+ get isGroup() {
13
+ return (0, typeGuards_1.isRGXGroupedToken)(this.token);
14
+ }
15
+ get isRepeatable() {
16
+ if ((0, typeGuards_1.isRGXToken)(this.token, 'class'))
17
+ return this.token.isRepeatable;
18
+ // Assume any other token is repeatable, since we don't know its implementation.
19
+ return true;
20
+ }
21
+ unwrap() {
22
+ return this.token;
23
+ }
24
+ toRgx() {
25
+ return this.unwrap();
26
+ }
27
+ }
28
+ exports.RGXClassWrapperToken = RGXClassWrapperToken;
29
+ RGXClassWrapperToken.check = (0, internal_1.createClassGuardFunction)(RGXClassWrapperToken);
30
+ RGXClassWrapperToken.assert = (0, internal_1.createAssertClassGuardFunction)(RGXClassWrapperToken);
31
+ exports.rgxClassWrapper = (0, internal_1.createConstructFunction)(RGXClassWrapperToken);
package/dist/concat.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  import * as t from "./types";
2
- export declare function rgxConcat(tokens: t.RGXToken[], groupWrap?: boolean): t.ValidRegexString;
2
+ export declare function rgxConcat(tokens: t.RGXToken[], groupWrap?: boolean, currentFlags?: string): t.ValidRegexString;
package/dist/concat.js CHANGED
@@ -3,6 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.rgxConcat = rgxConcat;
4
4
  const resolve_1 = require("./resolve");
5
5
  // Wrapper for letting an array of tokens be resolved as a concatenation instead of a union.
6
- function rgxConcat(tokens, groupWrap = true) {
7
- return tokens.map(t => (0, resolve_1.resolveRGXToken)(t, groupWrap)).join('');
6
+ function rgxConcat(tokens, groupWrap = true, currentFlags = '') {
7
+ return tokens.map(t => (0, resolve_1.resolveRGXToken)(t, groupWrap, true, currentFlags)).join('');
8
8
  }
package/dist/index.js CHANGED
@@ -37,7 +37,7 @@ __exportStar(require("./flag-transformer"), exports);
37
37
  (0, flag_transformer_1.registerCustomFlagTransformers)();
38
38
  function rgxa(tokens, flags = '') {
39
39
  (0, ExtRegExp_1.assertValidRegexFlags)(flags);
40
- const pattern = (0, concat_1.rgxConcat)(tokens);
40
+ const pattern = (0, concat_1.rgxConcat)(tokens, true, flags);
41
41
  return (0, ExtRegExp_1.extRegExp)(pattern, flags);
42
42
  }
43
43
  function rgx(flags = '') {
package/dist/resolve.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import * as t from "./types";
2
2
  export declare function escapeRegex(value: string): t.ValidRegexString;
3
- export declare function resolveRGXToken(token: t.RGXToken, groupWrap?: boolean, topLevel?: boolean): t.ValidRegexString;
3
+ export declare function resolveRGXToken(token: t.RGXToken, groupWrap?: boolean, topLevel?: boolean, currentFlags?: string): t.ValidRegexString;
package/dist/resolve.js CHANGED
@@ -41,21 +41,42 @@ const tg = __importStar(require("./typeGuards"));
41
41
  function escapeRegex(value) {
42
42
  return value.replaceAll(/[\-\^\$.*+?^${}()|[\]\\]/g, '\\$&');
43
43
  }
44
- function resolveRGXToken(token, groupWrap = true, topLevel = true) {
44
+ function localizableVanillaRegexFlagDiff(prev, next) {
45
+ // Remove anything other than the "ims" flags from both strings, as
46
+ // other flags are not localizable (including our custom flags).
47
+ prev = prev.replaceAll(/[^ims]/g, '');
48
+ next = next.replaceAll(/[^ims]/g, '');
49
+ // Format <added flags>-<removed flags>
50
+ const added = [...new Set(next.split(''))].filter(flag => !prev.includes(flag)).join('');
51
+ const removed = [...new Set(prev.split(''))].filter(flag => !next.includes(flag)).join('');
52
+ if (added === '' && removed === '')
53
+ return '';
54
+ if (removed === '')
55
+ return `${added}`;
56
+ return `${added}-${removed}`;
57
+ }
58
+ function resolveRGXToken(token, groupWrap = true, topLevel = true, currentFlags = '') {
45
59
  if (tg.isRGXNoOpToken(token))
46
60
  return '';
47
61
  if (tg.isRGXLiteralToken(token)) {
48
- if (groupWrap)
49
- return '(?:' + token.source + ')';
50
- else
51
- return token.source;
62
+ const localizableFlagDiff = localizableVanillaRegexFlagDiff(currentFlags, token.flags);
63
+ currentFlags = token.flags;
64
+ if (!localizableFlagDiff) {
65
+ if (groupWrap)
66
+ return '(?:' + token.source + ')';
67
+ else
68
+ return token.source;
69
+ }
70
+ else {
71
+ return `(?${localizableFlagDiff}:${token.source})`;
72
+ }
52
73
  }
53
74
  if (tg.isRGXNativeToken(token))
54
75
  return escapeRegex(String(token));
55
76
  if (tg.isRGXConvertibleToken(token)) {
56
77
  // The top-level group-wrapping preference propogates to a direct convertible token, but after that
57
78
  // the preference falls back to true whenever a token doesn't explicitly specify a preference.
58
- return resolveRGXToken(token.toRgx(), token.rgxGroupWrap ?? (topLevel ? groupWrap : true), false);
79
+ return resolveRGXToken(token.toRgx(), token.rgxGroupWrap ?? (topLevel ? groupWrap : true), false, currentFlags);
59
80
  }
60
81
  // Interpret arrays as unions
61
82
  if (tg.isRGXArrayToken(token, false)) {
@@ -66,11 +87,11 @@ function resolveRGXToken(token, groupWrap = true, topLevel = true) {
66
87
  token = [...(0, class_1.removeRgxUnionDuplicates)(...token)];
67
88
  // Don't preserve group wrapping preference for the recursive calls
68
89
  if (groupWrap)
69
- return '(?:' + token.map(t => resolveRGXToken(t, true, false)).join('|') + ')';
90
+ return '(?:' + token.map(t => resolveRGXToken(t, true, false, currentFlags)).join('|') + ')';
70
91
  else
71
- return token.map(t => resolveRGXToken(t, true, false)).join('|');
92
+ return token.map(t => resolveRGXToken(t, true, false, currentFlags)).join('|');
72
93
  }
73
- return resolveRGXToken(token[0]);
94
+ return resolveRGXToken(token[0], true, false, currentFlags);
74
95
  }
75
96
  // Ignoring this line since it should be impossible to reach if the types are correct, but we need it to satisfy the return type
76
97
  /* istanbul ignore next */
@@ -223,7 +223,7 @@ function assertValidRegexString(value) {
223
223
  }
224
224
  }
225
225
  function isValidVanillaRegexFlags(value) {
226
- const patternMatch = /^[gimsuy]*$/.test(value);
226
+ const patternMatch = /^[gimsuydv]*$/.test(value);
227
227
  if (!patternMatch)
228
228
  return false;
229
229
  // No repeated flags allowed
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "4.8.0",
3
+ "version": "4.10.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",