@ptolemy2002/rgx 2.0.1 → 2.1.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
@@ -42,7 +42,7 @@ constructor(message: string, code?: RGXErrorCode)
42
42
  - `code` (`RGXErrorCode`, optional): An optional error code that can be used to categorize the error. If not provided, it defaults to 'UNKNOWN'.
43
43
 
44
44
  ### RGXInvalidTokenError extends RGXError
45
- A specific error class for invalid RGX tokens. This error is thrown when a value fails validation as a specific RGX token type.
45
+ 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
46
 
47
47
  #### Constructor
48
48
  ```typescript
@@ -53,7 +53,7 @@ constructor(message: string, expected: string | null, got: unknown)
53
53
  - `got` (`unknown`): The actual value that was received, which failed validation.
54
54
 
55
55
  ### RGXInvalidRegexStringError extends RGXError
56
- A specific error class for invalid regex strings. This error is thrown when a string fails validation as a valid regex string.
56
+ A specific error class for invalid regex strings. This error is thrown when a string fails validation as a valid regex string. The error code is set to `INVALID_REGEX_STRING` on instantiation.
57
57
 
58
58
  #### Constructor
59
59
  ```typescript
@@ -63,7 +63,7 @@ constructor(message: string, got: string)
63
63
  - `got` (`string`): The actual string that was received, which failed validation.
64
64
 
65
65
  ### RGXInvalidVanillaRegexFlagsError extends RGXError
66
- A specific error class for invalid vanilla regex flags. This error is thrown when a string fails validation as valid vanilla regex flags.
66
+ 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
67
 
68
68
  #### Constructor
69
69
  ```typescript
@@ -328,14 +328,27 @@ const pattern3 = rgx()`${beginning}value: ${[word, optionalDigit]}${end}`; // /^
328
328
 
329
329
  #### Parameters
330
330
  **Direct**
331
- - `flags` (`string`, optional): The regex flags to apply to the resulting `RegExp` object (e.g., 'g', 'i', 'm', etc.). If not provided, no flags will be applied. If provided and not valid vanilla regex flags, an `RGXError` with code `INVALID_VANILLA_REGEX_FLAGS` will be thrown.
331
+ - `flags` (`string`, optional): The regex flags to apply to the resulting `RegExp` object (e.g., 'g', 'i', 'm', etc.). If not provided, no flags will be applied. If provided and not valid vanilla regex flags, an `RGXInvalidVanillaRegexFlagsError` will be thrown.
332
332
 
333
333
  **Template Tag**
334
334
  - `strings` (`TemplateStringsArray`): The literal parts of the template string.
335
335
  - `tokens` (`RGXToken[]`): The RGX tokens to be resolved and concatenated with the literal parts.
336
336
 
337
337
  #### Returns
338
- - `(strings: TemplateStringsArray, ...tokens: RGXToken[]) => RegExp`: A template tag function that takes a template literal and returns a `RegExp` object constructed from the resolved tokens and literal parts.
338
+ - `(strings: TemplateStringsArray, ...tokens: RGXToken[]) => RegExp`: A template tag function that takes a template literal and returns a `RegExp` object constructed from the resolved tokens, literal parts, and the provided flags.
339
+
340
+ ### rgxa
341
+ ```typescript
342
+ function rgxa(tokens: RGXToken[], flags?: string): RegExp
343
+ ```
344
+ As an alternative to using the `rgx` template tag, you can directly call `rgxa` with an array of RGX tokens and optional flags to get a `RegExp` object. This is useful in cases where you don't want to use a template literal.
345
+
346
+ #### Parameters
347
+ - `tokens` (`RGXToken[]`): The RGX tokens to be resolved and concatenated to form the regex pattern.
348
+ - `flags` (`string`, optional): The regex flags to apply to the resulting `RegExp` object (e.g., 'g', 'i', 'm', etc.). If not provided, no flags will be applied. If provided and not valid vanilla regex flags, an `RGXInvalidVanillaRegexFlagsError` will be thrown.
349
+
350
+ #### Returns
351
+ - `RegExp`: A `RegExp` object constructed from the resolved tokens and the provided flags.
339
352
 
340
353
  ## Peer Dependencies
341
354
  - `@ptolemy2002/ts-brand-utils` ^1.0.0
package/dist/index.d.ts CHANGED
@@ -1,21 +1,9 @@
1
1
  import * as t from "./types";
2
2
  export * from "./errors";
3
3
  export * from "./types";
4
- export declare function isRGXNoOpToken(value: unknown): value is t.RGXNoOpToken;
5
- export declare function assertRGXNoOpToken(value: unknown): asserts value is t.RGXNoOpToken;
6
- export declare function isRGXLiteralToken(value: unknown): value is t.RGXLiteralToken;
7
- export declare function assertRGXLiteralToken(value: unknown): asserts value is t.RGXLiteralToken;
8
- export declare function isRGXNativeToken(value: unknown): value is t.RGXNativeToken;
9
- export declare function assertRGXNativeToken(value: unknown): asserts value is t.RGXNativeToken;
10
- export declare function isRGXConvertibleToken(value: unknown): value is t.RGXConvertibleToken;
11
- export declare function assertRGXConvertibleToken(value: unknown): asserts value is t.RGXConvertibleToken;
12
- export declare function rgxTokenType(value: t.RGXToken): t.RGXTokenType;
13
- export declare function rgxTokenFromType<T extends t.RGXTokenType>(type: T, value: t.RGXToken): t.RGXTokenFromType<T>;
14
- export declare function isValidRegexString(value: string): value is t.ValidRegexString;
15
- export declare function assertValidRegexString(value: string): asserts value is t.ValidRegexString;
16
- export declare function isValidVanillaRegexFlags(value: string): value is t.ValidVanillaRegexFlags;
17
- export declare function assertValidVanillaRegexFlags(value: string): asserts value is t.ValidVanillaRegexFlags;
4
+ export * from "./type-guards";
18
5
  export declare function escapeRegex(value: string): t.ValidRegexString;
19
6
  export declare function resolveRGXToken(token: t.RGXToken): t.ValidRegexString;
20
7
  export declare function rgxConcat(tokens: t.RGXToken[]): t.ValidRegexString;
8
+ export declare function rgxa(tokens: t.RGXToken[], flags?: string): RegExp;
21
9
  export default function rgx(flags?: string): (strings: TemplateStringsArray, ...tokens: t.RGXToken[]) => RegExp;
package/dist/index.js CHANGED
@@ -35,132 +35,28 @@ var __importStar = (this && this.__importStar) || (function () {
35
35
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
36
36
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
37
37
  };
38
- var __importDefault = (this && this.__importDefault) || function (mod) {
39
- return (mod && mod.__esModule) ? mod : { "default": mod };
40
- };
41
38
  Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.isRGXNoOpToken = isRGXNoOpToken;
43
- exports.assertRGXNoOpToken = assertRGXNoOpToken;
44
- exports.isRGXLiteralToken = isRGXLiteralToken;
45
- exports.assertRGXLiteralToken = assertRGXLiteralToken;
46
- exports.isRGXNativeToken = isRGXNativeToken;
47
- exports.assertRGXNativeToken = assertRGXNativeToken;
48
- exports.isRGXConvertibleToken = isRGXConvertibleToken;
49
- exports.assertRGXConvertibleToken = assertRGXConvertibleToken;
50
- exports.rgxTokenType = rgxTokenType;
51
- exports.rgxTokenFromType = rgxTokenFromType;
52
- exports.isValidRegexString = isValidRegexString;
53
- exports.assertValidRegexString = assertValidRegexString;
54
- exports.isValidVanillaRegexFlags = isValidVanillaRegexFlags;
55
- exports.assertValidVanillaRegexFlags = assertValidVanillaRegexFlags;
56
39
  exports.escapeRegex = escapeRegex;
57
40
  exports.resolveRGXToken = resolveRGXToken;
58
41
  exports.rgxConcat = rgxConcat;
42
+ exports.rgxa = rgxa;
59
43
  exports.default = rgx;
60
- const is_callable_1 = __importDefault(require("is-callable"));
61
44
  const e = __importStar(require("./errors"));
45
+ const tg = __importStar(require("./type-guards"));
62
46
  __exportStar(require("./errors"), exports);
63
47
  __exportStar(require("./types"), exports);
64
- function isRGXNoOpToken(value) {
65
- return value === null || value === undefined;
66
- }
67
- function assertRGXNoOpToken(value) {
68
- if (!isRGXNoOpToken(value)) {
69
- throw new e.RGXInvalidTokenError(`Invalid no-op token`, 'null or undefined', value);
70
- }
71
- }
72
- function isRGXLiteralToken(value) {
73
- return value instanceof RegExp;
74
- }
75
- function assertRGXLiteralToken(value) {
76
- if (!isRGXLiteralToken(value)) {
77
- throw new e.RGXInvalidTokenError(`Invalid literal token`, 'RegExp', value);
78
- }
79
- }
80
- function isRGXNativeToken(value) {
81
- return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || isRGXNoOpToken(value);
82
- }
83
- function assertRGXNativeToken(value) {
84
- if (!isRGXNativeToken(value)) {
85
- throw new e.RGXInvalidTokenError(`Invalid native token`, 'string, number, boolean, null, or undefined', value);
86
- }
87
- }
88
- function isRGXConvertibleToken(value) {
89
- if (typeof value === 'object' && value !== null && 'toRgx' in value) {
90
- if ((0, is_callable_1.default)(value.toRgx)) {
91
- const returnValue = value.toRgx();
92
- if (Array.isArray(returnValue)) {
93
- return returnValue.every(value => isRGXNativeToken(value) || isRGXLiteralToken(value));
94
- }
95
- return isRGXNativeToken(returnValue) || isRGXLiteralToken(returnValue);
96
- }
97
- return false;
98
- }
99
- return false;
100
- }
101
- function assertRGXConvertibleToken(value) {
102
- if (!isRGXConvertibleToken(value)) {
103
- throw new e.RGXInvalidTokenError(`Invalid convertible token`, 'object with a toRgx method that returns a valid token', value);
104
- }
105
- }
106
- function rgxTokenType(value) {
107
- if (isRGXNoOpToken(value))
108
- return 'no-op';
109
- if (isRGXLiteralToken(value))
110
- return 'literal';
111
- if (isRGXNativeToken(value))
112
- return 'native';
113
- if (isRGXConvertibleToken(value))
114
- return 'convertible';
115
- if (Array.isArray(value))
116
- return value.map(rgxTokenType);
117
- // Ignoring this line since it should be impossible to reach if the types are correct, but we need it to satisfy the return type
118
- /* istanbul ignore next */
119
- throw new e.RGXInvalidTokenError(`Invalid RGX token: ${value}`, null, value);
120
- }
121
- function rgxTokenFromType(type, value) {
122
- // Ignoring this line because the function is entirely a TypeScript utility that doesn't need to be tested at runtime.
123
- /* istanbul ignore next */
124
- return value;
125
- }
126
- function isValidRegexString(value) {
127
- try {
128
- new RegExp(value);
129
- return true;
130
- }
131
- catch {
132
- return false;
133
- }
134
- }
135
- function assertValidRegexString(value) {
136
- if (!isValidRegexString(value)) {
137
- throw new e.RGXInvalidRegexStringError(`Invalid regex string: ${value}`, value);
138
- }
139
- }
140
- function isValidVanillaRegexFlags(value) {
141
- const patternMatch = /^[gimsuy]*$/.test(value);
142
- if (!patternMatch)
143
- return false;
144
- // No repeated flags allowed
145
- const flagsSet = new Set(value);
146
- return flagsSet.size === value.length;
147
- }
148
- function assertValidVanillaRegexFlags(value) {
149
- if (!isValidVanillaRegexFlags(value)) {
150
- throw new e.RGXInvalidVanillaRegexFlagsError(`Invalid vanilla regex flags: ${value}`, value);
151
- }
152
- }
48
+ __exportStar(require("./type-guards"), exports);
153
49
  function escapeRegex(value) {
154
50
  return value.replaceAll(/[\-\^\$.*+?^${}()|[\]\\]/g, '\\$&');
155
51
  }
156
52
  function resolveRGXToken(token) {
157
- if (isRGXNoOpToken(token))
53
+ if (tg.isRGXNoOpToken(token))
158
54
  return '';
159
- if (isRGXLiteralToken(token))
55
+ if (tg.isRGXLiteralToken(token))
160
56
  return '(?:' + token.source + ')';
161
- if (isRGXNativeToken(token))
57
+ if (tg.isRGXNativeToken(token))
162
58
  return escapeRegex(String(token));
163
- if (isRGXConvertibleToken(token)) {
59
+ if (tg.isRGXConvertibleToken(token)) {
164
60
  return resolveRGXToken(token.toRgx());
165
61
  }
166
62
  // Interpret arrays as unions
@@ -180,17 +76,21 @@ function resolveRGXToken(token) {
180
76
  function rgxConcat(tokens) {
181
77
  return tokens.map(resolveRGXToken).join('');
182
78
  }
79
+ function rgxa(tokens, flags = '') {
80
+ tg.assertValidVanillaRegexFlags(flags);
81
+ const pattern = rgxConcat(tokens);
82
+ return new RegExp(pattern, flags);
83
+ }
183
84
  function rgx(flags = '') {
184
- assertValidVanillaRegexFlags(flags);
85
+ tg.assertValidVanillaRegexFlags(flags);
185
86
  return (strings, ...tokens) => {
186
- let pattern = '';
187
- const resolvedTokens = tokens.map(resolveRGXToken);
87
+ const tokenArray = [];
188
88
  for (let i = 0; i < strings.length; i++) {
189
- pattern += strings[i];
190
- if (i < resolvedTokens.length) {
191
- pattern += resolvedTokens[i];
192
- }
89
+ if (strings[i])
90
+ tokenArray.push(strings[i]);
91
+ if (i < tokens.length)
92
+ tokenArray.push(tokens[i]);
193
93
  }
194
- return new RegExp(pattern, flags);
94
+ return rgxa(tokenArray, flags);
195
95
  };
196
96
  }
@@ -0,0 +1,15 @@
1
+ import * as t from "./types";
2
+ export declare function isRGXNoOpToken(value: unknown): value is t.RGXNoOpToken;
3
+ export declare function assertRGXNoOpToken(value: unknown): asserts value is t.RGXNoOpToken;
4
+ export declare function isRGXLiteralToken(value: unknown): value is t.RGXLiteralToken;
5
+ export declare function assertRGXLiteralToken(value: unknown): asserts value is t.RGXLiteralToken;
6
+ export declare function isRGXNativeToken(value: unknown): value is t.RGXNativeToken;
7
+ export declare function assertRGXNativeToken(value: unknown): asserts value is t.RGXNativeToken;
8
+ export declare function isRGXConvertibleToken(value: unknown): value is t.RGXConvertibleToken;
9
+ export declare function assertRGXConvertibleToken(value: unknown): asserts value is t.RGXConvertibleToken;
10
+ export declare function rgxTokenType(value: t.RGXToken): t.RGXTokenType;
11
+ export declare function rgxTokenFromType<T extends t.RGXTokenType>(type: T, value: t.RGXToken): t.RGXTokenFromType<T>;
12
+ export declare function isValidRegexString(value: string): value is t.ValidRegexString;
13
+ export declare function assertValidRegexString(value: string): asserts value is t.ValidRegexString;
14
+ export declare function isValidVanillaRegexFlags(value: string): value is t.ValidVanillaRegexFlags;
15
+ export declare function assertValidVanillaRegexFlags(value: string): asserts value is t.ValidVanillaRegexFlags;
@@ -0,0 +1,143 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.isRGXNoOpToken = isRGXNoOpToken;
40
+ exports.assertRGXNoOpToken = assertRGXNoOpToken;
41
+ exports.isRGXLiteralToken = isRGXLiteralToken;
42
+ exports.assertRGXLiteralToken = assertRGXLiteralToken;
43
+ exports.isRGXNativeToken = isRGXNativeToken;
44
+ exports.assertRGXNativeToken = assertRGXNativeToken;
45
+ exports.isRGXConvertibleToken = isRGXConvertibleToken;
46
+ exports.assertRGXConvertibleToken = assertRGXConvertibleToken;
47
+ exports.rgxTokenType = rgxTokenType;
48
+ exports.rgxTokenFromType = rgxTokenFromType;
49
+ exports.isValidRegexString = isValidRegexString;
50
+ exports.assertValidRegexString = assertValidRegexString;
51
+ exports.isValidVanillaRegexFlags = isValidVanillaRegexFlags;
52
+ exports.assertValidVanillaRegexFlags = assertValidVanillaRegexFlags;
53
+ const e = __importStar(require("./errors"));
54
+ const is_callable_1 = __importDefault(require("is-callable"));
55
+ function isRGXNoOpToken(value) {
56
+ return value === null || value === undefined;
57
+ }
58
+ function assertRGXNoOpToken(value) {
59
+ if (!isRGXNoOpToken(value)) {
60
+ throw new e.RGXInvalidTokenError(`Invalid no-op token`, 'null or undefined', value);
61
+ }
62
+ }
63
+ function isRGXLiteralToken(value) {
64
+ return value instanceof RegExp;
65
+ }
66
+ function assertRGXLiteralToken(value) {
67
+ if (!isRGXLiteralToken(value)) {
68
+ throw new e.RGXInvalidTokenError(`Invalid literal token`, 'RegExp', value);
69
+ }
70
+ }
71
+ function isRGXNativeToken(value) {
72
+ return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || isRGXNoOpToken(value);
73
+ }
74
+ function assertRGXNativeToken(value) {
75
+ if (!isRGXNativeToken(value)) {
76
+ throw new e.RGXInvalidTokenError(`Invalid native token`, 'string, number, boolean, null, or undefined', value);
77
+ }
78
+ }
79
+ function isRGXConvertibleToken(value) {
80
+ if (typeof value === 'object' && value !== null && 'toRgx' in value) {
81
+ if ((0, is_callable_1.default)(value.toRgx)) {
82
+ const returnValue = value.toRgx();
83
+ if (Array.isArray(returnValue)) {
84
+ return returnValue.every(value => isRGXNativeToken(value) || isRGXLiteralToken(value));
85
+ }
86
+ return isRGXNativeToken(returnValue) || isRGXLiteralToken(returnValue);
87
+ }
88
+ return false;
89
+ }
90
+ return false;
91
+ }
92
+ function assertRGXConvertibleToken(value) {
93
+ if (!isRGXConvertibleToken(value)) {
94
+ throw new e.RGXInvalidTokenError(`Invalid convertible token`, 'object with a toRgx method that returns a valid token', value);
95
+ }
96
+ }
97
+ function rgxTokenType(value) {
98
+ if (isRGXNoOpToken(value))
99
+ return 'no-op';
100
+ if (isRGXLiteralToken(value))
101
+ return 'literal';
102
+ if (isRGXNativeToken(value))
103
+ return 'native';
104
+ if (isRGXConvertibleToken(value))
105
+ return 'convertible';
106
+ if (Array.isArray(value))
107
+ return value.map(rgxTokenType);
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
+ /* istanbul ignore next */
110
+ throw new e.RGXInvalidTokenError(`Invalid RGX token: ${value}`, null, value);
111
+ }
112
+ function rgxTokenFromType(type, value) {
113
+ // Ignoring this line because the function is entirely a TypeScript utility that doesn't need to be tested at runtime.
114
+ /* istanbul ignore next */
115
+ return value;
116
+ }
117
+ function isValidRegexString(value) {
118
+ try {
119
+ new RegExp(value);
120
+ return true;
121
+ }
122
+ catch {
123
+ return false;
124
+ }
125
+ }
126
+ function assertValidRegexString(value) {
127
+ if (!isValidRegexString(value)) {
128
+ throw new e.RGXInvalidRegexStringError(`Invalid regex string: ${value}`, value);
129
+ }
130
+ }
131
+ function isValidVanillaRegexFlags(value) {
132
+ const patternMatch = /^[gimsuy]*$/.test(value);
133
+ if (!patternMatch)
134
+ return false;
135
+ // No repeated flags allowed
136
+ const flagsSet = new Set(value);
137
+ return flagsSet.size === value.length;
138
+ }
139
+ function assertValidVanillaRegexFlags(value) {
140
+ if (!isValidVanillaRegexFlags(value)) {
141
+ throw new e.RGXInvalidVanillaRegexFlagsError(`Invalid vanilla regex flags: ${value}`, value);
142
+ }
143
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",