Skip to content

Commit 031ac6d

Browse files
Filmbostock
andauthored
a parameterized route can map [foo] onto itself (#1767)
* a parameterized route can map [foo] onto itself * isParameterized; lift test --------- Co-authored-by: Mike Bostock <[email protected]>
1 parent 1b821c9 commit 031ac6d

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/route.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ function routeParams(root: string, cwd: string, parts: string[], exts: string[])
6262
return;
6363
case 1: {
6464
const [first] = parts;
65-
for (const ext of exts) {
66-
const path = join(root, cwd, first + ext);
67-
if (existsSync(path)) {
68-
if (!statSync(path).isFile()) return; // ignore non-files
69-
return {path: join(cwd, first + ext), ext};
65+
if (!isParameterized(first)) {
66+
for (const ext of exts) {
67+
const path = join(root, cwd, first + ext);
68+
if (existsSync(path)) {
69+
if (!statSync(path).isFile()) return; // ignore non-files
70+
return {path: join(cwd, first + ext), ext};
71+
}
7072
}
7173
}
7274
if (first) {
@@ -84,8 +86,10 @@ function routeParams(root: string, cwd: string, parts: string[], exts: string[])
8486
const path = join(root, cwd, first);
8587
if (existsSync(path)) {
8688
if (!statSync(path).isDirectory()) return; // ignore non-directories
87-
const found = routeParams(root, join(cwd, first), rest, exts);
88-
if (found) return found;
89+
if (!isParameterized(first)) {
90+
const found = routeParams(root, join(cwd, first), rest, exts);
91+
if (found) return found;
92+
}
8993
}
9094
if (first) {
9195
for (const dir of globSync("*\\[?*\\]*/", {cwd: join(root, cwd)})) {

test/route-test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ describe("route(root, path, exts)", () => {
4747
it("finds a non-parameterized file ahead of a parameterized file", () => {
4848
assert.deepStrictEqual(route("test/input/params", "foo", [".md"]), {path: "foo.md", ext: ".md"}); // prettier-ignore
4949
});
50+
it("maps a bracketed parameter onto itself", () => {
51+
assert.deepStrictEqual(route("test/input/params", "[dir]/[file]", [".md"]), {path: "[dir]/[file].md", ext: ".md", params: {dir: "[dir]", file: "[file]"}}); // prettier-ignore
52+
assert.deepStrictEqual(route("test/input/params", "[dir]/foo", [".md"]), {path: "[dir]/foo.md", ext: ".md", params: {dir: "[dir]"}}); // prettier-ignore
53+
assert.deepStrictEqual(route("test/input/params", "[dir]/[baz]", [".md"]), {path: "[dir]/[file].md", ext: ".md", params: {dir: "[dir]", file: "[baz]"}}); // prettier-ignore
54+
assert.deepStrictEqual(route("test/input/params", "foo/[file]", [".md"]), {path: "foo/[file].md", ext: ".md", params: {file: "[file]"}}); // prettier-ignore
55+
assert.deepStrictEqual(route("test/input/params", "[foo]-suffix", [".js"]), {path: "[file]-suffix.js", ext: ".js", params: {file: "[foo]"}}); // prettier-ignore
56+
assert.deepStrictEqual(route("test/input/params", "[file]-suffix", [".js"]), {path: "[file]-suffix.js", ext: ".js", params: {file: "[file]"}}); // prettier-ignore
57+
});
5058
it("finds the most-specific parameterized match", () => {
5159
assert.deepStrictEqual(route("test/input/params", "foo/foo", [".md"]), {path: "foo/foo.md", ext: ".md"}); // prettier-ignore
5260
assert.deepStrictEqual(route("test/input/params", "foo/bar", [".md"]), {path: "foo/[file].md", ext: ".md", params: {file: "bar"}}); // prettier-ignore

0 commit comments

Comments
 (0)