@ptolemy2002/rgx 2.0.2 → 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
@@ -5,4 +5,5 @@ export * from "./type-guards";
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;
8
+ export declare function rgxa(tokens: t.RGXToken[], flags?: string): RegExp;
8
9
  export default function rgx(flags?: string): (strings: TemplateStringsArray, ...tokens: t.RGXToken[]) => RegExp;
package/dist/index.js CHANGED
@@ -39,6 +39,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.escapeRegex = escapeRegex;
40
40
  exports.resolveRGXToken = resolveRGXToken;
41
41
  exports.rgxConcat = rgxConcat;
42
+ exports.rgxa = rgxa;
42
43
  exports.default = rgx;
43
44
  const e = __importStar(require("./errors"));
44
45
  const tg = __importStar(require("./type-guards"));
@@ -75,17 +76,21 @@ function resolveRGXToken(token) {
75
76
  function rgxConcat(tokens) {
76
77
  return tokens.map(resolveRGXToken).join('');
77
78
  }
79
+ function rgxa(tokens, flags = '') {
80
+ tg.assertValidVanillaRegexFlags(flags);
81
+ const pattern = rgxConcat(tokens);
82
+ return new RegExp(pattern, flags);
83
+ }
78
84
  function rgx(flags = '') {
79
85
  tg.assertValidVanillaRegexFlags(flags);
80
86
  return (strings, ...tokens) => {
81
- let pattern = '';
82
- const resolvedTokens = tokens.map(resolveRGXToken);
87
+ const tokenArray = [];
83
88
  for (let i = 0; i < strings.length; i++) {
84
- pattern += strings[i];
85
- if (i < resolvedTokens.length) {
86
- pattern += resolvedTokens[i];
87
- }
89
+ if (strings[i])
90
+ tokenArray.push(strings[i]);
91
+ if (i < tokens.length)
92
+ tokenArray.push(tokens[i]);
88
93
  }
89
- return new RegExp(pattern, flags);
94
+ return rgxa(tokenArray, flags);
90
95
  };
91
96
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ptolemy2002/rgx",
3
- "version": "2.0.2",
3
+ "version": "2.1.0",
4
4
  "private": false,
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",