@zokugun/regexp 0.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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2021-present Baptiste Augrain
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,141 @@
1
+ [@zokugun/regexp](https://github.com/zokugun/node-regexp)
2
+ =========================================================
3
+
4
+ [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE)
5
+ [![NPM Version](https://img.shields.io/npm/v/@zokugun/regexp.svg?colorB=green)](https://www.npmjs.com/package/@zokugun/regexp)
6
+ [![Donation](https://img.shields.io/badge/donate-ko--fi-green)](https://ko-fi.com/daiyam)
7
+ [![Donation](https://img.shields.io/badge/donate-liberapay-green)](https://liberapay.com/daiyam/donate)
8
+ [![Donation](https://img.shields.io/badge/donate-paypal-green)](https://paypal.me/daiyam99)
9
+
10
+ With `@zokugun/regexp`, you can parse a regular expression to get an AST. Then you can visit, transform or/and translate the ast. When you have finished your edits, you can stringify the AST to get a string to create a `RegExp`.
11
+
12
+ Features
13
+ --------
14
+
15
+ - Parse a regular expression into a detailed AST.
16
+ - Visit and transform AST nodes.
17
+ - Translate regexps between flavors (for example, `ES2018`).
18
+ - Stringify an AST back to a RegExp string.
19
+ - Escape RegExp special characters in strings.
20
+
21
+ Installation
22
+ ------------
23
+
24
+ ```bash
25
+ npm add @zokugun/regexp
26
+ ```
27
+
28
+ Quick Start
29
+ -----------
30
+
31
+ ```typescript
32
+ import { escape, parse, stringify, translate, visit, Flavor, Token, TokenType } from '@zokugun/regexp'
33
+
34
+ function listCaptureGroups(regex: string): Token[] {
35
+ const ast = parse(regex);
36
+
37
+ const groups: Token[] = [];
38
+
39
+ visit(ast.body, {
40
+ [TokenType.CAPTURE_GROUP](token) {
41
+ groups.push(token);
42
+ }
43
+ });
44
+
45
+ return groups;
46
+ }
47
+
48
+ function toES2018(source: string): RegExp {
49
+ return new RegExp(translate(source, Flavor.ES2018));
50
+ }
51
+ ```
52
+
53
+ Syntax
54
+ ------
55
+
56
+ The library is supporting ES2018 syntax and some elements of PCRE2 syntax.
57
+
58
+ | Characters / constructs | Corresponding article |
59
+ | ---------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
60
+ | `\`, `.`, `\cX`, `\d`, `\D`, `\f`, `\n`, `\r`, `\s`, `\S`, `\t`, `\v`, `\w`, `\W`, `\0`, `\xhh`, `\uhhhh`, `\uhhhhh`, `[\b]` | [Character classes (MDN)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Character_Classes) |
61
+ | `^`, `$`, `x(?=y)`, `x(?!y)`, `(?<=y)x`, `(?<!y)x`, `\b`, `\B` | [Assertions (MDN)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Assertions) |
62
+ | `(x)`, `(?:x)`, `(?<Name>x)`, `x\|y`, `[xyz]`, `[^xyz]`, `\Number` | [Groups and ranges (MDN)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Groups_and_Ranges) |
63
+ | `*`, `+`, `?`, `x{n}`, `x{n,}`, `x{n,m}` | [Quantifiers (MDN)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Quantifiers) |
64
+ | `\p{UnicodeProperty}`, `\P{UnicodeProperty}` | [Unicode property escapes (MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Unicode_Property_Escapes) |
65
+ | `(?imsxUJX-imsxUJX)x`, `(?i:x)y` | [Option Setting (PCRE)](https://mariadb.com/kb/en/pcre/#option-setting) |
66
+
67
+ API reference
68
+ -------------
69
+
70
+ ### parse(value: string | RegExp) => Token
71
+
72
+ with `interface Token`, parse the `value` to generate an AST tree.
73
+
74
+ ### stringify(tokens?: Token | Token[] | RegExp) => string
75
+
76
+ generate a string based on the given AST tokens.
77
+
78
+ ### visit(tokens?: Token | Token[], callback?: { [TokenType: string]: Visitor } | Visitor) => void
79
+
80
+ with `type Visitor = (token: Token) => void`, call the `callback` when iterating the given AST tokens
81
+
82
+ ### transform(tokens?: Token | Token[], callback?: { [TokenType: string]: Transformer } | Transformer) => void
83
+
84
+ with `type Transformer = (token: Token, parent: Token | null, key: string | null, index: number | null) => void`, call the `callback` when iterating the given AST tokens.
85
+ The `this` context of the `callback` with have the following functions:
86
+
87
+ - `this.remove() => void`: remove the current token
88
+ - `this.replace(token: string | Token | Token[], transform?: boolean) => void`: replace the current token with the given token(s). If `transform` is true, then the new token(s) are going to be transformed.
89
+ - `this.transform(tokens: Token | Token[], parent?: Token, key?: string) => void`: run the tranformation on the given token(s).
90
+
91
+ ### translate(value: string | RegExp | Token | Token[], target: Flavor, toString?: boolean = true) => string | Token | Token[]
92
+
93
+ translate a regex for the `target` regexp language.
94
+
95
+ ```typescript
96
+ function toES2018(source: string): RegExp {
97
+ return new RegExp(translate(source, Flavor.ES2018));
98
+ }
99
+ ```
100
+
101
+ | Supported Flavors |
102
+ | ----------------- |
103
+ | `ES2018` |
104
+
105
+ ### escape(value: string) => string
106
+
107
+ escape the RegExp special characters from the `value`.
108
+
109
+ Contributions
110
+ -------------
111
+
112
+ Contributions are most welcome. Please:
113
+ - Open issues and feature requests under the repository discussions.
114
+ - Follow the [`CONTRIBUTING.md`](./CONTRIBUTING.md).
115
+
116
+ Donations
117
+ ---------
118
+
119
+ Support this project by becoming a financial contributor.
120
+
121
+ <table>
122
+ <tr>
123
+ <td><img src="https://raw.githubusercontent.com/daiyam/assets/master/icons/256/funding_kofi.png" alt="Ko-fi" width="80px" height="80px"></td>
124
+ <td><a href="https://ko-fi.com/daiyam" target="_blank">ko-fi.com/daiyam</a></td>
125
+ </tr>
126
+ <tr>
127
+ <td><img src="https://raw.githubusercontent.com/daiyam/assets/master/icons/256/funding_liberapay.png" alt="Liberapay" width="80px" height="80px"></td>
128
+ <td><a href="https://liberapay.com/daiyam/donate" target="_blank">liberapay.com/daiyam/donate</a></td>
129
+ </tr>
130
+ <tr>
131
+ <td><img src="https://raw.githubusercontent.com/daiyam/assets/master/icons/256/funding_paypal.png" alt="PayPal" width="80px" height="80px"></td>
132
+ <td><a href="https://paypal.me/daiyam99" target="_blank">paypal.me/daiyam99</a></td>
133
+ </tr>
134
+ </table>
135
+
136
+ License
137
+ -------
138
+
139
+ Copyright &copy; 2026-present Baptiste Augrain
140
+
141
+ Licensed under the [MIT license](https://opensource.org/licenses/MIT).
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.escape = escape;
4
+ const ESCAPE_REGEX = /[-|\\{}()[\]^$+*?.]/g;
5
+ function escape(value) {
6
+ return value.replaceAll(ESCAPE_REGEX, '\\$&');
7
+ }
@@ -0,0 +1 @@
1
+ export declare function escape(value: string): string;