Skip to content

Commit 8ef23ca

Browse files
authored
feat: support captures (#10)
BREAKING CHANGE: Drop to support Node.js 12
1 parent add8b24 commit 8ef23ca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+173
-39
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ GitHub launched in 2018-04-10.`;
3838
const inputPatterns = [
3939
"git", // => /git/g
4040
"/github/i", // => /github/ig
41-
"/\\d{4}-\\d{2}-\\d{2}/" // => /\d{4}-\d{2}-\d{2}/g
41+
"/(\\d{4})-(\\d{2})-(\\d{2})/" // => /\d{4}-\d{2}-\d{2}/g
4242
];
4343

4444
const results = matchPatterns(inputText, inputPatterns);
4545
assert.deepStrictEqual(results, [
46-
{ match: "GitHub", startIndex: 1, endIndex: 7 },
47-
{ match: "git", startIndex: 65, endIndex: 68 },
48-
{ match: "GitHub", startIndex: 107, endIndex: 113 },
49-
{ match: "2018-04-10", startIndex: 126, endIndex: 136 }
46+
{ match: "GitHub", startIndex: 1, endIndex: 7, captures: [] },
47+
{ match: "git", startIndex: 65, endIndex: 68, captures: [] },
48+
{ match: "GitHub", startIndex: 107, endIndex: 113, captures: [] },
49+
{ match: "2018-04-10", startIndex: 126, endIndex: 136, captures: ["2018", "04", "10"] }
5050
]);
5151
```
5252

@@ -84,14 +84,14 @@ RegExp-like String:
8484

8585
```json
8686
[
87-
"/a \\w+/"
87+
"/a (\\w+)/"
8888
]
8989
```
9090

9191
Results:
9292

9393
```
94-
[ { match: 'a pen', startIndex: 8, endIndex: 13 } ]
94+
[ { match: 'a pen', startIndex: 8, endIndex: 13, captures: ["pen"] } ]
9595
```
9696

9797
## Examples

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
},
7171
"dependencies": {
7272
"escape-string-regexp": "^2.0.0",
73-
"execall": "^2.0.0",
7473
"lodash.sortby": "^4.7.0",
7574
"lodash.uniq": "^4.5.0",
7675
"lodash.uniqwith": "^4.5.0",

src/regexp-string-matcher.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import uniqWith from "lodash.uniqwith";
33
import sortBy from "lodash.sortby";
44
import escapeStringRegexp from "escape-string-regexp";
55
import { isRegExpString, parseRegExpString } from "./regexp-parse";
6-
7-
import execall from "execall";
86
import toRegex from "to-regex";
97

108
const DEFAULT_FLAGS = "g";
@@ -18,6 +16,9 @@ const defaultFlags = (flagsString: string) => {
1816

1917
export interface matchPatternResult {
2018
match: string;
19+
// captured results
20+
// [$1, $2 ...]
21+
captures: string[];
2122
startIndex: number;
2223
endIndex: number;
2324
}
@@ -55,12 +56,16 @@ export const matchPatterns = (text: string, regExpLikeStrings: string[]): matchP
5556
return createRegExp(patternString);
5657
})
5758
.forEach((regExp) => {
58-
const execallResults = execall(regExp, text);
59-
execallResults.forEach((result) => {
60-
const match = result.match;
59+
const results = text.matchAll(regExp);
60+
Array.from(results).forEach((result) => {
61+
if (result.index === undefined) {
62+
return;
63+
}
64+
const match = result[0];
6165
const index = result.index;
6266
matchPatternResults.push({
6367
match: match,
68+
captures: result.slice(1), // without match all text - [$1, $2 ...]
6469
startIndex: index,
6570
endIndex: index + match.length
6671
});

test/example-test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ GitHub launched in 2018-04-10.`;
1010
const inputPatterns = [
1111
"git", // => /git/g
1212
"/github/i", // => /github/ig
13-
"/\\d{4}-\\d{2}-\\d{2}/" // => /\d{4}-\d{2}-\d{2}/g
13+
"/(\\d{4})-(\\d{2})-(\\d{2})/" // => /\d{4}-\d{2}-\d{2}/g
1414
];
1515

1616
const results = matchPatterns(inputText, inputPatterns);
1717
assert.deepStrictEqual(results, [
18-
{ match: "GitHub", startIndex: 1, endIndex: 7 },
19-
{ match: "git", startIndex: 65, endIndex: 68 },
20-
{ match: "GitHub", startIndex: 107, endIndex: 113 },
21-
{ match: "2018-04-10", startIndex: 126, endIndex: 136 }
18+
{ match: "GitHub", startIndex: 1, endIndex: 7, captures: [] },
19+
{ match: "git", startIndex: 65, endIndex: 68, captures: [] },
20+
{ match: "GitHub", startIndex: 107, endIndex: 113, captures: [] },
21+
{ match: "2018-04-10", startIndex: 126, endIndex: 136, captures: ["2018", "04", "10"] }
2222
]);
2323
});
2424

2525
it("RegExp-like string", () => {
2626
const inputText = `This is a pen.`;
2727
// RegExp like strings
28-
const inputPatterns = ["/a \\w+/"];
28+
const inputPatterns = ["/a (\\w+)/"];
2929

3030
const results = matchPatterns(inputText, inputPatterns);
31-
assert.deepStrictEqual(results, [{ match: "a pen", startIndex: 8, endIndex: 13 }]);
31+
assert.deepStrictEqual(results, [{ match: "a pen", startIndex: 8, endIndex: 13, captures: ["pen"] }]);
3232
});

test/snapshot-test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe("Snapshot testing", () => {
2828
.map((result) => {
2929
return `
3030
- match text: **${result.match}**
31+
- captures: **${JSON.stringify(result.captures)}**
3132
- startIndex: **${result.startIndex}**
3233
- endIndex: **${result.endIndex}**
3334
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
"/(\\$)(\\d+)/i"
3+
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$200 and $400
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
- match text: **$200**
3+
- captures: **["$","200"]**
4+
- startIndex: **0**
5+
- endIndex: **4**
6+
7+
```
8+
**$200** and $400
9+
10+
```
11+
12+
13+
14+
- match text: **$400**
15+
- captures: **["$","400"]**
16+
- startIndex: **9**
17+
- endIndex: **13**
18+
19+
```
20+
$200 and **$400**
21+
22+
```
23+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"match": "$200",
4+
"captures": [
5+
"$",
6+
"200"
7+
],
8+
"startIndex": 0,
9+
"endIndex": 4
10+
},
11+
{
12+
"match": "$400",
13+
"captures": [
14+
"$",
15+
"400"
16+
],
17+
"startIndex": 9,
18+
"endIndex": 13
19+
}
20+
]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
"/(\\d+)/i"
3+
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A123456789C
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
- match text: **123456789**
3+
- captures: **["123456789"]**
4+
- startIndex: **1**
5+
- endIndex: **10**
6+
7+
```
8+
A**123456789**C
9+
10+
```
11+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"match": "123456789",
4+
"captures": [
5+
"123456789"
6+
],
7+
"startIndex": 1,
8+
"endIndex": 10
9+
}
10+
]

test/snapshots/duplicated-match-patterns/output-for-human.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
- match text: **Git**
3+
- captures: **[]**
34
- startIndex: **0**
45
- endIndex: **3**
56

@@ -13,6 +14,7 @@ GitHub launched in 2018-04-10.
1314

1415

1516
- match text: **git**
17+
- captures: **[]**
1618
- startIndex: **64**
1719
- endIndex: **67**
1820

@@ -26,6 +28,7 @@ GitHub launched in 2018-04-10.
2628

2729

2830
- match text: **Git**
31+
- captures: **[]**
2932
- startIndex: **106**
3033
- endIndex: **109**
3134

test/snapshots/duplicated-match-patterns/output-for-machine.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
[
22
{
33
"match": "Git",
4+
"captures": [],
45
"startIndex": 0,
56
"endIndex": 3
67
},
78
{
89
"match": "git",
10+
"captures": [],
911
"startIndex": 64,
1012
"endIndex": 67
1113
},
1214
{
1315
"match": "Git",
16+
"captures": [],
1417
"startIndex": 106,
1518
"endIndex": 109
1619
}

test/snapshots/global-regexp/output-for-human.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
- match text: **GitHub**
3+
- captures: **[]**
34
- startIndex: **0**
45
- endIndex: **6**
56

@@ -13,6 +14,7 @@ GitHub launched in 2018-04-10.
1314

1415

1516
- match text: **GitHub**
17+
- captures: **[]**
1618
- startIndex: **106**
1719
- endIndex: **112**
1820

test/snapshots/global-regexp/output-for-machine.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
[
22
{
33
"match": "GitHub",
4+
"captures": [],
45
"startIndex": 0,
56
"endIndex": 6
67
},
78
{
89
"match": "GitHub",
10+
"captures": [],
911
"startIndex": 106,
1012
"endIndex": 112
1113
}

test/snapshots/ignore-case-regexp/output-for-human.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
- match text: **Git**
3+
- captures: **[]**
34
- startIndex: **0**
45
- endIndex: **3**
56

@@ -13,6 +14,7 @@ GitHub launched in 2018-04-10.
1314

1415

1516
- match text: **git**
17+
- captures: **[]**
1618
- startIndex: **64**
1719
- endIndex: **67**
1820

@@ -26,6 +28,7 @@ GitHub launched in 2018-04-10.
2628

2729

2830
- match text: **Git**
31+
- captures: **[]**
2932
- startIndex: **106**
3033
- endIndex: **109**
3134

test/snapshots/ignore-case-regexp/output-for-machine.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
[
22
{
33
"match": "Git",
4+
"captures": [],
45
"startIndex": 0,
56
"endIndex": 3
67
},
78
{
89
"match": "git",
10+
"captures": [],
911
"startIndex": 64,
1012
"endIndex": 67
1113
},
1214
{
1315
"match": "Git",
16+
"captures": [],
1417
"startIndex": 106,
1518
"endIndex": 109
1619
}

test/snapshots/mixed-string-regexp-patterns/output-for-human.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
- match text: **git**
3+
- captures: **[]**
34
- startIndex: **64**
45
- endIndex: **67**
56

@@ -13,6 +14,7 @@ GitHub launched in 2018-04-10.
1314

1415

1516
- match text: **2018**
17+
- captures: **[]**
1618
- startIndex: **125**
1719
- endIndex: **129**
1820

@@ -26,6 +28,7 @@ GitHub launched in **2018**-04-10.
2628

2729

2830
- match text: **04**
31+
- captures: **[]**
2932
- startIndex: **130**
3033
- endIndex: **132**
3134

@@ -39,6 +42,7 @@ GitHub launched in 2018-**04**-10.
3942

4043

4144
- match text: **10**
45+
- captures: **[]**
4246
- startIndex: **133**
4347
- endIndex: **135**
4448

test/snapshots/mixed-string-regexp-patterns/output-for-machine.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
[
22
{
33
"match": "git",
4+
"captures": [],
45
"startIndex": 64,
56
"endIndex": 67
67
},
78
{
89
"match": "2018",
10+
"captures": [],
911
"startIndex": 125,
1012
"endIndex": 129
1113
},
1214
{
1315
"match": "04",
16+
"captures": [],
1417
"startIndex": 130,
1518
"endIndex": 132
1619
},
1720
{
1821
"match": "10",
22+
"captures": [],
1923
"startIndex": 133,
2024
"endIndex": 135
2125
}

0 commit comments

Comments
 (0)