Skip to content

Commit f60b43f

Browse files
authored
fix: preserve empty segments (#160)
1 parent 8b2ea34 commit f60b43f

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

β€Žsrc/operations/_utils.tsβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { NullProtoObj } from "../_utils.ts";
22
import type { MatchedRoute, ParamsIndexMap } from "../types.ts";
33

44
export function splitPath(path: string): string[] {
5-
return path.split("/").filter(Boolean);
5+
const [_, ...s] = path.split("/");
6+
return s[s.length - 1] === "" ? s.slice(0, -1) : s;
67
}
78

89
export function getMatchParams(

β€Žtest/_utils.tsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function formatTree(
2525
): string | string[] {
2626
result.push(
2727
// prettier-ignore
28-
`${prefix}${depth === 0 ? "" : "β”œβ”€β”€ "}${node.key ? `/${node.key}` : (depth === 0 ? "<root>" : "<?>")}${_formatMethods(node)}`,
28+
`${prefix}${depth === 0 ? "" : "β”œβ”€β”€ "}${node.key ? `/${node.key}` : (depth === 0 ? "<root>" : "<empty>")}${_formatMethods(node)}`,
2929
);
3030

3131
const childrenArray = [

β€Žtest/bench/bundle.test.tsβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe("benchmark", () => {
1414
`;
1515
const { bytes, gzipSize } = await getBundleSize(code);
1616
console.log("bundle size", { bytes, gzipSize });
17-
expect(bytes).toBeLessThanOrEqual(2700); // <2.7kb
17+
expect(bytes).toBeLessThanOrEqual(2800); // <2.8kb
1818
expect(gzipSize).toBeLessThanOrEqual(1100); // <1.1kb
1919
});
2020
});

β€Žtest/router.test.tsβ€Ž

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,29 @@ describe("Router lookup", function () {
386386
},
387387
);
388388
});
389+
390+
describe("empty segments", function () {
391+
testRouter(
392+
["/test//route", "/test/:param/route"],
393+
(router) =>
394+
expect(formatTree(router.root)).toMatchInlineSnapshot(`
395+
"<root>
396+
β”œβ”€β”€ /test
397+
β”‚ β”œβ”€β”€ <empty>
398+
β”‚ β”‚ β”œβ”€β”€ /route β”ˆ> [GET] /test//route
399+
β”‚ β”œβ”€β”€ /*
400+
β”‚ β”‚ β”œβ”€β”€ /route β”ˆ> [GET] /test/:param/route"
401+
`),
402+
{
403+
"/test//route": {
404+
data: { path: "/test//route" },
405+
},
406+
"/test/id/route": {
407+
data: { path: "/test/:param/route" },
408+
},
409+
},
410+
);
411+
});
389412
});
390413

391414
describe("Router insert", () => {

0 commit comments

Comments
Β (0)