Skip to content

feat: support captures #10

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

Merged
merged 2 commits into from
May 17, 2022
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
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ GitHub launched in 2018-04-10.`;
const inputPatterns = [
"git", // => /git/g
"/github/i", // => /github/ig
"/\\d{4}-\\d{2}-\\d{2}/" // => /\d{4}-\d{2}-\d{2}/g
"/(\\d{4})-(\\d{2})-(\\d{2})/" // => /\d{4}-\d{2}-\d{2}/g
];

const results = matchPatterns(inputText, inputPatterns);
assert.deepStrictEqual(results, [
{ match: "GitHub", startIndex: 1, endIndex: 7 },
{ match: "git", startIndex: 65, endIndex: 68 },
{ match: "GitHub", startIndex: 107, endIndex: 113 },
{ match: "2018-04-10", startIndex: 126, endIndex: 136 }
{ match: "GitHub", startIndex: 1, endIndex: 7, captures: [] },
{ match: "git", startIndex: 65, endIndex: 68, captures: [] },
{ match: "GitHub", startIndex: 107, endIndex: 113, captures: [] },
{ match: "2018-04-10", startIndex: 126, endIndex: 136, captures: ["2018", "04", "10"] }
]);
```

Expand Down Expand Up @@ -84,14 +84,14 @@ RegExp-like String:

```json
[
"/a \\w+/"
"/a (\\w+)/"
]
```

Results:

```
[ { match: 'a pen', startIndex: 8, endIndex: 13 } ]
[ { match: 'a pen', startIndex: 8, endIndex: 13, captures: ["pen"] } ]
```

## Examples
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
},
"dependencies": {
"escape-string-regexp": "^2.0.0",
"execall": "^2.0.0",
"lodash.sortby": "^4.7.0",
"lodash.uniq": "^4.5.0",
"lodash.uniqwith": "^4.5.0",
Expand Down
15 changes: 10 additions & 5 deletions src/regexp-string-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import uniqWith from "lodash.uniqwith";
import sortBy from "lodash.sortby";
import escapeStringRegexp from "escape-string-regexp";
import { isRegExpString, parseRegExpString } from "./regexp-parse";

import execall from "execall";
import toRegex from "to-regex";

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

export interface matchPatternResult {
match: string;
// captured results
// [$1, $2 ...]
captures: string[];
startIndex: number;
endIndex: number;
}
Expand Down Expand Up @@ -55,12 +56,16 @@ export const matchPatterns = (text: string, regExpLikeStrings: string[]): matchP
return createRegExp(patternString);
})
.forEach((regExp) => {
const execallResults = execall(regExp, text);
execallResults.forEach((result) => {
const match = result.match;
const results = text.matchAll(regExp);
Array.from(results).forEach((result) => {
if (result.index === undefined) {
return;
}
const match = result[0];
const index = result.index;
matchPatternResults.push({
match: match,
captures: result.slice(1), // without match all text - [$1, $2 ...]
startIndex: index,
endIndex: index + match.length
});
Expand Down
14 changes: 7 additions & 7 deletions test/example-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ GitHub launched in 2018-04-10.`;
const inputPatterns = [
"git", // => /git/g
"/github/i", // => /github/ig
"/\\d{4}-\\d{2}-\\d{2}/" // => /\d{4}-\d{2}-\d{2}/g
"/(\\d{4})-(\\d{2})-(\\d{2})/" // => /\d{4}-\d{2}-\d{2}/g
];

const results = matchPatterns(inputText, inputPatterns);
assert.deepStrictEqual(results, [
{ match: "GitHub", startIndex: 1, endIndex: 7 },
{ match: "git", startIndex: 65, endIndex: 68 },
{ match: "GitHub", startIndex: 107, endIndex: 113 },
{ match: "2018-04-10", startIndex: 126, endIndex: 136 }
{ match: "GitHub", startIndex: 1, endIndex: 7, captures: [] },
{ match: "git", startIndex: 65, endIndex: 68, captures: [] },
{ match: "GitHub", startIndex: 107, endIndex: 113, captures: [] },
{ match: "2018-04-10", startIndex: 126, endIndex: 136, captures: ["2018", "04", "10"] }
]);
});

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

const results = matchPatterns(inputText, inputPatterns);
assert.deepStrictEqual(results, [{ match: "a pen", startIndex: 8, endIndex: 13 }]);
assert.deepStrictEqual(results, [{ match: "a pen", startIndex: 8, endIndex: 13, captures: ["pen"] }]);
});
1 change: 1 addition & 0 deletions test/snapshot-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe("Snapshot testing", () => {
.map((result) => {
return `
- match text: **${result.match}**
- captures: **${JSON.stringify(result.captures)}**
- startIndex: **${result.startIndex}**
- endIndex: **${result.endIndex}**

Expand Down
3 changes: 3 additions & 0 deletions test/snapshots/capture-multi-regexp/input-patterns.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"/(\\$)(\\d+)/i"
]
1 change: 1 addition & 0 deletions test/snapshots/capture-multi-regexp/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$200 and $400
23 changes: 23 additions & 0 deletions test/snapshots/capture-multi-regexp/output-for-human.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

- match text: **$200**
- captures: **["$","200"]**
- startIndex: **0**
- endIndex: **4**

```
**$200** and $400

```



- match text: **$400**
- captures: **["$","400"]**
- startIndex: **9**
- endIndex: **13**

```
$200 and **$400**

```

20 changes: 20 additions & 0 deletions test/snapshots/capture-multi-regexp/output-for-machine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"match": "$200",
"captures": [
"$",
"200"
],
"startIndex": 0,
"endIndex": 4
},
{
"match": "$400",
"captures": [
"$",
"400"
],
"startIndex": 9,
"endIndex": 13
}
]
3 changes: 3 additions & 0 deletions test/snapshots/capture-single-regexp/input-patterns.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"/(\\d+)/i"
]
1 change: 1 addition & 0 deletions test/snapshots/capture-single-regexp/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A123456789C
11 changes: 11 additions & 0 deletions test/snapshots/capture-single-regexp/output-for-human.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

- match text: **123456789**
- captures: **["123456789"]**
- startIndex: **1**
- endIndex: **10**

```
A**123456789**C

```

10 changes: 10 additions & 0 deletions test/snapshots/capture-single-regexp/output-for-machine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"match": "123456789",
"captures": [
"123456789"
],
"startIndex": 1,
"endIndex": 10
}
]
3 changes: 3 additions & 0 deletions test/snapshots/duplicated-match-patterns/output-for-human.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

- match text: **Git**
- captures: **[]**
- startIndex: **0**
- endIndex: **3**

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


- match text: **git**
- captures: **[]**
- startIndex: **64**
- endIndex: **67**

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


- match text: **Git**
- captures: **[]**
- startIndex: **106**
- endIndex: **109**

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
[
{
"match": "Git",
"captures": [],
"startIndex": 0,
"endIndex": 3
},
{
"match": "git",
"captures": [],
"startIndex": 64,
"endIndex": 67
},
{
"match": "Git",
"captures": [],
"startIndex": 106,
"endIndex": 109
}
Expand Down
2 changes: 2 additions & 0 deletions test/snapshots/global-regexp/output-for-human.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

- match text: **GitHub**
- captures: **[]**
- startIndex: **0**
- endIndex: **6**

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


- match text: **GitHub**
- captures: **[]**
- startIndex: **106**
- endIndex: **112**

Expand Down
2 changes: 2 additions & 0 deletions test/snapshots/global-regexp/output-for-machine.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[
{
"match": "GitHub",
"captures": [],
"startIndex": 0,
"endIndex": 6
},
{
"match": "GitHub",
"captures": [],
"startIndex": 106,
"endIndex": 112
}
Expand Down
3 changes: 3 additions & 0 deletions test/snapshots/ignore-case-regexp/output-for-human.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

- match text: **Git**
- captures: **[]**
- startIndex: **0**
- endIndex: **3**

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


- match text: **git**
- captures: **[]**
- startIndex: **64**
- endIndex: **67**

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


- match text: **Git**
- captures: **[]**
- startIndex: **106**
- endIndex: **109**

Expand Down
3 changes: 3 additions & 0 deletions test/snapshots/ignore-case-regexp/output-for-machine.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
[
{
"match": "Git",
"captures": [],
"startIndex": 0,
"endIndex": 3
},
{
"match": "git",
"captures": [],
"startIndex": 64,
"endIndex": 67
},
{
"match": "Git",
"captures": [],
"startIndex": 106,
"endIndex": 109
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

- match text: **git**
- captures: **[]**
- startIndex: **64**
- endIndex: **67**

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


- match text: **2018**
- captures: **[]**
- startIndex: **125**
- endIndex: **129**

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


- match text: **04**
- captures: **[]**
- startIndex: **130**
- endIndex: **132**

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


- match text: **10**
- captures: **[]**
- startIndex: **133**
- endIndex: **135**

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
[
{
"match": "git",
"captures": [],
"startIndex": 64,
"endIndex": 67
},
{
"match": "2018",
"captures": [],
"startIndex": 125,
"endIndex": 129
},
{
"match": "04",
"captures": [],
"startIndex": 130,
"endIndex": 132
},
{
"match": "10",
"captures": [],
"startIndex": 133,
"endIndex": 135
}
Expand Down
Loading