@ptolemy2002/rgx 2.8.0 → 2.9.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
@@ -140,6 +140,8 @@ Standard array properties and methods like `length`, `push`, `pop`, etc. are imp
140
140
  ### RGXClassToken (abstract)
141
141
  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).
142
142
 
143
+ 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.
144
+
143
145
  #### Abstract Methods
144
146
  - `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).
145
147
 
@@ -153,7 +155,7 @@ An abstract base class for creating custom RGX token classes. Subclasses must im
153
155
  ### RGXClassUnionToken extends RGXClassToken
154
156
  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.
155
157
 
156
- A function `rgxClassUnion` is provided with the same parameters as this class' constructor, for easier instantiation without needing to use the `new` keyword.
158
+ 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.
157
159
 
158
160
  #### Constructor
159
161
  ```typescript
@@ -7,3 +7,5 @@ export declare abstract class RGXClassToken {
7
7
  or(...others: RGXTokenCollectionInput[]): RGXClassUnionToken;
8
8
  resolve(): ValidRegexString;
9
9
  }
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.RGXClassToken = void 0;
3
+ exports.assertRgxClassToken = exports.isRgxClassToken = exports.RGXClassToken = void 0;
4
4
  const errors_1 = require("../errors");
5
5
  const resolve_1 = require("../resolve");
6
6
  class RGXClassToken {
@@ -15,3 +15,13 @@ class RGXClassToken {
15
15
  }
16
16
  }
17
17
  exports.RGXClassToken = RGXClassToken;
18
+ // The createClassGuard function only accepts non-abstract classes, so we
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) => {
23
+ if (!(value instanceof RGXClassToken)) {
24
+ throw new errors_1.RGXInvalidTokenError("Invalid token type", { type: "custom", values: ["instance of RGXClassToken"] }, value);
25
+ }
26
+ };
27
+ exports.assertRgxClassToken = assertRgxClassToken;
@@ -14,3 +14,5 @@ export declare class RGXClassUnionToken extends RGXClassToken {
14
14
  export declare function expandRgxUnionTokens(...tokens: RGXTokenCollectionInput[]): RGXTokenCollection;
15
15
  export declare function removeRgxUnionDuplicates(...tokens: RGXTokenCollectionInput[]): RGXTokenCollection;
16
16
  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.rgxClassUnion = exports.RGXClassUnionToken = void 0;
3
+ exports.assertRgxClassUnionToken = exports.isRgxClassUnionToken = exports.rgxClassUnion = exports.RGXClassUnionToken = void 0;
4
4
  exports.expandRgxUnionTokens = expandRgxUnionTokens;
5
5
  exports.removeRgxUnionDuplicates = removeRgxUnionDuplicates;
6
6
  const internal_1 = require("../internal");
@@ -87,3 +87,5 @@ function removeRgxUnionDuplicates(...tokens) {
87
87
  return new collection_1.RGXTokenCollection(uniqueTokens, 'union');
88
88
  }
89
89
  exports.rgxClassUnion = (0, internal_1.createConstructFunction)(RGXClassUnionToken);
90
+ exports.isRgxClassUnionToken = (0, internal_1.createClassGuardFunction)(RGXClassUnionToken);
91
+ exports.assertRgxClassUnionToken = (0, internal_1.createAssertClassGuardFunction)(RGXClassUnionToken);
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RGXInvalidTokenError = void 0;
4
4
  const errors_1 = require("./");
5
5
  const js_utils_1 = require("@ptolemy2002/js-utils");
6
+ const class_1 = require("../class");
6
7
  const tokenExpectationMap = {
7
8
  'no-op': ['null', 'undefined'],
8
9
  'literal': ['RegExp'],
@@ -44,7 +45,8 @@ class RGXInvalidTokenError extends errors_1.RGXError {
44
45
  this.setExpected(expected);
45
46
  }
46
47
  toString() {
47
- return `${this.name}: ${this.message}; Expected: ${this.expected}; Got: ${JSON.stringify(this.got)}`;
48
+ const gotString = (0, class_1.isRgxClassToken)(this.got) ? `instance of ${this.got.constructor.name}` : JSON.stringify(this.got);
49
+ return `${this.name}: ${this.message}; Expected: ${this.expected}; Got: [${gotString}]`;
48
50
  }
49
51
  }
50
52
  exports.RGXInvalidTokenError = RGXInvalidTokenError;
@@ -0,0 +1,2 @@
1
+ export declare function createClassGuardFunction<T extends new (...args: unknown[]) => unknown>(constructor: T): (value: unknown) => value is InstanceType<T>;
2
+ export declare function createAssertClassGuardFunction<T extends new (...args: unknown[]) => unknown>(constructor: T): (value: unknown) => asserts value is InstanceType<T>;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createClassGuardFunction = createClassGuardFunction;
4
+ exports.createAssertClassGuardFunction = createAssertClassGuardFunction;
5
+ const errors_1 = require("../errors");
6
+ function createClassGuardFunction(constructor) {
7
+ return (value) => value instanceof constructor;
8
+ }
9
+ function createAssertClassGuardFunction(constructor) {
10
+ return (value) => {
11
+ if (!(value instanceof constructor)) {
12
+ throw new errors_1.RGXInvalidTokenError("Invalid token type", { type: "custom", values: [`instance of ${constructor.name}`] }, value);
13
+ }
14
+ };
15
+ }
@@ -1,2 +1,3 @@
1
1
  export * from "./createConstructFunction";
2
+ export * from "./createClassGuardFunction";
2
3
  export * from "./taggedTemplateToArray";
@@ -15,4 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./createConstructFunction"), exports);
18
+ __exportStar(require("./createClassGuardFunction"), exports);
18
19
  __exportStar(require("./taggedTemplateToArray"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "2.8.0",
3
+ "version": "2.9.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",