rregex 1.0.0 → 1.1.1
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 +230 -1
- package/babel.config.js +7 -0
- package/jest.transform.js +3 -0
- package/lib/browser.d.ts +471 -26
- package/lib/browser.js +299 -88
- package/lib/commonjs.d.ts +450 -12
- package/lib/commonjs.js +306 -90
- package/lib/module.d.ts +450 -12
- package/lib/module.js +293 -90
- package/lib/rregex.wasm +0 -0
- package/lib/rregex.wasm.d.ts +21 -14
- package/lib/types.d.ts +471 -26
- package/package.json +14 -3
- package/test/rregex.test.ts +235 -0
package/README.md
CHANGED
|
@@ -1,2 +1,231 @@
|
|
|
1
1
|
# rregex
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
A WebAssembly build of [Rust Regex](https://docs.rs/regex/latest/regex/) for Javascript
|
|
4
|
+
|
|
5
|
+
> Note: this project is not intended to be used in production jet
|
|
6
|
+
|
|
7
|
+
* [Why Rust Regex](#why-rust-regex)
|
|
8
|
+
* [Install](#install)
|
|
9
|
+
* [API](#api)
|
|
10
|
+
* [`isMatch(text: string): boolean`](#ismatchtext-string-boolean)
|
|
11
|
+
* [`isMatchAt(text: string, limit: number): boolean`](#ismatchattext-string-limit-number-boolean)
|
|
12
|
+
* [`find(text: string): Match | undefined;`](#findtext-string-match--undefined)
|
|
13
|
+
* [`findAt(text: string): Match | undefined;`](#findattext-string-match--undefined)
|
|
14
|
+
* [`findAll(text: string): Match[];`](#findalltext-string-match)
|
|
15
|
+
* [`replace(text: string, rep: string): string;`](#replacetext-string-rep-string-string)
|
|
16
|
+
* [`replaceAll(text: string, rep: string): string;`](#replacealltext-string-rep-string-string)
|
|
17
|
+
* [`replacen(text: string, limit: number, rep: string): string;`](#replacentext-string-limit-number-rep-string-string)
|
|
18
|
+
* [`split(text: string): string[];`](#splittext-string-string)
|
|
19
|
+
* [`splitn(text: string, limit: number): string[];`](#splitntext-string-limit-number-string)
|
|
20
|
+
* [`shortestMatch(text: string): number | undefined;`](#shortestmatchtext-string-number--undefined)
|
|
21
|
+
* [`shortestMatchAt(text: string, limit: number): number | undefined;`](#shortestmatchattext-string-limit-number-number--undefined)
|
|
22
|
+
* [Known Issues](#known-issues)
|
|
23
|
+
|
|
24
|
+
## Why Rust Regex
|
|
25
|
+
|
|
26
|
+
Rust has a powerful Regex library with a lot of features that don't exists en the standard `Regex` object
|
|
27
|
+
|
|
28
|
+
[See the official documentation](https://docs.rs/regex/latest/regex/#syntax) for more detail
|
|
29
|
+
|
|
30
|
+
## Install
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npm install rregex
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## API
|
|
37
|
+
|
|
38
|
+
Similar to the native `Regex` object you can create a new `RRegex` instance using a string.
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { RRegex } from "rregex";
|
|
42
|
+
const re = new RRegex("^d{4}-d{2}-d{2}$");
|
|
43
|
+
assert.equal(re.isMatch("2014-01-01"), true);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
> Note: It doesn't take a second parameter because fags are part of the syntax ([See Documentation](https://docs.rs/regex/latest/regex/#grouping-and-flags))
|
|
47
|
+
|
|
48
|
+
### `isMatch(text: string): boolean;`
|
|
49
|
+
|
|
50
|
+
Returns true if and only if there is a match for the regex in the string given. ([See Documentation](https://docs.rs/regex/latest/regex/struct.Regex.html#method.is_match))
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
const text = "I categorically deny having triskaidekaphobia.";
|
|
54
|
+
const re = new RRegex("\\b\\w{13}\\b");
|
|
55
|
+
expect(re.isMatch(text)).toEqual(true);
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### `isMatchAt(text: string, limit: number): boolean;`
|
|
59
|
+
|
|
60
|
+
Returns the same as is_match, but starts the search at the given offset. ([See Documentation](https://docs.rs/regex/latest/regex/struct.Regex.html#method.is_match_at))
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
const text = "I categorically deny having triskaidekaphobia.";
|
|
64
|
+
const re = new RRegex("\\b\\w{13}\\b");
|
|
65
|
+
expect(re.isMatchAt(text, 1)).toBe(true);
|
|
66
|
+
expect(re.isMatchAt(text, 5)).toBe(false);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### `find(text: string): Match | undefined;`
|
|
70
|
+
|
|
71
|
+
Returns the start and end byte range of the leftmost-first match in `text`. If no match exists, then `undefined` is returned. ([See Documentation](https://docs.rs/regex/latest/regex/struct.Regex.html#method.find))
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
const text = "I categorically deny having triskaidekaphobia.";
|
|
75
|
+
const re = new RRegex("\\b\\w{13}\\b");
|
|
76
|
+
expect(re.find(text)).toEqual({
|
|
77
|
+
value: "categorically",
|
|
78
|
+
start: 2,
|
|
79
|
+
end: 15,
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### `findAt(text: string): Match | undefined;`
|
|
84
|
+
|
|
85
|
+
Returns the same as find, but starts the search at the given offset. ([See Documentation](https://docs.rs/regex/latest/regex/struct.Regex.html#method.find_at))
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
const text = "I categorically deny having triskaidekaphobia.";
|
|
89
|
+
const re = new RRegex("\\b\\w{13}\\b");
|
|
90
|
+
expect(re.findAt(text, 1)).toEqual({
|
|
91
|
+
value: "categorically",
|
|
92
|
+
start: 2,
|
|
93
|
+
end: 15,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
expect(re.findAt(text, 5)).toEqual(undefined);
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### `findAll(text: string): Match[];`
|
|
100
|
+
|
|
101
|
+
Returns an array for each successive non-overlapping match in text, returning the start and end byte indices with respect to text ([See Documentation](https://docs.rs/regex/latest/regex/struct.Regex.html#method.find_iter))
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
const text = 'Retroactively relinquishing remunerations is reprehensible.'
|
|
105
|
+
const re = new RRegex('\\b\\w{13}\\b')
|
|
106
|
+
expect(re.findAll(text)).toEqual([
|
|
107
|
+
{
|
|
108
|
+
"end": 13,
|
|
109
|
+
"start": 0,
|
|
110
|
+
"value": "Retroactively",
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
"end": 27,
|
|
114
|
+
"start": 14,
|
|
115
|
+
"value": "relinquishing",
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
"end": 41,
|
|
119
|
+
"start": 28,
|
|
120
|
+
"value": "remunerations",
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
"end": 58,
|
|
124
|
+
"start": 45,
|
|
125
|
+
"value": "reprehensible",
|
|
126
|
+
},
|
|
127
|
+
])
|
|
128
|
+
})
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### `replace(text: string, rep: string): string;`
|
|
132
|
+
|
|
133
|
+
Replaces the leftmost-first match with the replacement provided.
|
|
134
|
+
|
|
135
|
+
If no match is found, then a copy of the string is returned unchanged. ([See Documentation](https://docs.rs/regex/latest/regex/struct.Regex.html#method.replace))
|
|
136
|
+
|
|
137
|
+
Note that this function is polymorphic with respect to the replacement. In typical usage, this can just be a normal string:
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
const re = new RRegex("[^01]+");
|
|
141
|
+
expect(re.replace("1078910", "")).toBe("1010");
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
But this is a bit cumbersome to use all the time. Instead, a simple syntax is supported that expands `$name` into the corresponding capture group. Here’s the last example, but using this expansion technique with named capture groups:
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
const re = new RRegex("(?P<last>[^,\\s]+),\\s+(?P<first>\\S+)");
|
|
148
|
+
const result = re.replace("Springsteen, Bruce", "$first $last");
|
|
149
|
+
expect(result).toEqual("Bruce Springsteen");
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Note that using `$2` instead of `$first` or `$1` instead of `$last` would produce the same result. To write a literal `$` use `$$`.
|
|
153
|
+
|
|
154
|
+
Sometimes the replacement string requires use of curly braces to delineate a capture group replacement and surrounding literal text. For example, if we wanted to join two words together with an underscore:
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
const re = new RRegex("(?P<first>\\w+)\\s+(?P<second>\\w+)");
|
|
158
|
+
const result = re.replace("deep fried", "${first}_$second");
|
|
159
|
+
expect(result).toEqual("deep_fried");
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Without the curly braces, the capture group name `first_` would be used, and since it doesn’t exist, it would be replaced with the empty string.
|
|
163
|
+
|
|
164
|
+
### `replaceAll(text: string, rep: string): string;`
|
|
165
|
+
|
|
166
|
+
Replaces all non-overlapping matches in `text` with the replacement provided. This is the same as calling `replacen` with `limit` set to `0`.
|
|
167
|
+
|
|
168
|
+
See the documentation for `replace` for details on how to access capturing group matches in the replacement string. ([See Documentation](https://docs.rs/regex/latest/regex/struct.Regex.html#method.replace_all))
|
|
169
|
+
|
|
170
|
+
### `replacen(text: string, limit: number, rep: string): string;`
|
|
171
|
+
|
|
172
|
+
Replaces at most `limit` non-overlapping matches in `text` with the replacement provided. If `limit` is 0, then all non-overlapping matches are replaced. ([See Documentation](https://docs.rs/regex/latest/regex/struct.Regex.html#method.replacen))
|
|
173
|
+
|
|
174
|
+
### `split(text: string): string[];`
|
|
175
|
+
|
|
176
|
+
Returns an iterator of substrings of `text` delimited by a match of the regular expression. Namely, each element of the iterator corresponds to text that isn’t matched by the regular expression. ([See Documentation](https://docs.rs/regex/latest/regex/struct.Regex.html#method.split))
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
const re = new RRegex("[ \\t]+");
|
|
180
|
+
const fields = re.split("a b \t c\td e");
|
|
181
|
+
expect(fields).toEqual(["a", "b", "c", "d", "e"]);
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### `splitn(text: string, limit: number): string[];`
|
|
185
|
+
|
|
186
|
+
Returns an iterator of at most `limit` substrings of `text` delimited by a match of the regular expression. (A limit of 0 will return no substrings.) Namely, each element of the iterator corresponds to text that isn’t matched by the regular expression. The remainder of the string that is not split will be the last element in the iterator.([See Documentation](https://docs.rs/regex/latest/regex/struct.Regex.html#method.splitn))
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
const re = new RRegex("\\W+");
|
|
190
|
+
const fields = re.splitn("Hey! How are you?", 3);
|
|
191
|
+
expect(fields).toEqual(["Hey", "How", "are you?"]);
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### `shortestMatch(text: string): number | undefined;`
|
|
195
|
+
|
|
196
|
+
Returns the end location of a match in the text given.
|
|
197
|
+
|
|
198
|
+
This method may have the same performance characteristics as `is_match`, except it provides an end location for a match. In particular, the location returned may be shorter than the proper end of the leftmost-first match. ([See Documentation](https://docs.rs/regex/latest/regex/struct.Regex.html#method.shortest_match))
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
const text = 'aaaaa'
|
|
202
|
+
const pos = new RRegex('a+')
|
|
203
|
+
expect(pos.shortestMatch(text)).toBe(1)
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### `shortestMatchAt(text: string, limit: number): number | undefined;`
|
|
207
|
+
|
|
208
|
+
Returns the same as shortest_match, but starts the search at the given offset. ([See Documentation](https://docs.rs/regex/latest/regex/struct.Regex.html#method.shortest_match_at))
|
|
209
|
+
|
|
210
|
+
## Known Issues
|
|
211
|
+
|
|
212
|
+
If you call `splitn(text, limit)` and the expected result length is equal to `limit - 1` the result will include an extra item `""`, this behavior does not happen if `limit` es greater.
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
const regex = new RRegex(',')
|
|
216
|
+
expect(regex.splitn('a,b,c', 0)).toEqual([])
|
|
217
|
+
expect(regex.splitn('a,b,c', 1)).toEqual(['a,b,c'])
|
|
218
|
+
expect(regex.splitn('a,b,c', 2)).toEqual(['a', 'b,c'])
|
|
219
|
+
expect(regex.splitn('a,b,c', 3)).toEqual(['a', 'b', 'c'])
|
|
220
|
+
|
|
221
|
+
// This result includes an unexpected extra item
|
|
222
|
+
expect(regex.splitn('a,b,c', 4)).toEqual(['a', 'b', 'c', ''])
|
|
223
|
+
expect(regex.splitn('a,b,c', 5)).toEqual(['a', 'b', 'c'])
|
|
224
|
+
|
|
225
|
+
expect(regex.splitn('abc', 0)).toEqual([])
|
|
226
|
+
expect(regex.splitn('abc', 1)).toEqual(['abc'])
|
|
227
|
+
|
|
228
|
+
// This result includes an unexpected extra item
|
|
229
|
+
expect(regex.splitn('abc', 2)).toEqual(['abc', ''])
|
|
230
|
+
expect(regex.splitn('abc', 3)).toEqual(['abc'])
|
|
231
|
+
```
|
package/babel.config.js
ADDED