Skip to content

Commit 5d0c8f5

Browse files
committed
fix(routeToRegExp): support named wildcard
1 parent 205430d commit 5d0c8f5

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/regexp.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ export function routeToRegExp(route: string = "/"): RegExp {
44
if (!segment) continue;
55
if (segment === "*") {
66
reSegments.push("[^/]*");
7-
} else if (segment === "**") {
8-
reSegments.push("?(?<_>.*)");
7+
} else if (segment.startsWith("**")) {
8+
reSegments.push(
9+
segment === "**" ? "?(?<_>.*)" : `?(?<${segment.slice(3)}>.+)`,
10+
);
911
} else if (segment.includes(":")) {
1012
reSegments.push(
1113
segment

test/regexp.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ describe("routeToRegExp", () => {
3636
["/path/anything/more", { _: "anything/more" }],
3737
],
3838
},
39+
"/base/**:path": {
40+
regex: /^\/base\/?(?<path>.+)\/?$/,
41+
match: [["/base/anything/more", { path: "anything/more" }]],
42+
},
3943
} as const;
4044

4145
for (const [route, expected] of Object.entries(routes)) {

0 commit comments

Comments
 (0)