rregex 1.0.0 → 1.0.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 CHANGED
@@ -1,2 +1,191 @@
1
1
  # rregex
2
- Rust Regex binding for Javascript
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
8
+
9
+ Rust has a powerful Regex library with a lot of features that don't exists en the standard `Regex` object
10
+
11
+ [See the official documentation](https://docs.rs/regex/latest/regex/#syntax) for more detail
12
+
13
+ ## Install
14
+
15
+ ```bash
16
+ npm install rregex
17
+ ```
18
+
19
+ ## API
20
+
21
+ Similar to the native `Regex` object you can create a new `RRegex` instance using a string.
22
+
23
+ ```typescript
24
+ import { RRegex } from "rregex";
25
+ const re = new RRegex("^d{4}-d{2}-d{2}$");
26
+ assert.equal(re.isMatch("2014-01-01"), true);
27
+ ```
28
+
29
+ > 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))
30
+
31
+ ### `isMatch(text: string): boolean`
32
+
33
+ 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))
34
+
35
+ ```typescript
36
+ const text = "I categorically deny having triskaidekaphobia.";
37
+ const re = new RRegex("\\b\\w{13}\\b");
38
+ expect(re.isMatch(text)).toEqual(true);
39
+ ```
40
+
41
+ ### `isMatchAt(text: string, limit: number): boolean`
42
+
43
+ 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))
44
+
45
+ ```typescript
46
+ const text = "I categorically deny having triskaidekaphobia.";
47
+ const re = new RRegex("\\b\\w{13}\\b");
48
+ expect(re.isMatchAt(text, 1)).toBe(true);
49
+ expect(re.isMatchAt(text, 5)).toBe(false);
50
+ ```
51
+
52
+ ### `find(text: string): Match | undefined;`
53
+
54
+ 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))
55
+
56
+ ```typescript
57
+ const text = "I categorically deny having triskaidekaphobia.";
58
+ const re = new RRegex("\\b\\w{13}\\b");
59
+ expect(re.find(text)).toEqual({
60
+ value: "categorically",
61
+ start: 2,
62
+ end: 15,
63
+ });
64
+ ```
65
+
66
+ ### `findAt(text: string): Match | undefined;`
67
+
68
+ 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))
69
+
70
+ ```typescript
71
+ const text = "I categorically deny having triskaidekaphobia.";
72
+ const re = new RRegex("\\b\\w{13}\\b");
73
+ expect(re.findAt(text, 1)).toEqual({
74
+ value: "categorically",
75
+ start: 2,
76
+ end: 15,
77
+ });
78
+
79
+ expect(re.findAt(text, 5)).toEqual(undefined);
80
+ ```
81
+
82
+ ### `findAll(text: string): Match[];`
83
+
84
+ 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))
85
+
86
+ ```typescript
87
+ const text = 'Retroactively relinquishing remunerations is reprehensible.'
88
+ const re = new RRegex('\\b\\w{13}\\b')
89
+ expect(re.findAll(text)).toEqual([
90
+ {
91
+ "end": 13,
92
+ "start": 0,
93
+ "value": "Retroactively",
94
+ },
95
+ {
96
+ "end": 27,
97
+ "start": 14,
98
+ "value": "relinquishing",
99
+ },
100
+ {
101
+ "end": 41,
102
+ "start": 28,
103
+ "value": "remunerations",
104
+ },
105
+ {
106
+ "end": 58,
107
+ "start": 45,
108
+ "value": "reprehensible",
109
+ },
110
+ ])
111
+ })
112
+ ```
113
+
114
+ ### `replace(text: string, rep: string): string;`
115
+
116
+ Replaces the leftmost-first match with the replacement provided.
117
+
118
+ 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))
119
+
120
+ Note that this function is polymorphic with respect to the replacement. In typical usage, this can just be a normal string:
121
+
122
+ ```typescript
123
+ const re = new RRegex("[^01]+");
124
+ expect(re.replace("1078910", "")).toBe("1010");
125
+ ```
126
+
127
+ 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:
128
+
129
+ ```typescript
130
+ const re = new RRegex("(?P<last>[^,\\s]+),\\s+(?P<first>\\S+)");
131
+ const result = re.replace("Springsteen, Bruce", "$first $last");
132
+ expect(result).toEqual("Bruce Springsteen");
133
+ ```
134
+
135
+ Note that using `$2` instead of `$first` or `$1` instead of `$last` would produce the same result. To write a literal `$` use `$$`.
136
+
137
+ 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:
138
+
139
+ ```typescript
140
+ const re = new RRegex("(?P<first>\\w+)\\s+(?P<second>\\w+)");
141
+ const result = re.replace("deep fried", "${first}_$second");
142
+ expect(result).toEqual("deep_fried");
143
+ ```
144
+
145
+ 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.
146
+
147
+ ### `replaceAll(text: string, rep: string): string;`
148
+
149
+ Replaces all non-overlapping matches in `text` with the replacement provided. This is the same as calling `replacen` with `limit` set to `0`.
150
+
151
+ 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))
152
+
153
+ ### `replacen(text: string, limit: number, rep: string): string;`
154
+
155
+ 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))
156
+
157
+ ### `split(text: string): string[];`
158
+
159
+ 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))
160
+
161
+ ```typescript
162
+ const re = new RRegex("[ \\t]+");
163
+ const fields = re.split("a b \t c\td e");
164
+ expect(fields).toEqual(["a", "b", "c", "d", "e"]);
165
+ ```
166
+
167
+ ### `splitn(text: string, limit: number): string[];`
168
+
169
+ 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))
170
+
171
+ ```typescript
172
+ const re = new RRegex("\\W+");
173
+ const fields = re.splitn("Hey! How are you?", 3);
174
+ expect(fields).toEqual(["Hey", "How", "are you?"]);
175
+ ```
176
+
177
+ ### `shortestMatch(text: string): number | undefined;`
178
+
179
+ Returns the end location of a match in the text given.
180
+
181
+ 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))
182
+
183
+ ```typescript
184
+ const text = 'aaaaa'
185
+ const pos = new RRegex('a+')
186
+ expect(pos.shortestMatch(text)).toBe(1)
187
+ ```
188
+
189
+ ### `shortestMatchAt(text: string, limit: number): number | undefined;`
190
+
191
+ 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))
@@ -0,0 +1,7 @@
1
+ module.exports = {
2
+ presets: [
3
+ ['@babel/preset-env', { targets: { esmodules: true } }],
4
+ '@babel/preset-typescript',
5
+ ],
6
+ plugins: ['@babel/plugin-proposal-class-properties'],
7
+ }
@@ -0,0 +1,3 @@
1
+ module.exports = require('babel-jest').createTransformer(
2
+ require('./babel.config.js')
3
+ )