@ptolemy2002/rgx 7.2.0 → 7.3.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
@@ -24,6 +24,7 @@ type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToke
24
24
  type RGXClassTokenConstructor = new (...args: unknown[]) => RGXClassToken;
25
25
  type RGXGroupedToken = RGXToken[] | RGXLiteralToken | RGXGroupedConvertibleToken;
26
26
  type RGXGroupedConvertibleToken = (RGXConvertibleToken & { readonly rgxIsGroup: true }) | (Omit<RGXConvertibleToken, "toRgx"> & { toRgx: () => RGXGroupedToken, readonly rgxGroupWrap: true });
27
+ type RGXRepeatableConvertibleToken = RGXConvertibleToken & { readonly rgxIsRepeatable: true | undefined };
27
28
 
28
29
  const validRegexSymbol = Symbol('rgx.ValidRegex');
29
30
  type ValidRegexBrandSymbol = typeof validRegexSymbol;
@@ -45,7 +46,7 @@ type ValidIdentifier = Branded<string, [ValidIdentifierBrandSymbol]>;
45
46
 
46
47
  type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | 'class' | RGXTokenType[];
47
48
  type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
48
- type RGXTokenTypeGuardInput = RGXTokenTypeFlat | null | RGXClassTokenConstructor | typeof RegExp | typeof ExtRegExp | typeof RGXTokenCollection | RGXTokenTypeGuardInput[];
49
+ type RGXTokenTypeGuardInput = "repeatable" | RGXTokenTypeFlat | null | RGXClassTokenConstructor | typeof RegExp | typeof ExtRegExp | typeof RGXTokenCollection | RGXTokenTypeGuardInput[];
49
50
  type RGXTokenFromType<T extends RGXTokenTypeGuardInput> =
50
51
  // Maps token type strings to their corresponding types, e.g.:
51
52
  // 'no-op' -> RGXNoOpToken, 'literal' -> RGXLiteralToken, etc.
@@ -911,7 +912,7 @@ Converts an `RGXTokenType` to its flat equivalent `RGXTokenTypeFlat`. If the typ
911
912
  function rgxTokenTypeGuardInputToFlat(type: RGXTokenTypeGuardInput): RGXTokenTypeFlat | null
912
913
  ```
913
914
 
914
- Converts an `RGXTokenTypeGuardInput` to its flat equivalent. If the type is `null`, it returns `null`; if it is an array, it returns `'array'`; if it is an `RGXClassTokenConstructor` (a constructor for an `RGXClassToken` subclass), it returns `'class'` (making it slightly lossy in that case); otherwise, it returns the type as-is.
915
+ Converts an `RGXTokenTypeGuardInput` to its flat equivalent. If the type is `null`, it returns `null`; if it is an array, it returns `'array'`; if it is a RegEx constructor, it returns `'literal`; if it is the `RGXTokenCollection` constructor, it returns `'convertible'`; if it is an `RGXClassTokenConstructor` (a constructor for an `RGXClassToken` subclass), it returns `'class'` (making it slightly lossy in that case); otherwise, it returns the type as-is.
915
916
 
916
917
  #### Parameters
917
918
  - `type` (`RGXTokenTypeGuardInput`): The type guard input to convert.
@@ -930,6 +931,8 @@ When `type` is a constructor, it performs an `instanceof` check against that spe
930
931
 
931
932
  When `type` is an array, it checks that every element of the value array is a valid RGX token matching the corresponding type in the `type` array. If `matchLength` is `true` (the default), it also requires that the value array has the same length as the type array; if `false`, it allows the value array to be longer than the type array, as long as all elements up to the length of the type array match and all elements after that are still valid RGX tokens of any type.
932
933
 
934
+ When `type` is `"repeatable"`, it passes for any token that is not convertible, then checks if `rgxIsRepeatable` is `true | undefined` for convertible tokens.
935
+
933
936
  #### Parameters
934
937
  - `value` (`unknown`): The value to check.
935
938
  - `type` (`T`, optional): The token type to check against. Can be a token type string, `null` (checks against all token types), an `RGXClassTokenConstructor` (checks via `instanceof`), or an array of these. Defaults to `null`.
@@ -32,7 +32,7 @@ class RGXRepeatToken extends base_1.RGXClassToken {
32
32
  return this._token;
33
33
  }
34
34
  set token(value) {
35
- if ((0, typeGuards_1.isRGXToken)(value, "convertible") && typeof value.rgxIsRepeatable === "boolean" && !value.rgxIsRepeatable) {
35
+ if ((0, typeGuards_1.isRGXToken)(value, "convertible") && !(0, typeGuards_1.isRGXToken)(value, "repeatable")) {
36
36
  if ((0, typeGuards_1.isRGXToken)(value, "class")) {
37
37
  throw new errors_1.RGXNotSupportedError(`Repeating ${value.constructor.name} tokens`, "The token was manually marked as non-repeatable.");
38
38
  }
package/dist/constants.js CHANGED
@@ -61,6 +61,13 @@ defineRGXConstant("start", {
61
61
  return /^/;
62
62
  }
63
63
  });
64
+ defineRGXConstant("line-start", {
65
+ rgxGroupWrap: false,
66
+ rgxIsRepeatable: false,
67
+ toRgx() {
68
+ return /^/m;
69
+ }
70
+ });
64
71
  defineRGXConstant("end", {
65
72
  rgxGroupWrap: false,
66
73
  rgxIsRepeatable: false,
@@ -68,6 +75,13 @@ defineRGXConstant("end", {
68
75
  return /$/;
69
76
  }
70
77
  });
78
+ defineRGXConstant("line-end", {
79
+ rgxGroupWrap: false,
80
+ rgxIsRepeatable: false,
81
+ toRgx() {
82
+ return /$/m;
83
+ }
84
+ });
71
85
  defineRGXConstant("word-bound", {
72
86
  rgxGroupWrap: false,
73
87
  rgxIsRepeatable: false,
@@ -65,6 +65,8 @@ const e = __importStar(require("./errors"));
65
65
  const is_callable_1 = __importDefault(require("is-callable"));
66
66
  const internal_1 = require("./internal");
67
67
  const class_1 = require("./class");
68
+ const ExtRegExp_1 = require("./ExtRegExp");
69
+ const collection_1 = require("./collection");
68
70
  function isRGXNoOpToken(value) {
69
71
  return value === null || value === undefined;
70
72
  }
@@ -171,8 +173,14 @@ function rgxTokenTypeGuardInputToFlat(type) {
171
173
  return null;
172
174
  if (Array.isArray(type))
173
175
  return 'array';
176
+ if (type === RegExp || type === ExtRegExp_1.ExtRegExp)
177
+ return 'literal';
178
+ if (type === collection_1.RGXTokenCollection)
179
+ return 'convertible';
174
180
  if ((0, internal_1.isConstructor)(type))
175
181
  return 'class';
182
+ if (type === "repeatable")
183
+ return null;
176
184
  return type;
177
185
  }
178
186
  function isRGXToken(value, type = null, matchLength = true) {
@@ -192,6 +200,11 @@ function isRGXToken(value, type = null, matchLength = true) {
192
200
  return true;
193
201
  if (typeMatches('convertible') && isRGXConvertibleToken(value))
194
202
  return true;
203
+ // Non-covertible tokens are repeatable by default. Convertible tokens are only non-repeatable if they have the rgxIsRepeatable property set to false.
204
+ if (type === 'repeatable' && !isRGXConvertibleToken(value, false))
205
+ return true;
206
+ if (type === 'repeatable' && isRGXConvertibleToken(value))
207
+ return value.rgxIsRepeatable ?? true;
195
208
  if (typeMatches('array') && isRGXArrayToken(value))
196
209
  return true;
197
210
  if (Array.isArray(type) && Array.isArray(value) && (!matchLength || type.length === value.length)) {
package/dist/types.d.ts CHANGED
@@ -22,10 +22,13 @@ export type RGXGroupedConvertibleToken = (RGXConvertibleToken & {
22
22
  toRgx: () => RGXGroupedToken;
23
23
  readonly rgxGroupWrap: true;
24
24
  });
25
+ export type RGXRepeatableConvertibleToken = RGXConvertibleToken & {
26
+ readonly rgxIsRepeatable: true | undefined;
27
+ };
25
28
  export type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | 'class' | RGXTokenType[];
26
29
  export type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
27
- export type RGXTokenTypeGuardInput = RGXTokenTypeFlat | null | RGXClassTokenConstructor | typeof RegExp | typeof ExtRegExp | typeof RGXTokenCollection | RGXTokenTypeGuardInput[];
28
- export type RGXTokenFromType<T extends RGXTokenTypeGuardInput> = T extends null ? RGXToken : T extends 'no-op' ? RGXNoOpToken : T extends 'literal' ? RGXLiteralToken : T extends 'native' ? RGXNativeToken : T extends 'convertible' ? RGXConvertibleToken : T extends 'class' ? RGXClassToken : T extends 'array' ? RGXToken[] : T extends new (...args: unknown[]) => infer R ? R : T extends RGXTokenTypeGuardInput[] ? {
30
+ export type RGXTokenTypeGuardInput = "repeatable" | RGXTokenTypeFlat | null | RGXClassTokenConstructor | typeof RegExp | typeof ExtRegExp | typeof RGXTokenCollection | RGXTokenTypeGuardInput[];
31
+ export type RGXTokenFromType<T extends RGXTokenTypeGuardInput> = T extends null ? RGXToken : T extends 'no-op' ? RGXNoOpToken : T extends 'literal' ? RGXLiteralToken : T extends 'native' ? RGXNativeToken : T extends 'convertible' ? RGXConvertibleToken : T extends 'class' ? RGXClassToken : T extends 'array' ? RGXToken[] : T extends 'repeatable' ? Exclude<RGXToken, RGXConvertibleToken> | RGXRepeatableConvertibleToken : T extends new (...args: unknown[]) => infer R ? R : T extends RGXTokenTypeGuardInput[] ? {
29
32
  [K in keyof T]: T[K] extends RGXTokenTypeGuardInput ? RGXTokenFromType<T[K]> : never;
30
33
  } : never;
31
34
  export type RangeObject = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "7.2.0",
3
+ "version": "7.3.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",