Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for the regex d flag #71

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ declare class SuperExpressive {
*/
caseInsensitive: SuperExpressive;

/**
* Uses the `d` flag on the regular expression, which indicates that it should generate indices for the start and end of each capture group.
*/
generateIndices: SuperExpressive;

/**
* Uses the `y` flag on the regular expression, which indicates that it should create a stateful regular expression that can be resumed from the last match.
*/
Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class SuperExpressive {
hasDefinedStart: false,
hasDefinedEnd: false,
flags: {
d: false,
g: false,
y: false,
m: false,
Expand Down Expand Up @@ -194,6 +195,12 @@ class SuperExpressive {
return next;
}

get generateIndices() {
const next = this[clone]();
next.state.flags.d = true;
return next;
}

get sticky() {
const next = this[clone]();
next.state.flags.y = true;
Expand Down
1 change: 1 addition & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('SuperExpressive', () => {
testRegexEquality('Flag: g', /(?:)/g, SuperExpressive().allowMultipleMatches );
testRegexEquality('Flag: m', /(?:)/m, SuperExpressive().lineByLine );
testRegexEquality('Flag: i', /(?:)/i, SuperExpressive().caseInsensitive );
testRegexEquality('Flag: d', /(?:)/d, SuperExpressive().generateIndices );
testRegexEquality('Flag: y', /(?:)/y, SuperExpressive().sticky);
testRegexEquality('Flag: u', /(?:)/u, SuperExpressive().unicode);
testRegexEquality('Flag: s', /(?:)/s, SuperExpressive().singleLine);
Expand Down
15 changes: 15 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- [.allowMultipleMatches](#allowMultipleMatches)
- [.lineByLine](#lineByLine)
- [.caseInsensitive](#caseInsensitive)
- [.generateIndices](#generateIndices)
- [.sticky](#sticky)
- [.unicode](#unicode)
- [.singleLine](#singleLine)
Expand Down Expand Up @@ -202,6 +203,20 @@ SuperExpressive()
/HELLO/i
```

### .generateIndices

Uses the `d` flag on the regular expression, which indicates that it should generate indices for the start and end of each capture group.

**Example**
```JavaScript
SuperExpressive()
.generateIndices
.string('hello')
.toRegex();
// ->
/hello/d
```

### .sticky

Uses the `y` flag on the regular expression, which indicates that it should create a stateful regular expression that can be resumed from the last match.
Expand Down