Skip to content

Commit

Permalink
Merge pull request #71 from CogentRedTester/d-flag
Browse files Browse the repository at this point in the history
add support for the regex `d` flag
  • Loading branch information
francisrstokes authored Jul 5, 2024
2 parents 325bdb5 + 059432f commit 48f230f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
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 @@ -165,6 +165,7 @@ class SuperExpressive {
hasDefinedStart: false,
hasDefinedEnd: false,
flags: {
d: false,
g: false,
y: false,
m: false,
Expand Down Expand Up @@ -196,6 +197,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 @@ -203,6 +204,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

0 comments on commit 48f230f

Please sign in to comment.