@ptolemy2002/rgx 2.1.0 → 2.2.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
@@ -21,11 +21,19 @@ type ValidVanillaRegexFlagsBrandSymbol = typeof validVanillaRegexFlagsSymbol;
21
21
  type ValidVanillaRegexFlags = Branded<string, [ValidVanillaRegexFlagsBrandSymbol]>;
22
22
 
23
23
  type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | RGXTokenType[];
24
+ type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
24
25
  type RGXTokenFromType<T extends RGXTokenType> =
25
26
  // ... see source for full definition
26
27
  ;
27
28
 
28
29
  type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_VANILLA_REGEX_FLAGS';
30
+ type ExpectedTokenType = {
31
+ type: "tokenType";
32
+ values: RGXTokenTypeFlat[];
33
+ } | {
34
+ type: "custom";
35
+ values: string[];
36
+ };
29
37
  ```
30
38
 
31
39
  ## Classes
@@ -49,7 +57,7 @@ A specific error class for invalid RGX tokens. This error is thrown when a value
49
57
  constructor(message: string, expected: string | null, got: unknown)
50
58
  ```
51
59
  - `message` (`string`): The error message.
52
- - `expected` (`string` | `null`): A description of the expected token type. If `null`, will use a default message indicating the expected types of any RGX token.
60
+ - `expected` (`ExpectedTokenType | null`): Either an object describing the expected token type(s) or `null` if all token types are expected. This is used to generate a human-readable description of what was expected.
53
61
  - `got` (`unknown`): The actual value that was received, which failed validation.
54
62
 
55
63
  ### RGXInvalidRegexStringError extends RGXError
@@ -0,0 +1,5 @@
1
+ export type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_VANILLA_REGEX_FLAGS';
2
+ export declare class RGXError extends Error {
3
+ code: RGXErrorCode;
4
+ constructor(message: string, code?: RGXErrorCode);
5
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RGXError = void 0;
4
+ class RGXError extends Error {
5
+ constructor(message, code) {
6
+ super(message);
7
+ this.code = 'UNKNOWN';
8
+ this.name = 'RGXError';
9
+ if (code) {
10
+ this.code = code;
11
+ }
12
+ }
13
+ }
14
+ exports.RGXError = RGXError;
@@ -0,0 +1,4 @@
1
+ export * from './base';
2
+ export * from './invalidToken';
3
+ export * from './invalidRegexString';
4
+ export * from './invalidVanillaRegexFlags';
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./base"), exports);
18
+ __exportStar(require("./invalidToken"), exports);
19
+ __exportStar(require("./invalidRegexString"), exports);
20
+ __exportStar(require("./invalidVanillaRegexFlags"), exports);
@@ -0,0 +1,6 @@
1
+ import { RGXError } from "./";
2
+ export declare class RGXInvalidRegexStringError extends RGXError {
3
+ got: string;
4
+ constructor(message: string, got: string);
5
+ toString(): string;
6
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RGXInvalidRegexStringError = void 0;
4
+ const errors_1 = require("./");
5
+ class RGXInvalidRegexStringError extends errors_1.RGXError {
6
+ constructor(message, got) {
7
+ super(message, 'INVALID_REGEX_STRING');
8
+ this.name = 'RGXInvalidRegexStringError';
9
+ this.got = got;
10
+ }
11
+ toString() {
12
+ return `${this.name}: ${this.message}; Got: ${JSON.stringify(this.got)}`;
13
+ }
14
+ }
15
+ exports.RGXInvalidRegexStringError = RGXInvalidRegexStringError;
@@ -0,0 +1,16 @@
1
+ import { RGXError } from "./";
2
+ import { RGXTokenTypeFlat } from "../types";
3
+ export type ExpectedTokenType = {
4
+ type: "tokenType";
5
+ values: RGXTokenTypeFlat[];
6
+ } | {
7
+ type: "custom";
8
+ values: string[];
9
+ };
10
+ export declare class RGXInvalidTokenError extends RGXError {
11
+ expected: string;
12
+ got: unknown;
13
+ setExpected(expected: ExpectedTokenType | null): string;
14
+ constructor(message: string, expected: ExpectedTokenType | null, got: unknown);
15
+ toString(): string;
16
+ }
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RGXInvalidTokenError = void 0;
4
+ const errors_1 = require("./");
5
+ const js_utils_1 = require("@ptolemy2002/js-utils");
6
+ const tokenExpectationMap = {
7
+ 'no-op': ['null', 'undefined'],
8
+ 'literal': ['RegExp'],
9
+ 'native': ['string', 'number', 'boolean', 'null', 'undefined'],
10
+ 'convertible': ['object with a toRgx method that returns a valid token'],
11
+ 'array': ['array of native/literal tokens'],
12
+ };
13
+ class RGXInvalidTokenError extends errors_1.RGXError {
14
+ setExpected(expected) {
15
+ let result;
16
+ if (expected === null) {
17
+ // Add them all
18
+ const uniqueValues = new Set();
19
+ for (const tokenType in tokenExpectationMap) {
20
+ tokenExpectationMap[tokenType].forEach(e => uniqueValues.add(e));
21
+ }
22
+ result = (0, js_utils_1.listInPlainEnglish)(Array.from(uniqueValues), { conjunction: 'or' });
23
+ }
24
+ else if (expected.type === 'tokenType') {
25
+ const uniqueValues = new Set();
26
+ for (const tokenType of expected.values) {
27
+ const expectations = tokenExpectationMap[tokenType];
28
+ if (expectations) {
29
+ expectations.forEach(e => uniqueValues.add(e));
30
+ }
31
+ }
32
+ result = (0, js_utils_1.listInPlainEnglish)(Array.from(uniqueValues), { conjunction: 'or' });
33
+ }
34
+ else {
35
+ result = (0, js_utils_1.listInPlainEnglish)(expected.values, { conjunction: 'or' });
36
+ }
37
+ this.expected = `[${result}]`;
38
+ return this.expected;
39
+ }
40
+ constructor(message, expected, got) {
41
+ super(message, 'INVALID_RGX_TOKEN');
42
+ this.name = 'RGXInvalidTokenError';
43
+ this.got = got;
44
+ this.setExpected(expected);
45
+ }
46
+ toString() {
47
+ return `${this.name}: ${this.message}; Expected: ${this.expected}; Got: ${JSON.stringify(this.got)}`;
48
+ }
49
+ }
50
+ exports.RGXInvalidTokenError = RGXInvalidTokenError;
@@ -0,0 +1,6 @@
1
+ import { RGXError } from "./";
2
+ export declare class RGXInvalidVanillaRegexFlagsError extends RGXError {
3
+ got: string;
4
+ constructor(message: string, got: string);
5
+ toString(): string;
6
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RGXInvalidVanillaRegexFlagsError = void 0;
4
+ const errors_1 = require("./");
5
+ class RGXInvalidVanillaRegexFlagsError extends errors_1.RGXError {
6
+ constructor(message, got) {
7
+ super(message, 'INVALID_VANILLA_REGEX_FLAGS');
8
+ this.name = 'RGXInvalidVanillaRegexFlagsError';
9
+ this.got = got;
10
+ }
11
+ toString() {
12
+ return `${this.name}: ${this.message}; Got: ${JSON.stringify(this.got)}`;
13
+ }
14
+ }
15
+ exports.RGXInvalidVanillaRegexFlagsError = RGXInvalidVanillaRegexFlagsError;
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as t from "./types";
2
2
  export * from "./errors";
3
3
  export * from "./types";
4
- export * from "./type-guards";
4
+ export * from "./typeGuards";
5
5
  export declare function escapeRegex(value: string): t.ValidRegexString;
6
6
  export declare function resolveRGXToken(token: t.RGXToken): t.ValidRegexString;
7
7
  export declare function rgxConcat(tokens: t.RGXToken[]): t.ValidRegexString;
package/dist/index.js CHANGED
@@ -42,10 +42,10 @@ exports.rgxConcat = rgxConcat;
42
42
  exports.rgxa = rgxa;
43
43
  exports.default = rgx;
44
44
  const e = __importStar(require("./errors"));
45
- const tg = __importStar(require("./type-guards"));
45
+ const tg = __importStar(require("./typeGuards"));
46
46
  __exportStar(require("./errors"), exports);
47
47
  __exportStar(require("./types"), exports);
48
- __exportStar(require("./type-guards"), exports);
48
+ __exportStar(require("./typeGuards"), exports);
49
49
  function escapeRegex(value) {
50
50
  return value.replaceAll(/[\-\^\$.*+?^${}()|[\]\\]/g, '\\$&');
51
51
  }
@@ -57,7 +57,7 @@ function isRGXNoOpToken(value) {
57
57
  }
58
58
  function assertRGXNoOpToken(value) {
59
59
  if (!isRGXNoOpToken(value)) {
60
- throw new e.RGXInvalidTokenError(`Invalid no-op token`, 'null or undefined', value);
60
+ throw new e.RGXInvalidTokenError(`Invalid no-op token`, { type: "tokenType", values: ['no-op'] }, value);
61
61
  }
62
62
  }
63
63
  function isRGXLiteralToken(value) {
@@ -65,7 +65,7 @@ function isRGXLiteralToken(value) {
65
65
  }
66
66
  function assertRGXLiteralToken(value) {
67
67
  if (!isRGXLiteralToken(value)) {
68
- throw new e.RGXInvalidTokenError(`Invalid literal token`, 'RegExp', value);
68
+ throw new e.RGXInvalidTokenError("Invalid literal token", { type: "tokenType", values: ['literal'] }, value);
69
69
  }
70
70
  }
71
71
  function isRGXNativeToken(value) {
@@ -73,7 +73,7 @@ function isRGXNativeToken(value) {
73
73
  }
74
74
  function assertRGXNativeToken(value) {
75
75
  if (!isRGXNativeToken(value)) {
76
- throw new e.RGXInvalidTokenError(`Invalid native token`, 'string, number, boolean, null, or undefined', value);
76
+ throw new e.RGXInvalidTokenError("Invalid native token", { type: "tokenType", values: ['native'] }, value);
77
77
  }
78
78
  }
79
79
  function isRGXConvertibleToken(value) {
@@ -91,7 +91,7 @@ function isRGXConvertibleToken(value) {
91
91
  }
92
92
  function assertRGXConvertibleToken(value) {
93
93
  if (!isRGXConvertibleToken(value)) {
94
- throw new e.RGXInvalidTokenError(`Invalid convertible token`, 'object with a toRgx method that returns a valid token', value);
94
+ throw new e.RGXInvalidTokenError(`Invalid convertible token`, { type: "tokenType", values: ['convertible'] }, value);
95
95
  }
96
96
  }
97
97
  function rgxTokenType(value) {
@@ -107,7 +107,7 @@ function rgxTokenType(value) {
107
107
  return value.map(rgxTokenType);
108
108
  // Ignoring this line since it should be impossible to reach if the types are correct, but we need it to satisfy the return type
109
109
  /* istanbul ignore next */
110
- throw new e.RGXInvalidTokenError(`Invalid RGX token: ${value}`, null, value);
110
+ throw new e.RGXInvalidTokenError("Invalid RGX token", null, value);
111
111
  }
112
112
  function rgxTokenFromType(type, value) {
113
113
  // Ignoring this line because the function is entirely a TypeScript utility that doesn't need to be tested at runtime.
@@ -125,7 +125,7 @@ function isValidRegexString(value) {
125
125
  }
126
126
  function assertValidRegexString(value) {
127
127
  if (!isValidRegexString(value)) {
128
- throw new e.RGXInvalidRegexStringError(`Invalid regex string: ${value}`, value);
128
+ throw new e.RGXInvalidRegexStringError("Invalid regex string", value);
129
129
  }
130
130
  }
131
131
  function isValidVanillaRegexFlags(value) {
@@ -138,6 +138,6 @@ function isValidVanillaRegexFlags(value) {
138
138
  }
139
139
  function assertValidVanillaRegexFlags(value) {
140
140
  if (!isValidVanillaRegexFlags(value)) {
141
- throw new e.RGXInvalidVanillaRegexFlagsError(`Invalid vanilla regex flags: ${value}`, value);
141
+ throw new e.RGXInvalidVanillaRegexFlagsError("Invalid vanilla regex flags", value);
142
142
  }
143
143
  }
package/dist/types.d.ts CHANGED
@@ -8,6 +8,7 @@ export type RGXConvertibleToken = {
8
8
  };
9
9
  export type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
10
10
  export type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | RGXTokenType[];
11
+ export type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
11
12
  export type RGXTokenFromType<T extends RGXTokenType> = T extends 'no-op' ? RGXNoOpToken : T extends 'literal' ? RGXLiteralToken : T extends 'native' ? RGXNativeToken : T extends 'convertible' ? RGXConvertibleToken : T extends RGXTokenType[] ? {
12
13
  [K in keyof T]: T[K] extends RGXTokenType ? RGXTokenFromType<T[K]> : never;
13
14
  } : never;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -29,6 +29,7 @@
29
29
  "release-major": "bash ./scripts/release.sh major"
30
30
  },
31
31
  "devDependencies": {
32
+ "@ptolemy2002/js-utils": "^3.2.2",
32
33
  "@ptolemy2002/ts-brand-utils": "^1.0.0",
33
34
  "@ptolemy2002/ts-utils": "^3.4.0",
34
35
  "@types/is-callable": "^1.1.2",
package/dist/errors.d.ts DELETED
@@ -1,21 +0,0 @@
1
- export type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_VANILLA_REGEX_FLAGS';
2
- export declare class RGXError extends Error {
3
- code: RGXErrorCode;
4
- constructor(message: string, code?: RGXErrorCode);
5
- }
6
- export declare class RGXInvalidTokenError extends RGXError {
7
- expected: string;
8
- got: unknown;
9
- constructor(message: string, expected: string | null, got: unknown);
10
- toString(): string;
11
- }
12
- export declare class RGXInvalidRegexStringError extends RGXError {
13
- got: string;
14
- constructor(message: string, got: string);
15
- toString(): string;
16
- }
17
- export declare class RGXInvalidVanillaRegexFlagsError extends RGXError {
18
- got: string;
19
- constructor(message: string, got: string);
20
- toString(): string;
21
- }
package/dist/errors.js DELETED
@@ -1,50 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RGXInvalidVanillaRegexFlagsError = exports.RGXInvalidRegexStringError = exports.RGXInvalidTokenError = exports.RGXError = void 0;
4
- class RGXError extends Error {
5
- constructor(message, code) {
6
- super(message);
7
- this.code = 'UNKNOWN';
8
- this.name = 'RGXError';
9
- if (code) {
10
- this.code = code;
11
- }
12
- }
13
- }
14
- exports.RGXError = RGXError;
15
- class RGXInvalidTokenError extends RGXError {
16
- constructor(message, expected, got) {
17
- super(message, 'INVALID_RGX_TOKEN');
18
- this.expected = '[null, undefined, string, number, boolean, RegExp, convertible object, or array of native/literal tokens]';
19
- this.name = 'RGXInvalidTokenError';
20
- if (expected !== null)
21
- this.expected = "[" + expected + "]";
22
- this.got = got;
23
- }
24
- toString() {
25
- return `${this.name}: ${this.message}; Expected: ${this.expected}; Got: ${JSON.stringify(this.got)}`;
26
- }
27
- }
28
- exports.RGXInvalidTokenError = RGXInvalidTokenError;
29
- class RGXInvalidRegexStringError extends RGXError {
30
- constructor(message, got) {
31
- super(message, 'INVALID_REGEX_STRING');
32
- this.name = 'RGXInvalidRegexStringError';
33
- this.got = got;
34
- }
35
- toString() {
36
- return `${this.name}: ${this.message}; Got: ${JSON.stringify(this.got)}`;
37
- }
38
- }
39
- exports.RGXInvalidRegexStringError = RGXInvalidRegexStringError;
40
- class RGXInvalidVanillaRegexFlagsError extends RGXError {
41
- constructor(message, got) {
42
- super(message, 'INVALID_VANILLA_REGEX_FLAGS');
43
- this.name = 'RGXInvalidVanillaRegexFlagsError';
44
- this.got = got;
45
- }
46
- toString() {
47
- return `${this.name}: ${this.message}; Got: ${JSON.stringify(this.got)}`;
48
- }
49
- }
50
- exports.RGXInvalidVanillaRegexFlagsError = RGXInvalidVanillaRegexFlagsError;
File without changes