From 308ed3fd8645054101f12cc5f4c72ebc5c103952 Mon Sep 17 00:00:00 2001 From: azu Date: Wed, 18 May 2022 08:17:36 +0900 Subject: [PATCH 1/2] feat: support `captures` BREAKING CHANGE: Drop to support Node.js --- package.json | 1 - src/regexp-string-matcher.ts | 15 ++++++++---- test/example-test.ts | 14 +++++------ test/snapshot-test.ts | 1 + .../capture-multi-regexp/input-patterns.json | 3 +++ test/snapshots/capture-multi-regexp/input.txt | 1 + .../capture-multi-regexp/output-for-human.md | 23 +++++++++++++++++++ .../output-for-machine.json | 20 ++++++++++++++++ .../capture-single-regexp/input-patterns.json | 3 +++ .../snapshots/capture-single-regexp/input.txt | 1 + .../capture-single-regexp/output-for-human.md | 11 +++++++++ .../output-for-machine.json | 10 ++++++++ .../output-for-human.md | 3 +++ .../output-for-machine.json | 3 +++ .../global-regexp/output-for-human.md | 2 ++ .../global-regexp/output-for-machine.json | 2 ++ .../ignore-case-regexp/output-for-human.md | 3 +++ .../output-for-machine.json | 3 +++ .../output-for-human.md | 4 ++++ .../output-for-machine.json | 4 ++++ .../output-for-human.md | 3 +++ .../output-for-machine.json | 3 +++ .../output-for-human.md | 5 ++++ .../output-for-machine.json | 5 ++++ .../multiline-regexp/output-for-human.md | 3 +++ .../multiline-regexp/output-for-machine.json | 3 +++ .../number-regexp/output-for-human.md | 1 + .../number-regexp/output-for-machine.json | 1 + .../simple-regexp/output-for-human.md | 2 ++ .../simple-regexp/output-for-machine.json | 2 ++ .../slash-escaped-regexp/output-for-human.md | 1 + .../output-for-machine.json | 1 + .../slash-regexp/output-for-human.md | 1 + .../slash-regexp/output-for-machine.json | 1 + .../string-pattern/output-for-human.md | 2 ++ .../string-pattern/output-for-machine.json | 2 ++ .../unicode-regexp/output-for-human.md | 3 +++ .../unicode-regexp/output-for-machine.json | 3 +++ .../unnatural-pattern/output-for-human.md | 5 ++++ .../unnatural-pattern/output-for-machine.json | 5 ++++ yarn.lock | 19 --------------- 41 files changed, 166 insertions(+), 32 deletions(-) create mode 100644 test/snapshots/capture-multi-regexp/input-patterns.json create mode 100644 test/snapshots/capture-multi-regexp/input.txt create mode 100644 test/snapshots/capture-multi-regexp/output-for-human.md create mode 100644 test/snapshots/capture-multi-regexp/output-for-machine.json create mode 100644 test/snapshots/capture-single-regexp/input-patterns.json create mode 100644 test/snapshots/capture-single-regexp/input.txt create mode 100644 test/snapshots/capture-single-regexp/output-for-human.md create mode 100644 test/snapshots/capture-single-regexp/output-for-machine.json diff --git a/package.json b/package.json index a79e131..dd0075d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/regexp-string-matcher.ts b/src/regexp-string-matcher.ts index c2bdcd1..9a5ad28 100644 --- a/src/regexp-string-matcher.ts +++ b/src/regexp-string-matcher.ts @@ -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"; @@ -18,6 +16,9 @@ const defaultFlags = (flagsString: string) => { export interface matchPatternResult { match: string; + // captured results + // [$1, $2 ...] + captures: string[]; startIndex: number; endIndex: number; } @@ -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 }); diff --git a/test/example-test.ts b/test/example-test.ts index 13c8e4e..8d5ad35 100644 --- a/test/example-test.ts +++ b/test/example-test.ts @@ -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"] }]); }); diff --git a/test/snapshot-test.ts b/test/snapshot-test.ts index f71b846..0d411fa 100644 --- a/test/snapshot-test.ts +++ b/test/snapshot-test.ts @@ -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}** diff --git a/test/snapshots/capture-multi-regexp/input-patterns.json b/test/snapshots/capture-multi-regexp/input-patterns.json new file mode 100644 index 0000000..4edab09 --- /dev/null +++ b/test/snapshots/capture-multi-regexp/input-patterns.json @@ -0,0 +1,3 @@ +[ + "/(\\$)(\\d+)/i" +] diff --git a/test/snapshots/capture-multi-regexp/input.txt b/test/snapshots/capture-multi-regexp/input.txt new file mode 100644 index 0000000..aacdc2d --- /dev/null +++ b/test/snapshots/capture-multi-regexp/input.txt @@ -0,0 +1 @@ +$200 and $400 diff --git a/test/snapshots/capture-multi-regexp/output-for-human.md b/test/snapshots/capture-multi-regexp/output-for-human.md new file mode 100644 index 0000000..d68056b --- /dev/null +++ b/test/snapshots/capture-multi-regexp/output-for-human.md @@ -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** + +``` + diff --git a/test/snapshots/capture-multi-regexp/output-for-machine.json b/test/snapshots/capture-multi-regexp/output-for-machine.json new file mode 100644 index 0000000..3e27ac1 --- /dev/null +++ b/test/snapshots/capture-multi-regexp/output-for-machine.json @@ -0,0 +1,20 @@ +[ + { + "match": "$200", + "captures": [ + "$", + "200" + ], + "startIndex": 0, + "endIndex": 4 + }, + { + "match": "$400", + "captures": [ + "$", + "400" + ], + "startIndex": 9, + "endIndex": 13 + } +] \ No newline at end of file diff --git a/test/snapshots/capture-single-regexp/input-patterns.json b/test/snapshots/capture-single-regexp/input-patterns.json new file mode 100644 index 0000000..7fb7403 --- /dev/null +++ b/test/snapshots/capture-single-regexp/input-patterns.json @@ -0,0 +1,3 @@ +[ + "/(\\d+)/i" +] diff --git a/test/snapshots/capture-single-regexp/input.txt b/test/snapshots/capture-single-regexp/input.txt new file mode 100644 index 0000000..5be00e9 --- /dev/null +++ b/test/snapshots/capture-single-regexp/input.txt @@ -0,0 +1 @@ +A123456789C diff --git a/test/snapshots/capture-single-regexp/output-for-human.md b/test/snapshots/capture-single-regexp/output-for-human.md new file mode 100644 index 0000000..051732d --- /dev/null +++ b/test/snapshots/capture-single-regexp/output-for-human.md @@ -0,0 +1,11 @@ + +- match text: **123456789** +- captures: **["123456789"]** +- startIndex: **1** +- endIndex: **10** + +``` +A**123456789**C + +``` + diff --git a/test/snapshots/capture-single-regexp/output-for-machine.json b/test/snapshots/capture-single-regexp/output-for-machine.json new file mode 100644 index 0000000..8eb3590 --- /dev/null +++ b/test/snapshots/capture-single-regexp/output-for-machine.json @@ -0,0 +1,10 @@ +[ + { + "match": "123456789", + "captures": [ + "123456789" + ], + "startIndex": 1, + "endIndex": 10 + } +] \ No newline at end of file diff --git a/test/snapshots/duplicated-match-patterns/output-for-human.md b/test/snapshots/duplicated-match-patterns/output-for-human.md index 9914e0f..9973b33 100644 --- a/test/snapshots/duplicated-match-patterns/output-for-human.md +++ b/test/snapshots/duplicated-match-patterns/output-for-human.md @@ -1,5 +1,6 @@ - match text: **Git** +- captures: **[]** - startIndex: **0** - endIndex: **3** @@ -13,6 +14,7 @@ GitHub launched in 2018-04-10. - match text: **git** +- captures: **[]** - startIndex: **64** - endIndex: **67** @@ -26,6 +28,7 @@ GitHub launched in 2018-04-10. - match text: **Git** +- captures: **[]** - startIndex: **106** - endIndex: **109** diff --git a/test/snapshots/duplicated-match-patterns/output-for-machine.json b/test/snapshots/duplicated-match-patterns/output-for-machine.json index 5aef8cc..e309fa6 100644 --- a/test/snapshots/duplicated-match-patterns/output-for-machine.json +++ b/test/snapshots/duplicated-match-patterns/output-for-machine.json @@ -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 } diff --git a/test/snapshots/global-regexp/output-for-human.md b/test/snapshots/global-regexp/output-for-human.md index d867fe3..d5ce627 100644 --- a/test/snapshots/global-regexp/output-for-human.md +++ b/test/snapshots/global-regexp/output-for-human.md @@ -1,5 +1,6 @@ - match text: **GitHub** +- captures: **[]** - startIndex: **0** - endIndex: **6** @@ -13,6 +14,7 @@ GitHub launched in 2018-04-10. - match text: **GitHub** +- captures: **[]** - startIndex: **106** - endIndex: **112** diff --git a/test/snapshots/global-regexp/output-for-machine.json b/test/snapshots/global-regexp/output-for-machine.json index bd8d6df..930ffde 100644 --- a/test/snapshots/global-regexp/output-for-machine.json +++ b/test/snapshots/global-regexp/output-for-machine.json @@ -1,11 +1,13 @@ [ { "match": "GitHub", + "captures": [], "startIndex": 0, "endIndex": 6 }, { "match": "GitHub", + "captures": [], "startIndex": 106, "endIndex": 112 } diff --git a/test/snapshots/ignore-case-regexp/output-for-human.md b/test/snapshots/ignore-case-regexp/output-for-human.md index 9914e0f..9973b33 100644 --- a/test/snapshots/ignore-case-regexp/output-for-human.md +++ b/test/snapshots/ignore-case-regexp/output-for-human.md @@ -1,5 +1,6 @@ - match text: **Git** +- captures: **[]** - startIndex: **0** - endIndex: **3** @@ -13,6 +14,7 @@ GitHub launched in 2018-04-10. - match text: **git** +- captures: **[]** - startIndex: **64** - endIndex: **67** @@ -26,6 +28,7 @@ GitHub launched in 2018-04-10. - match text: **Git** +- captures: **[]** - startIndex: **106** - endIndex: **109** diff --git a/test/snapshots/ignore-case-regexp/output-for-machine.json b/test/snapshots/ignore-case-regexp/output-for-machine.json index 5aef8cc..e309fa6 100644 --- a/test/snapshots/ignore-case-regexp/output-for-machine.json +++ b/test/snapshots/ignore-case-regexp/output-for-machine.json @@ -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 } diff --git a/test/snapshots/mixed-string-regexp-patterns/output-for-human.md b/test/snapshots/mixed-string-regexp-patterns/output-for-human.md index a73d413..dab6bb1 100644 --- a/test/snapshots/mixed-string-regexp-patterns/output-for-human.md +++ b/test/snapshots/mixed-string-regexp-patterns/output-for-human.md @@ -1,5 +1,6 @@ - match text: **git** +- captures: **[]** - startIndex: **64** - endIndex: **67** @@ -13,6 +14,7 @@ GitHub launched in 2018-04-10. - match text: **2018** +- captures: **[]** - startIndex: **125** - endIndex: **129** @@ -26,6 +28,7 @@ GitHub launched in **2018**-04-10. - match text: **04** +- captures: **[]** - startIndex: **130** - endIndex: **132** @@ -39,6 +42,7 @@ GitHub launched in 2018-**04**-10. - match text: **10** +- captures: **[]** - startIndex: **133** - endIndex: **135** diff --git a/test/snapshots/mixed-string-regexp-patterns/output-for-machine.json b/test/snapshots/mixed-string-regexp-patterns/output-for-machine.json index 23077b7..828894b 100644 --- a/test/snapshots/mixed-string-regexp-patterns/output-for-machine.json +++ b/test/snapshots/mixed-string-regexp-patterns/output-for-machine.json @@ -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 } diff --git a/test/snapshots/more-than-one-regexp-patterns/output-for-human.md b/test/snapshots/more-than-one-regexp-patterns/output-for-human.md index 991a86f..4e114e2 100644 --- a/test/snapshots/more-than-one-regexp-patterns/output-for-human.md +++ b/test/snapshots/more-than-one-regexp-patterns/output-for-human.md @@ -1,5 +1,6 @@ - match text: **GitHub** +- captures: **[]** - startIndex: **0** - endIndex: **6** @@ -13,6 +14,7 @@ GitHub launched in 2018-04-10. - match text: **git** +- captures: **[]** - startIndex: **64** - endIndex: **67** @@ -26,6 +28,7 @@ GitHub launched in 2018-04-10. - match text: **GitHub** +- captures: **[]** - startIndex: **106** - endIndex: **112** diff --git a/test/snapshots/more-than-one-regexp-patterns/output-for-machine.json b/test/snapshots/more-than-one-regexp-patterns/output-for-machine.json index 9d66441..d243ce1 100644 --- a/test/snapshots/more-than-one-regexp-patterns/output-for-machine.json +++ b/test/snapshots/more-than-one-regexp-patterns/output-for-machine.json @@ -1,16 +1,19 @@ [ { "match": "GitHub", + "captures": [], "startIndex": 0, "endIndex": 6 }, { "match": "git", + "captures": [], "startIndex": 64, "endIndex": 67 }, { "match": "GitHub", + "captures": [], "startIndex": 106, "endIndex": 112 } diff --git a/test/snapshots/more-than-one-string-patterns/output-for-human.md b/test/snapshots/more-than-one-string-patterns/output-for-human.md index 4dbd38d..b918133 100644 --- a/test/snapshots/more-than-one-string-patterns/output-for-human.md +++ b/test/snapshots/more-than-one-string-patterns/output-for-human.md @@ -1,5 +1,6 @@ - match text: **GitHub** +- captures: **[]** - startIndex: **0** - endIndex: **6** @@ -13,6 +14,7 @@ GitHub launched in 2018-04-10. - match text: **GitHub** +- captures: **[]** - startIndex: **106** - endIndex: **112** @@ -26,6 +28,7 @@ It is mostly used for computer code. - match text: **2018** +- captures: **[]** - startIndex: **125** - endIndex: **129** @@ -39,6 +42,7 @@ GitHub launched in **2018**-04-10. - match text: **04** +- captures: **[]** - startIndex: **130** - endIndex: **132** @@ -52,6 +56,7 @@ GitHub launched in 2018-**04**-10. - match text: **10** +- captures: **[]** - startIndex: **133** - endIndex: **135** diff --git a/test/snapshots/more-than-one-string-patterns/output-for-machine.json b/test/snapshots/more-than-one-string-patterns/output-for-machine.json index bb75e75..64e7b24 100644 --- a/test/snapshots/more-than-one-string-patterns/output-for-machine.json +++ b/test/snapshots/more-than-one-string-patterns/output-for-machine.json @@ -1,26 +1,31 @@ [ { "match": "GitHub", + "captures": [], "startIndex": 0, "endIndex": 6 }, { "match": "GitHub", + "captures": [], "startIndex": 106, "endIndex": 112 }, { "match": "2018", + "captures": [], "startIndex": 125, "endIndex": 129 }, { "match": "04", + "captures": [], "startIndex": 130, "endIndex": 132 }, { "match": "10", + "captures": [], "startIndex": 133, "endIndex": 135 } diff --git a/test/snapshots/multiline-regexp/output-for-human.md b/test/snapshots/multiline-regexp/output-for-human.md index 5211646..94db4b7 100644 --- a/test/snapshots/multiline-regexp/output-for-human.md +++ b/test/snapshots/multiline-regexp/output-for-human.md @@ -2,6 +2,7 @@ - match text: **===START=== 1st inline text. ===END===** +- captures: **[]** - startIndex: **0** - endIndex: **38** @@ -30,6 +31,7 @@ multi line text. - match text: **===START=== 2nd inline text. ===END===** +- captures: **[]** - startIndex: **48** - endIndex: **86** @@ -59,6 +61,7 @@ multi line text. 3rd text. multi line text. ===END===** +- captures: **[]** - startIndex: **96** - endIndex: **144** diff --git a/test/snapshots/multiline-regexp/output-for-machine.json b/test/snapshots/multiline-regexp/output-for-machine.json index 327e0ec..bd0dedd 100644 --- a/test/snapshots/multiline-regexp/output-for-machine.json +++ b/test/snapshots/multiline-regexp/output-for-machine.json @@ -1,16 +1,19 @@ [ { "match": "===START===\n1st inline text.\n===END===", + "captures": [], "startIndex": 0, "endIndex": 38 }, { "match": "===START===\n2nd inline text.\n===END===", + "captures": [], "startIndex": 48, "endIndex": 86 }, { "match": "===START===\n3rd text.\nmulti line text.\n===END===", + "captures": [], "startIndex": 96, "endIndex": 144 } diff --git a/test/snapshots/number-regexp/output-for-human.md b/test/snapshots/number-regexp/output-for-human.md index 5a6294c..bc2a09a 100644 --- a/test/snapshots/number-regexp/output-for-human.md +++ b/test/snapshots/number-regexp/output-for-human.md @@ -1,5 +1,6 @@ - match text: **2018-04-10** +- captures: **[]** - startIndex: **125** - endIndex: **135** diff --git a/test/snapshots/number-regexp/output-for-machine.json b/test/snapshots/number-regexp/output-for-machine.json index 819af4a..8de9551 100644 --- a/test/snapshots/number-regexp/output-for-machine.json +++ b/test/snapshots/number-regexp/output-for-machine.json @@ -1,6 +1,7 @@ [ { "match": "2018-04-10", + "captures": [], "startIndex": 125, "endIndex": 135 } diff --git a/test/snapshots/simple-regexp/output-for-human.md b/test/snapshots/simple-regexp/output-for-human.md index d867fe3..d5ce627 100644 --- a/test/snapshots/simple-regexp/output-for-human.md +++ b/test/snapshots/simple-regexp/output-for-human.md @@ -1,5 +1,6 @@ - match text: **GitHub** +- captures: **[]** - startIndex: **0** - endIndex: **6** @@ -13,6 +14,7 @@ GitHub launched in 2018-04-10. - match text: **GitHub** +- captures: **[]** - startIndex: **106** - endIndex: **112** diff --git a/test/snapshots/simple-regexp/output-for-machine.json b/test/snapshots/simple-regexp/output-for-machine.json index bd8d6df..930ffde 100644 --- a/test/snapshots/simple-regexp/output-for-machine.json +++ b/test/snapshots/simple-regexp/output-for-machine.json @@ -1,11 +1,13 @@ [ { "match": "GitHub", + "captures": [], "startIndex": 0, "endIndex": 6 }, { "match": "GitHub", + "captures": [], "startIndex": 106, "endIndex": 112 } diff --git a/test/snapshots/slash-escaped-regexp/output-for-human.md b/test/snapshots/slash-escaped-regexp/output-for-human.md index de19022..ede814a 100644 --- a/test/snapshots/slash-escaped-regexp/output-for-human.md +++ b/test/snapshots/slash-escaped-regexp/output-for-human.md @@ -1,5 +1,6 @@ - match text: **feature/abc** +- captures: **[]** - startIndex: **0** - endIndex: **11** diff --git a/test/snapshots/slash-escaped-regexp/output-for-machine.json b/test/snapshots/slash-escaped-regexp/output-for-machine.json index 3b5f89e..de68d66 100644 --- a/test/snapshots/slash-escaped-regexp/output-for-machine.json +++ b/test/snapshots/slash-escaped-regexp/output-for-machine.json @@ -1,6 +1,7 @@ [ { "match": "feature/abc", + "captures": [], "startIndex": 0, "endIndex": 11 } diff --git a/test/snapshots/slash-regexp/output-for-human.md b/test/snapshots/slash-regexp/output-for-human.md index de19022..ede814a 100644 --- a/test/snapshots/slash-regexp/output-for-human.md +++ b/test/snapshots/slash-regexp/output-for-human.md @@ -1,5 +1,6 @@ - match text: **feature/abc** +- captures: **[]** - startIndex: **0** - endIndex: **11** diff --git a/test/snapshots/slash-regexp/output-for-machine.json b/test/snapshots/slash-regexp/output-for-machine.json index 3b5f89e..de68d66 100644 --- a/test/snapshots/slash-regexp/output-for-machine.json +++ b/test/snapshots/slash-regexp/output-for-machine.json @@ -1,6 +1,7 @@ [ { "match": "feature/abc", + "captures": [], "startIndex": 0, "endIndex": 11 } diff --git a/test/snapshots/string-pattern/output-for-human.md b/test/snapshots/string-pattern/output-for-human.md index d867fe3..d5ce627 100644 --- a/test/snapshots/string-pattern/output-for-human.md +++ b/test/snapshots/string-pattern/output-for-human.md @@ -1,5 +1,6 @@ - match text: **GitHub** +- captures: **[]** - startIndex: **0** - endIndex: **6** @@ -13,6 +14,7 @@ GitHub launched in 2018-04-10. - match text: **GitHub** +- captures: **[]** - startIndex: **106** - endIndex: **112** diff --git a/test/snapshots/string-pattern/output-for-machine.json b/test/snapshots/string-pattern/output-for-machine.json index bd8d6df..930ffde 100644 --- a/test/snapshots/string-pattern/output-for-machine.json +++ b/test/snapshots/string-pattern/output-for-machine.json @@ -1,11 +1,13 @@ [ { "match": "GitHub", + "captures": [], "startIndex": 0, "endIndex": 6 }, { "match": "GitHub", + "captures": [], "startIndex": 106, "endIndex": 112 } diff --git a/test/snapshots/unicode-regexp/output-for-human.md b/test/snapshots/unicode-regexp/output-for-human.md index 4371ecb..eb47116 100644 --- a/test/snapshots/unicode-regexp/output-for-human.md +++ b/test/snapshots/unicode-regexp/output-for-human.md @@ -1,5 +1,6 @@ - match text: **💩** +- captures: **[]** - startIndex: **3** - endIndex: **5** @@ -11,6 +12,7 @@ - match text: **💩** +- captures: **[]** - startIndex: **7** - endIndex: **9** @@ -22,6 +24,7 @@ - match text: **💩** +- captures: **[]** - startIndex: **11** - endIndex: **13** diff --git a/test/snapshots/unicode-regexp/output-for-machine.json b/test/snapshots/unicode-regexp/output-for-machine.json index b241efe..fa5766f 100644 --- a/test/snapshots/unicode-regexp/output-for-machine.json +++ b/test/snapshots/unicode-regexp/output-for-machine.json @@ -1,16 +1,19 @@ [ { "match": "💩", + "captures": [], "startIndex": 3, "endIndex": 5 }, { "match": "💩", + "captures": [], "startIndex": 7, "endIndex": 9 }, { "match": "💩", + "captures": [], "startIndex": 11, "endIndex": 13 } diff --git a/test/snapshots/unnatural-pattern/output-for-human.md b/test/snapshots/unnatural-pattern/output-for-human.md index b4b826a..eb9b475 100644 --- a/test/snapshots/unnatural-pattern/output-for-human.md +++ b/test/snapshots/unnatural-pattern/output-for-human.md @@ -1,5 +1,6 @@ - match text: **Eメール** +- captures: **[]** - startIndex: **3** - endIndex: **7** @@ -13,6 +14,7 @@ X軸とY軸を見る。 - match text: **X軸** +- captures: **[]** - startIndex: **9** - endIndex: **11** @@ -26,6 +28,7 @@ X軸とY軸を見る。 - match text: **Y軸** +- captures: **[]** - startIndex: **12** - endIndex: **14** @@ -39,6 +42,7 @@ X軸と**Y軸**を見る。 - match text: **X座標** +- captures: **[]** - startIndex: **22** - endIndex: **25** @@ -52,6 +56,7 @@ X軸とY軸を見る。 - match text: **C言語** +- captures: **[]** - startIndex: **29** - endIndex: **32** diff --git a/test/snapshots/unnatural-pattern/output-for-machine.json b/test/snapshots/unnatural-pattern/output-for-machine.json index ef0992e..db3991a 100644 --- a/test/snapshots/unnatural-pattern/output-for-machine.json +++ b/test/snapshots/unnatural-pattern/output-for-machine.json @@ -1,26 +1,31 @@ [ { "match": "Eメール", + "captures": [], "startIndex": 3, "endIndex": 7 }, { "match": "X軸", + "captures": [], "startIndex": 9, "endIndex": 11 }, { "match": "Y軸", + "captures": [], "startIndex": 12, "endIndex": 14 }, { "match": "X座標", + "captures": [], "startIndex": 22, "endIndex": 25 }, { "match": "C言語", + "captures": [], "startIndex": 29, "endIndex": 32 } diff --git a/yarn.lock b/yarn.lock index 24e6bd4..b4d15b4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -304,13 +304,6 @@ cliui@^7.0.2: strip-ansi "^6.0.0" wrap-ansi "^7.0.0" -clone-regexp@^2.1.0: - version "2.2.0" - resolved "https://registry.yarnpkg.com/clone-regexp/-/clone-regexp-2.2.0.tgz#7d65e00885cd8796405c35a737e7a86b7429e36f" - integrity sha512-beMpP7BOtTipFuW8hrJvREQ2DrRu3BE7by0ZpibtfBA+qfHYvMGTc2Yb1JMYPKg/JUw0CHYvpg796aNTSW9z7Q== - dependencies: - is-regexp "^2.0.0" - color-convert@^1.9.0: version "1.9.3" resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.3.tgz#bb71850690e1f136567de629d2d5471deda4c1e8" @@ -458,13 +451,6 @@ execa@^5.1.1: signal-exit "^3.0.3" strip-final-newline "^2.0.0" -execall@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/execall/-/execall-2.0.0.tgz#16a06b5fe5099df7d00be5d9c06eecded1663b45" - integrity sha512-0FU2hZ5Hh6iQnarpRtQurM/aAvp3RIbfvgLHrcqJYzhXyV2KFruhuChf9NC6waAhiUR7FFtlugkI4p7f2Fqlow== - dependencies: - clone-regexp "^2.1.0" - extend-shallow@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/extend-shallow/-/extend-shallow-3.0.2.tgz#26a71aaf073b39fb2127172746131c2704028db8" @@ -675,11 +661,6 @@ is-plain-object@^2.0.4: dependencies: isobject "^3.0.1" -is-regexp@^2.0.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/is-regexp/-/is-regexp-2.1.0.tgz#cd734a56864e23b956bf4e7c66c396a4c0b22c2d" - integrity sha512-OZ4IlER3zmRIoB9AqNhEggVxqIH4ofDns5nRrPS6yQxXE1TPCUpFznBfRQmQa8uC+pXqjMnukiJBxCisIxiLGA== - is-stream@^2.0.0: version "2.0.1" resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-2.0.1.tgz#fac1e3d53b97ad5a9d0ae9cef2389f5810a5c077" From 75618616afe0fc777863a59903204bd56ec5a263 Mon Sep 17 00:00:00 2001 From: azu Date: Wed, 18 May 2022 08:18:42 +0900 Subject: [PATCH 2/2] docs: Update README --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index af4d9aa..30121d8 100644 --- a/README.md +++ b/README.md @@ -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"] } ]); ``` @@ -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