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 named capturing groups to regexpToRegexp method #225

Merged
merged 6 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
61 changes: 58 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@
"@size-limit/preset-small-lib": "^2.1.6",
"@types/jest": "^24.0.22",
"@types/node": "^12.12.7",
"@types/semver": "^7.3.1",
"husky": "^3.0.9",
"jest": "^24.9.0",
"lint-staged": "^9.4.2",
"prettier": "^1.19.1",
"rimraf": "^3.0.0",
"semver": "^7.3.2",
"ts-jest": "^24.1.0",
"tslint": "^5.20.1",
"tslint-config-prettier": "^1.18.0",
Expand Down
54 changes: 54 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as util from "util";
import * as pathToRegexp from "./index";
import { gte } from "semver";

type Test = [
pathToRegexp.Path,
Expand Down Expand Up @@ -2594,6 +2595,59 @@ const TESTS: Test[] = [
]
];

/**
* Named capturing groups (available from Node version 10)
*/
if (gte(process.version, "10.0.0")) {
TESTS.push(
[
/\/(?<groupname>.+)/,
undefined,
[
{
name: "groupname",
prefix: "",
suffix: "",
modifier: "",
pattern: ""
}
],
[
["/", null],
["/foo", ["/foo", "foo"]]
],
[]
],
[
/\/(?<test>.*).(?<format>html|json)/,
undefined,
[
{
name: "test",
prefix: "",
suffix: "",
modifier: "",
pattern: ""
},
{
name: "format",
prefix: "",
suffix: "",
modifier: "",
pattern: ""
}
],
[
["/route", null],
["/route.txt", null],
["/route.html", ["/route.html", "route", "html"]],
["/route.json", ["/route.json", "route", "json"]]
],
[]
]
);
}

/**
* Dynamically generate the entire test suite.
*/
Expand Down
28 changes: 15 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,19 +453,21 @@ export type Token = string | Key;
function regexpToRegexp(path: RegExp, keys?: Key[]): RegExp {
if (!keys) return path;

// Use a negative lookahead to match only capturing groups.
const groups = path.source.match(/\((?!\?)/g);

if (groups) {
for (let i = 0; i < groups.length; i++) {
keys.push({
name: i,
prefix: "",
suffix: "",
modifier: "",
pattern: ""
});
}
const groupsRegex = /\((?:\?<(.*?)>)?(?!\?)/g;

let index = 0;
let execResult = groupsRegex.exec(path.source);
while (execResult) {
keys.push({
// Use parenthesized substring match if available, index otherwise
name: execResult[1] || index,
prefix: "",
suffix: "",
modifier: "",
pattern: ""
});
index++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably be good to stay consistent with the behavior in string tokens. In that method, we're only incrementing the counter each non-named group:

name: name || key++,
.

That would mean you probably want execResult[1] || index++. We should add a mixed test of named and unnamed groups to make sure it doesn't break.

Copy link
Contributor Author

@DamianKu DamianKu Jul 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK will do that.

Does it mean that this /\/(.+)\/(?<groupname>.+)\/(.+)/ called with /testRegex/testData/extraStuff should result in {"0":"testRegex","1":"extraStuff","groupname":"testData"} ?

execResult = groupsRegex.exec(path.source);
}

return path;
Expand Down