@ptolemy2002/rgx 2.1.0 → 2.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
@@ -9,7 +9,8 @@ import { MaybeArray } from "@ptolemy2002/ts-utils";
9
9
  type RGXNoOpToken = null | undefined;
10
10
  type RGXLiteralToken = RegExp;
11
11
  type RGXNativeToken = string | number | boolean | RGXNoOpToken;
12
- type RGXConvertibleToken = { toRgx: () => MaybeArray<RGXNativeToken | RGXLiteralToken> };
12
+ type RGXConvertibleTokenOutput = MaybeArray<RGXNativeToken | RGXLiteralToken>;
13
+ type RGXConvertibleToken = { toRgx: () => RGXConvertibleTokenOutput };
13
14
  type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
14
15
 
15
16
  const validRegexSymbol = Symbol('rgx.ValidRegex');
@@ -21,11 +22,20 @@ type ValidVanillaRegexFlagsBrandSymbol = typeof validVanillaRegexFlagsSymbol;
21
22
  type ValidVanillaRegexFlags = Branded<string, [ValidVanillaRegexFlagsBrandSymbol]>;
22
23
 
23
24
  type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | RGXTokenType[];
25
+ type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
24
26
  type RGXTokenFromType<T extends RGXTokenType> =
25
27
  // ... see source for full definition
26
28
  ;
27
29
 
28
30
  type RGXErrorCode = 'UNKNOWN' | 'INVALID_RGX_TOKEN' | 'INVALID_REGEX_STRING' | 'INVALID_VANILLA_REGEX_FLAGS';
31
+ type ExpectedTokenType = {
32
+ type: "tokenType";
33
+ values: RGXTokenTypeFlat[];
34
+ } | {
35
+ type: "custom";
36
+ values: string[];
37
+ };
38
+ type RGXTokenCollectionMode = 'union' | 'concat';
29
39
  ```
30
40
 
31
41
  ## Classes
@@ -41,15 +51,22 @@ constructor(message: string, code?: RGXErrorCode)
41
51
  - `message` (`string`): The error message.
42
52
  - `code` (`RGXErrorCode`, optional): An optional error code that can be used to categorize the error. If not provided, it defaults to 'UNKNOWN'.
43
53
 
54
+ #### Properties
55
+ - `code` (`RGXErrorCode`): The error code associated with the error, which can be used to identify the type of error that occurred.
56
+
44
57
  ### RGXInvalidTokenError extends RGXError
45
58
  A specific error class for invalid RGX tokens. This error is thrown when a value fails validation as a specific RGX token type. The error code is set to `INVALID_RGX_TOKEN` on instantiation.
46
59
 
47
60
  #### Constructor
48
61
  ```typescript
49
- constructor(message: string, expected: string | null, got: unknown)
62
+ constructor(message: string, expected: ExpectedTokenType | null, got: unknown)
50
63
  ```
51
64
  - `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.
65
+ - `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.
66
+ - `got` (`unknown`): The actual value that was received, which failed validation.
67
+
68
+ #### Properties
69
+ - `expected` (`string`): A human-readable description of the expected token type(s), generated from the `expected` parameter in the constructor. This can be used to provide more informative error messages.
53
70
  - `got` (`unknown`): The actual value that was received, which failed validation.
54
71
 
55
72
  ### RGXInvalidRegexStringError extends RGXError
@@ -62,6 +79,9 @@ constructor(message: string, got: string)
62
79
  - `message` (`string`): The error message.
63
80
  - `got` (`string`): The actual string that was received, which failed validation.
64
81
 
82
+ #### Properties
83
+ - `got` (`string`): The actual string that was received, which failed validation.
84
+
65
85
  ### RGXInvalidVanillaRegexFlagsError extends RGXError
66
86
  A specific error class for invalid vanilla regex flags. This error is thrown when a string fails validation as valid vanilla regex flags. The error code is set to `INVALID_VANILLA_REGEX_FLAGS` on instantiation.
67
87
 
@@ -72,6 +92,30 @@ constructor(message: string, got: string)
72
92
  - `message` (`string`): The error message.
73
93
  - `got` (`string`): The actual string that was received, which failed validation.
74
94
 
95
+ #### Properties
96
+ - `got` (`string`): The actual string that was received, which failed validation.
97
+
98
+ ### RGXTokenCollection
99
+ A class representing a collection of RGX tokens. This is not used internally, but may be useful for users who want to easily manage collections of RGX tokens like an array, but with additional metadata about the collection mode (union or concat).
100
+
101
+ #### Constructor
102
+ ```typescript
103
+ constructor(tokens: RGXToken[] = [], mode: RGXTokenCollectionMode = 'concat')
104
+ ```
105
+ - `tokens` (`RGXToken[]`, optional): An array of RGX tokens to be managed by the collection. Defaults to an empty array.
106
+ - `mode` (`RGXTokenCollectionMode`, optional): The mode of the collection, either 'union' or 'concat'. Defaults to 'concat'.
107
+
108
+ #### Properties
109
+ - `tokens` (`RGXToken[]`): The array of RGX tokens managed by the collection. In almost all cases, use `getTokens()` instead of accessing this property directly, as it will be copied to prevent external mutation.
110
+ - `mode` (`RGXTokenCollectionMode`): The mode of the collection, either 'union' or 'concat'. This determines how the tokens in the collection will be resolved when `toRgx()` is called.
111
+ - `toRgx()` (`() => RGXToken`): A method that resolves the collection to a single RGX token based on the collection mode. In both modes, a string is ultimately returned, but in 'union' mode, the tokens are resolved as alternatives (using the `|` operator), while in 'concat' mode, the tokens are resolved as concatenated together.
112
+ - `getTokens()` (`() => RGXToken[]`): A method that returns a copy of the array of RGX tokens managed by the collection. This is used to prevent external mutation of the internal `tokens` array.
113
+ - `clone()` (`() => RGXTokenCollection`): A method that creates and returns a deep clone of the RGXTokenCollection instance. This is useful for creating a new collection with the same tokens and mode without affecting the original collection.
114
+ - `asConcat()` (`() => RGXTokenCollection`): If this collection is in 'union' mode, this method returns a new RGXTokenCollection instance with the same tokens but in 'concat' mode. If the collection is already in 'concat' mode, it simply returns itself.
115
+ - `asUnion()` (`() => RGXTokenCollection`): If this collection is in 'concat' mode, this method returns a new RGXTokenCollection instance with the same tokens but in 'union' mode. If the collection is already in 'union' mode, it simply returns itself.
116
+
117
+ 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[]`).
118
+
75
119
  ## Functions
76
120
  The following functions are exported by the library:
77
121
 
@@ -351,9 +395,12 @@ As an alternative to using the `rgx` template tag, you can directly call `rgxa`
351
395
  - `RegExp`: A `RegExp` object constructed from the resolved tokens and the provided flags.
352
396
 
353
397
  ## Peer Dependencies
398
+ - `@ptolemy2002/immutability-utils` ^1.2.1
399
+ - `@ptolemy2002/js-utils` ^3.2.2
354
400
  - `@ptolemy2002/ts-brand-utils` ^1.0.0
355
401
  - `@ptolemy2002/ts-utils` ^3.4.0
356
402
  - `is-callable` ^1.2.7
403
+ - `lodash.clonedeep` ^4.5.0
357
404
 
358
405
  ## Commands
359
406
  The following commands exist in the project:
@@ -0,0 +1,41 @@
1
+ import { RGXConvertibleTokenOutput, RGXToken } from "./types";
2
+ export type RGXTokenCollectionMode = 'union' | 'concat';
3
+ export declare class RGXTokenCollection {
4
+ mode: RGXTokenCollectionMode;
5
+ tokens: RGXToken[];
6
+ constructor(tokens?: RGXToken[], mode?: RGXTokenCollectionMode);
7
+ toRgx(): RGXConvertibleTokenOutput;
8
+ getTokens(): RGXToken[];
9
+ clone(): RGXTokenCollection;
10
+ asConcat(): RGXTokenCollection;
11
+ asUnion(): RGXTokenCollection;
12
+ get length(): number;
13
+ at(index: number): RGXToken | undefined;
14
+ find(predicate: (token: RGXToken, index: number, array: RGXToken[]) => boolean): RGXToken | undefined;
15
+ findIndex(predicate: (token: RGXToken, index: number, array: RGXToken[]) => boolean): number;
16
+ indexOf(token: RGXToken, fromIndex?: number): number;
17
+ includes(token: RGXToken, fromIndex?: number): boolean;
18
+ some(predicate: (token: RGXToken, index: number, array: RGXToken[]) => boolean): boolean;
19
+ every(predicate: (token: RGXToken, index: number, array: RGXToken[]) => boolean): boolean;
20
+ forEach(callback: (token: RGXToken, index: number, array: RGXToken[]) => void): void;
21
+ map(callback: (token: RGXToken, index: number, array: RGXToken[]) => RGXToken): RGXTokenCollection;
22
+ filter(predicate: (token: RGXToken, index: number, array: RGXToken[]) => boolean): RGXTokenCollection;
23
+ reduce<T>(callback: (accumulator: T, token: RGXToken, index: number, array: RGXToken[]) => T, initialValue: T): T;
24
+ reduce(callback: (accumulator: RGXToken, token: RGXToken, index: number, array: RGXToken[]) => RGXToken): RGXToken;
25
+ flat(depth?: number): RGXTokenCollection;
26
+ flatMap(callback: (token: RGXToken, index: number, array: RGXToken[]) => RGXToken | RGXToken[]): RGXTokenCollection;
27
+ slice(start?: number, end?: number): RGXTokenCollection;
28
+ concat(...others: (RGXToken | RGXTokenCollection)[]): RGXTokenCollection;
29
+ push(...tokens: RGXToken[]): void;
30
+ pop(): RGXToken | undefined;
31
+ shift(): RGXToken | undefined;
32
+ unshift(...tokens: RGXToken[]): number;
33
+ splice(start: number, deleteCount?: number, ...items: RGXToken[]): RGXTokenCollection;
34
+ reverse(): this;
35
+ sort(compareFn?: (a: RGXToken, b: RGXToken) => number): this;
36
+ fill(value: RGXToken, start?: number, end?: number): this;
37
+ [Symbol.iterator](): Iterator<RGXToken>;
38
+ entries(): IterableIterator<[number, RGXToken]>;
39
+ keys(): IterableIterator<number>;
40
+ values(): IterableIterator<RGXToken>;
41
+ }
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.RGXTokenCollection = void 0;
7
+ const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
8
+ const index_1 = require("./index");
9
+ const immutability_utils_1 = require("@ptolemy2002/immutability-utils");
10
+ class RGXTokenCollection {
11
+ constructor(tokens = [], mode = 'concat') {
12
+ this.tokens = [];
13
+ this.tokens = (0, lodash_clonedeep_1.default)(tokens);
14
+ this.mode = mode;
15
+ }
16
+ toRgx() {
17
+ if (this.mode === 'union') {
18
+ return (0, index_1.resolveRGXToken)(this.tokens.map(index_1.resolveRGXToken));
19
+ }
20
+ else {
21
+ return (0, index_1.rgxConcat)(this.tokens);
22
+ }
23
+ }
24
+ getTokens() {
25
+ return (0, lodash_clonedeep_1.default)(this.tokens);
26
+ }
27
+ clone() {
28
+ return new RGXTokenCollection(this.tokens, this.mode);
29
+ }
30
+ asConcat() {
31
+ if (this.mode === 'concat')
32
+ return this;
33
+ return (0, immutability_utils_1.immutableMut)(this, clone => {
34
+ clone.mode = 'concat';
35
+ });
36
+ }
37
+ asUnion() {
38
+ if (this.mode === 'union')
39
+ return this;
40
+ return (0, immutability_utils_1.immutableMut)(this, clone => {
41
+ clone.mode = 'union';
42
+ });
43
+ }
44
+ // ------------------ Standard array properties and methods ------------------
45
+ get length() {
46
+ return this.tokens.length;
47
+ }
48
+ at(index) {
49
+ return this.tokens[index];
50
+ }
51
+ find(predicate) {
52
+ return this.tokens.find(predicate);
53
+ }
54
+ findIndex(predicate) {
55
+ return this.tokens.findIndex(predicate);
56
+ }
57
+ indexOf(token, fromIndex) {
58
+ return this.tokens.indexOf(token, fromIndex);
59
+ }
60
+ includes(token, fromIndex) {
61
+ return this.tokens.includes(token, fromIndex);
62
+ }
63
+ some(predicate) {
64
+ return this.tokens.some(predicate);
65
+ }
66
+ every(predicate) {
67
+ return this.tokens.every(predicate);
68
+ }
69
+ forEach(callback) {
70
+ this.tokens.forEach(callback);
71
+ }
72
+ map(callback) {
73
+ return (0, immutability_utils_1.immutableMut)(this, clone => {
74
+ clone.tokens = clone.tokens.map(callback);
75
+ });
76
+ }
77
+ filter(predicate) {
78
+ return (0, immutability_utils_1.immutableMut)(this, clone => {
79
+ clone.tokens = clone.tokens.filter(predicate);
80
+ });
81
+ }
82
+ reduce(callback, ...rest) {
83
+ if (rest.length > 0) {
84
+ return this.tokens.reduce(callback, rest[0]);
85
+ }
86
+ return this.tokens.reduce(callback);
87
+ }
88
+ flat(depth = 1) {
89
+ return (0, immutability_utils_1.immutableMut)(this, clone => {
90
+ // Fixing TypeScript complaining about possible infinite recursion here.
91
+ clone.tokens = clone.tokens.flat(depth);
92
+ });
93
+ }
94
+ flatMap(callback) {
95
+ return (0, immutability_utils_1.immutableMut)(this, clone => {
96
+ // Fixing TypeScript complaining about possible infinite recursion here.
97
+ clone.tokens = clone.tokens.flatMap(callback);
98
+ });
99
+ }
100
+ slice(start, end) {
101
+ return (0, immutability_utils_1.immutableMut)(this, clone => {
102
+ clone.tokens = clone.tokens.slice(start, end);
103
+ });
104
+ }
105
+ concat(...others) {
106
+ return (0, immutability_utils_1.immutableMut)(this, clone => {
107
+ const arrays = others.map(o => o instanceof RGXTokenCollection ? o.tokens : [o]);
108
+ clone.tokens = clone.tokens.concat(...arrays.flat());
109
+ });
110
+ }
111
+ push(...tokens) {
112
+ this.tokens.push(...tokens);
113
+ }
114
+ pop() {
115
+ return this.tokens.pop();
116
+ }
117
+ shift() {
118
+ return this.tokens.shift();
119
+ }
120
+ unshift(...tokens) {
121
+ return this.tokens.unshift(...tokens);
122
+ }
123
+ splice(start, deleteCount, ...items) {
124
+ return (0, immutability_utils_1.immutableMut)(this, clone => {
125
+ const removed = deleteCount === undefined
126
+ ? this.tokens.splice(start)
127
+ : this.tokens.splice(start, deleteCount, ...items);
128
+ clone.tokens = removed;
129
+ });
130
+ }
131
+ reverse() {
132
+ this.tokens.reverse();
133
+ return this;
134
+ }
135
+ sort(compareFn) {
136
+ this.tokens.sort(compareFn);
137
+ return this;
138
+ }
139
+ fill(value, start, end) {
140
+ this.tokens.fill(value, start, end);
141
+ return this;
142
+ }
143
+ [Symbol.iterator]() {
144
+ return this.tokens[Symbol.iterator]();
145
+ }
146
+ entries() {
147
+ return this.tokens.entries();
148
+ }
149
+ keys() {
150
+ return this.tokens.keys();
151
+ }
152
+ values() {
153
+ return this.tokens.values();
154
+ }
155
+ }
156
+ exports.RGXTokenCollection = RGXTokenCollection;
@@ -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,8 @@
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
+ export * from "./collection";
5
6
  export declare function escapeRegex(value: string): t.ValidRegexString;
6
7
  export declare function resolveRGXToken(token: t.RGXToken): t.ValidRegexString;
7
8
  export declare function rgxConcat(tokens: t.RGXToken[]): t.ValidRegexString;
package/dist/index.js CHANGED
@@ -42,10 +42,11 @@ 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
+ __exportStar(require("./collection"), exports);
49
50
  function escapeRegex(value) {
50
51
  return value.replaceAll(/[\-\^\$.*+?^${}()|[\]\\]/g, '\\$&');
51
52
  }
@@ -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
@@ -3,11 +3,13 @@ import { MaybeArray } from "@ptolemy2002/ts-utils";
3
3
  export type RGXNoOpToken = null | undefined;
4
4
  export type RGXLiteralToken = RegExp;
5
5
  export type RGXNativeToken = string | number | boolean | RGXNoOpToken;
6
+ export type RGXConvertibleTokenOutput = MaybeArray<RGXNativeToken | RGXLiteralToken>;
6
7
  export type RGXConvertibleToken = {
7
- toRgx: () => MaybeArray<RGXNativeToken | RGXLiteralToken>;
8
+ toRgx: () => RGXConvertibleTokenOutput;
8
9
  };
9
10
  export type RGXToken = RGXNativeToken | RGXLiteralToken | RGXConvertibleToken | RGXToken[];
10
11
  export type RGXTokenType = 'no-op' | 'literal' | 'native' | 'convertible' | RGXTokenType[];
12
+ export type RGXTokenTypeFlat = Exclude<RGXTokenType, RGXTokenType[]> | "array";
11
13
  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
14
  [K in keyof T]: T[K] extends RGXTokenType ? RGXTokenFromType<T[K]> : never;
13
15
  } : never;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "2.1.0",
3
+ "version": "2.3.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -29,20 +29,27 @@
29
29
  "release-major": "bash ./scripts/release.sh major"
30
30
  },
31
31
  "devDependencies": {
32
+ "@ptolemy2002/immutability-utils": "^1.2.1",
33
+ "@ptolemy2002/js-utils": "^3.2.2",
32
34
  "@ptolemy2002/ts-brand-utils": "^1.0.0",
33
35
  "@ptolemy2002/ts-utils": "^3.4.0",
34
36
  "@types/is-callable": "^1.1.2",
35
37
  "@types/jest": "^29.5.0",
38
+ "@types/lodash.clonedeep": "^4.5.9",
36
39
  "is-callable": "^1.2.7",
37
40
  "jest": "^29.5.0",
41
+ "lodash.clonedeep": "^4.5.0",
38
42
  "ts-jest": "^29.1.0",
39
43
  "ts-patch": "^3.3.0",
40
44
  "tsconfig-paths": "^4.2.0",
41
45
  "typescript-transform-paths": "^3.5.3"
42
46
  },
43
47
  "peerDependencies": {
48
+ "@ptolemy2002/immutability-utils": "^1.2.1",
49
+ "@ptolemy2002/js-utils": "^3.2.2",
44
50
  "@ptolemy2002/ts-brand-utils": "^1.0.0",
45
51
  "@ptolemy2002/ts-utils": "^3.4.0",
46
- "is-callable": "^1.2.7"
52
+ "is-callable": "^1.2.7",
53
+ "lodash.clonedeep": "^4.5.0"
47
54
  }
48
55
  }
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