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 all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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
90 changes: 90 additions & 0 deletions src/index.spec.ts
@@ -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,95 @@ 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"]]
],
[]
],
[
/\/(.+)\/(?<groupname>.+)\/(.+)/,
undefined,
[
{
name: 0,
prefix: "",
suffix: "",
modifier: "",
pattern: ""
},
{
name: "groupname",
prefix: "",
suffix: "",
modifier: "",
pattern: ""
},
{
name: 1,
prefix: "",
suffix: "",
modifier: "",
pattern: ""
}
],
[
["/test", null],
["/test/testData", null],
[
"/test/testData/extraStuff",
["/test/testData/extraStuff", "test", "testData", "extraStuff"]
]
],
[]
]
);
}

/**
* Dynamically generate the entire test suite.
*/
Expand Down
27 changes: 14 additions & 13 deletions src/index.ts
Expand Up @@ -453,19 +453,20 @@ 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: ""
});
execResult = groupsRegex.exec(path.source);
}

return path;
Expand Down