Skip to content

Commit 5682d66

Browse files
committed
fix(routeToRegExp): preserve anonymous counter ids
1 parent 5d0c8f5 commit 5682d66

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/regexp.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
export function routeToRegExp(route: string = "/"): RegExp {
22
const reSegments = [];
3+
let idCtr = 0;
34
for (const segment of route.split("/")) {
45
if (!segment) continue;
56
if (segment === "*") {
6-
reSegments.push("[^/]*");
7+
reSegments.push(`(?<_${idCtr++}>[^/]*)`);
78
} else if (segment.startsWith("**")) {
89
reSegments.push(
910
segment === "**" ? "?(?<_>.*)" : `?(?<${segment.slice(3)}>.+)`,

test/regexp.test.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@ describe("routeToRegExp", () => {
2525
match: [["/path/value1/value2", { param1: "value1", param2: "value2" }]],
2626
},
2727
"/path/*/foo": {
28-
regex: /^\/path\/[^/]*\/foo\/?$/,
29-
match: [["/path/anything/foo"], ["/path//foo"], ["/path//foo/"]],
28+
regex: /^\/path\/(?<_0>[^/]*)\/foo\/?$/,
29+
match: [
30+
["/path/anything/foo", { _0: "anything" }],
31+
["/path//foo", { _0: "" }],
32+
["/path//foo/", { _0: "" }],
33+
],
3034
},
3135
"/path/**": {
3236
regex: /^\/path\/?(?<_>.*)\/?$/,
3337
match: [
34-
["/path/"],
35-
["/path"],
38+
["/path/", { _: "" }],
39+
["/path", { _: "" }],
3640
["/path/anything/more", { _: "anything/more" }],
3741
],
3842
},
@@ -50,7 +54,14 @@ describe("routeToRegExp", () => {
5054
const regex = routeToRegExp(route);
5155

5256
for (const [path, params] of expected.match) {
53-
expect(findRoute(router, "", path)).toMatchObject({ data: { route } });
57+
expect(findRoute(router, "", path)).toMatchObject(
58+
params
59+
? {
60+
data: { route },
61+
params,
62+
}
63+
: { data: { route } },
64+
);
5465

5566
const match = path.match(regex);
5667
expect(match, path).not.toBeNull();

0 commit comments

Comments
 (0)