@ptolemy2002/rgx 3.1.1 → 4.0.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
@@ -141,10 +141,16 @@ constructor(tokens: RGXTokenCollectionInput = [], mode: RGXTokenCollectionMode =
141
141
 
142
142
  Standard array properties and methods like `length`, `push`, `pop`, etc. are implemented to work with the internal `tokens` array, but providing collection instances instead of raw arrays when relevant (e.g., `map` has the third parameter typed as `RGXTokenCollection` instead of `RGXToken[]`).
143
143
 
144
+ #### Static Properties
145
+ - `check(value: unknown): value is RGXTokenCollection`: A type guard that checks if the given value is an instance of `RGXTokenCollection`.
146
+ - `assert(value: unknown): asserts value is RGXTokenCollection`: An assertion that checks if the given value is an instance of `RGXTokenCollection`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
147
+
144
148
  ### RGXClassToken (abstract)
145
149
  An abstract base class for creating custom RGX token classes. Subclasses must implement the `toRgx()` method, which returns any valid `RGXToken` (including other convertible tokens, allowing for recursive structures).
146
150
 
147
- Two type guards are provided for this class: `isRgxClassToken` to check if a value is an instance of `RGXClassToken`, and `assertRgxClassToken` to assert that a value is an instance of `RGXClassToken` (throwing an `InvalidTokenError` if the assertion fails). Both of these take a single parameter of type `unknown` for the value.
151
+ #### Static Properties
152
+ - `check(value: unknown): value is RGXClassToken`: A type guard that checks if the given value is an instance of `RGXClassToken`.
153
+ - `assert(value: unknown): asserts value is RGXClassToken`: An assertion that checks if the given value is an instance of `RGXClassToken`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
148
154
 
149
155
  #### Abstract Methods
150
156
  - `toRgx() => RGXToken`: Must be implemented by subclasses to return the token's regex representation as any valid RGX token (native, literal, convertible, or array of tokens).
@@ -159,7 +165,11 @@ Two type guards are provided for this class: `isRgxClassToken` to check if a val
159
165
  ### RGXClassUnionToken extends RGXClassToken
160
166
  A class representing a union (alternation) of RGX tokens. This is typically created via the `or()` method on `RGXClassToken`, but can also be instantiated directly.
161
167
 
162
- A function `rgxClassUnion` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword. Two type guards are provided for this class: `isRgxClassUnionToken` to check if a value is an instance of `RGXClassUnionToken`, and `assertRgxClassUnionToken` to assert that a value is an instance of `RGXClassUnionToken` (throwing an `InvalidTokenError` if the assertion fails). Both of these take a single parameter of type `unknown` for the value.
168
+ A function `rgxClassUnion` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
169
+
170
+ #### Static Properties
171
+ - `check(value: unknown): value is RGXClassUnionToken`: A type guard that checks if the given value is an instance of `RGXClassUnionToken`.
172
+ - `assert(value: unknown): asserts value is RGXClassUnionToken`: An assertion that checks if the given value is an instance of `RGXClassUnionToken`. If the assertion fails, an `RGXInvalidTokenError` will be thrown.
163
173
 
164
174
  #### Constructor
165
175
  ```typescript
@@ -3,9 +3,9 @@ import { RGXTokenCollectionInput } from "../collection";
3
3
  import type { RGXClassUnionToken } from "./union";
4
4
  export declare abstract class RGXClassToken {
5
5
  abstract toRgx(): RGXToken;
6
+ static check: (value: unknown) => value is RGXClassToken;
7
+ static assert: (value: unknown) => asserts value is RGXClassToken;
6
8
  get isGroup(): boolean;
7
9
  or(...others: RGXTokenCollectionInput[]): RGXClassUnionToken;
8
10
  resolve(): ValidRegexString;
9
11
  }
10
- export declare const isRgxClassToken: (value: unknown) => value is RGXClassToken;
11
- export declare const assertRgxClassToken: (value: unknown) => asserts value is RGXClassToken;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.assertRgxClassToken = exports.isRgxClassToken = exports.RGXClassToken = void 0;
3
+ exports.RGXClassToken = void 0;
4
4
  const errors_1 = require("../errors");
5
5
  const resolve_1 = require("../resolve");
6
6
  class RGXClassToken {
@@ -17,11 +17,9 @@ class RGXClassToken {
17
17
  exports.RGXClassToken = RGXClassToken;
18
18
  // The createClassGuard function only accepts non-abstract classes, so we
19
19
  // manually define the guard and assertion functions for RGXClassToken here.
20
- const isRgxClassToken = (value) => value instanceof RGXClassToken;
21
- exports.isRgxClassToken = isRgxClassToken;
22
- const assertRgxClassToken = (value) => {
20
+ RGXClassToken.check = (value) => value instanceof RGXClassToken;
21
+ RGXClassToken.assert = (value) => {
23
22
  if (!(value instanceof RGXClassToken)) {
24
23
  throw new errors_1.RGXInvalidTokenError("Invalid token type", { type: "custom", values: ["instance of RGXClassToken"] }, value);
25
24
  }
26
25
  };
27
- exports.assertRgxClassToken = assertRgxClassToken;
@@ -4,6 +4,8 @@ import { RGXClassToken } from "./base";
4
4
  export type RGXUnionInsertionPosition = 'prefix' | 'suffix';
5
5
  export declare class RGXClassUnionToken extends RGXClassToken {
6
6
  tokens: RGXTokenCollection;
7
+ static check: (value: unknown) => value is RGXClassUnionToken;
8
+ static assert: (value: unknown) => asserts value is RGXClassUnionToken;
7
9
  get isGroup(): boolean;
8
10
  constructor(tokens?: RGXTokenCollectionInput);
9
11
  cleanTokens(): this;
@@ -14,5 +16,3 @@ export declare class RGXClassUnionToken extends RGXClassToken {
14
16
  export declare function expandRgxUnionTokens(...tokens: RGXTokenCollectionInput[]): RGXTokenCollection;
15
17
  export declare function removeRgxUnionDuplicates(...tokens: RGXTokenCollectionInput[]): RGXTokenCollection;
16
18
  export declare const rgxClassUnion: (tokens?: RGXTokenCollectionInput) => RGXClassUnionToken;
17
- export declare const isRgxClassUnionToken: (value: unknown) => value is RGXClassUnionToken;
18
- export declare const assertRgxClassUnionToken: (value: unknown) => asserts value is RGXClassUnionToken;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.assertRgxClassUnionToken = exports.isRgxClassUnionToken = exports.rgxClassUnion = exports.RGXClassUnionToken = void 0;
3
+ exports.rgxClassUnion = exports.RGXClassUnionToken = void 0;
4
4
  exports.expandRgxUnionTokens = expandRgxUnionTokens;
5
5
  exports.removeRgxUnionDuplicates = removeRgxUnionDuplicates;
6
6
  const internal_1 = require("../internal");
@@ -49,6 +49,8 @@ class RGXClassUnionToken extends base_1.RGXClassToken {
49
49
  }
50
50
  }
51
51
  exports.RGXClassUnionToken = RGXClassUnionToken;
52
+ RGXClassUnionToken.check = (0, internal_1.createClassGuardFunction)(RGXClassUnionToken);
53
+ RGXClassUnionToken.assert = (0, internal_1.createAssertClassGuardFunction)(RGXClassUnionToken);
52
54
  function expandRgxUnionTokens(...tokens) {
53
55
  const result = new collection_1.RGXTokenCollection();
54
56
  for (const token of tokens) {
@@ -87,5 +89,3 @@ function removeRgxUnionDuplicates(...tokens) {
87
89
  return new collection_1.RGXTokenCollection(uniqueTokens, 'union');
88
90
  }
89
91
  exports.rgxClassUnion = (0, internal_1.createConstructFunction)(RGXClassUnionToken);
90
- exports.isRgxClassUnionToken = (0, internal_1.createClassGuardFunction)(RGXClassUnionToken);
91
- exports.assertRgxClassUnionToken = (0, internal_1.createAssertClassGuardFunction)(RGXClassUnionToken);
@@ -6,6 +6,8 @@ export type RGXTokenCollectionInput = RGXToken | RGXTokenCollection;
6
6
  export declare class RGXTokenCollection implements Collection<RGXToken> {
7
7
  mode: RGXTokenCollectionMode;
8
8
  tokens: RGXToken[];
9
+ static check: (value: unknown) => value is RGXTokenCollection;
10
+ static assert: (value: unknown) => asserts value is RGXTokenCollection;
9
11
  constructor(tokens?: RGXTokenCollectionInput, mode?: RGXTokenCollectionMode);
10
12
  toRgx(): RegExp;
11
13
  getTokens(): RGXToken[];
@@ -164,4 +164,6 @@ class RGXTokenCollection {
164
164
  }
165
165
  }
166
166
  exports.RGXTokenCollection = RGXTokenCollection;
167
+ RGXTokenCollection.check = (0, internal_1.createClassGuardFunction)(RGXTokenCollection);
168
+ RGXTokenCollection.assert = (0, internal_1.createAssertClassGuardFunction)(RGXTokenCollection);
167
169
  exports.rgxTokenCollection = (0, internal_1.createConstructFunction)(RGXTokenCollection);
@@ -46,7 +46,7 @@ class RGXInvalidTokenError extends errors_1.RGXError {
46
46
  this.setExpected(expected);
47
47
  }
48
48
  toString() {
49
- const gotString = (0, class_1.isRgxClassToken)(this.got) ? `instance of ${this.got.constructor.name}` : JSON.stringify(this.got);
49
+ const gotString = class_1.RGXClassToken.check(this.got) ? `instance of ${this.got.constructor.name}` : JSON.stringify(this.got);
50
50
  return `${this.name}: ${this.message}; Expected: ${this.expected}; Got: [${gotString}]`;
51
51
  }
52
52
  }
@@ -58,9 +58,9 @@ exports.assertValidRegexString = assertValidRegexString;
58
58
  exports.isValidVanillaRegexFlags = isValidVanillaRegexFlags;
59
59
  exports.assertValidVanillaRegexFlags = assertValidVanillaRegexFlags;
60
60
  const e = __importStar(require("./errors"));
61
- const class_1 = require("./class");
62
61
  const is_callable_1 = __importDefault(require("is-callable"));
63
62
  const internal_1 = require("./internal");
63
+ const class_1 = require("./class");
64
64
  function isRGXNoOpToken(value) {
65
65
  return value === null || value === undefined;
66
66
  }
@@ -122,7 +122,7 @@ function rgxTokenTypeFlat(value, recognizeClass = true) {
122
122
  if (isRGXNativeToken(value))
123
123
  return 'native';
124
124
  // We have to check class before convertible because class tokens are also convertible.
125
- if (recognizeClass && (0, class_1.isRgxClassToken)(value))
125
+ if (recognizeClass && class_1.RGXClassToken.check(value))
126
126
  return 'class';
127
127
  if (isRGXConvertibleToken(value))
128
128
  return 'convertible';
@@ -167,7 +167,7 @@ function isRGXToken(value, type = null, matchLength = true) {
167
167
  if (typeMatches('native') && isRGXNativeToken(value))
168
168
  return true;
169
169
  // We have to check class before convertible because class tokens are also convertible.
170
- if (typeMatches('class') && (0, class_1.isRgxClassToken)(value))
170
+ if (typeMatches('class') && class_1.RGXClassToken.check(value))
171
171
  return true;
172
172
  if (typeMatches('convertible') && isRGXConvertibleToken(value))
173
173
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "3.1.1",
3
+ "version": "4.0.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",