Skip to content

Commit

Permalink
a parameterized route can map [foo] onto itself (#1767)
Browse files Browse the repository at this point in the history
* a parameterized route can map [foo] onto itself

* isParameterized; lift test

---------

Co-authored-by: Mike Bostock <[email protected]>
  • Loading branch information
Fil and mbostock authored Oct 19, 2024
1 parent 1b821c9 commit 031ac6d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@ function routeParams(root: string, cwd: string, parts: string[], exts: string[])
return;
case 1: {
const [first] = parts;
for (const ext of exts) {
const path = join(root, cwd, first + ext);
if (existsSync(path)) {
if (!statSync(path).isFile()) return; // ignore non-files
return {path: join(cwd, first + ext), ext};
if (!isParameterized(first)) {
for (const ext of exts) {
const path = join(root, cwd, first + ext);
if (existsSync(path)) {
if (!statSync(path).isFile()) return; // ignore non-files
return {path: join(cwd, first + ext), ext};
}
}
}
if (first) {
Expand All @@ -84,8 +86,10 @@ function routeParams(root: string, cwd: string, parts: string[], exts: string[])
const path = join(root, cwd, first);
if (existsSync(path)) {
if (!statSync(path).isDirectory()) return; // ignore non-directories
const found = routeParams(root, join(cwd, first), rest, exts);
if (found) return found;
if (!isParameterized(first)) {
const found = routeParams(root, join(cwd, first), rest, exts);
if (found) return found;
}
}
if (first) {
for (const dir of globSync("*\\[?*\\]*/", {cwd: join(root, cwd)})) {
Expand Down
8 changes: 8 additions & 0 deletions test/route-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ describe("route(root, path, exts)", () => {
it("finds a non-parameterized file ahead of a parameterized file", () => {
assert.deepStrictEqual(route("test/input/params", "foo", [".md"]), {path: "foo.md", ext: ".md"}); // prettier-ignore
});
it("maps a bracketed parameter onto itself", () => {
assert.deepStrictEqual(route("test/input/params", "[dir]/[file]", [".md"]), {path: "[dir]/[file].md", ext: ".md", params: {dir: "[dir]", file: "[file]"}}); // prettier-ignore
assert.deepStrictEqual(route("test/input/params", "[dir]/foo", [".md"]), {path: "[dir]/foo.md", ext: ".md", params: {dir: "[dir]"}}); // prettier-ignore
assert.deepStrictEqual(route("test/input/params", "[dir]/[baz]", [".md"]), {path: "[dir]/[file].md", ext: ".md", params: {dir: "[dir]", file: "[baz]"}}); // prettier-ignore
assert.deepStrictEqual(route("test/input/params", "foo/[file]", [".md"]), {path: "foo/[file].md", ext: ".md", params: {file: "[file]"}}); // prettier-ignore
assert.deepStrictEqual(route("test/input/params", "[foo]-suffix", [".js"]), {path: "[file]-suffix.js", ext: ".js", params: {file: "[foo]"}}); // prettier-ignore
assert.deepStrictEqual(route("test/input/params", "[file]-suffix", [".js"]), {path: "[file]-suffix.js", ext: ".js", params: {file: "[file]"}}); // prettier-ignore
});
it("finds the most-specific parameterized match", () => {
assert.deepStrictEqual(route("test/input/params", "foo/foo", [".md"]), {path: "foo/foo.md", ext: ".md"}); // prettier-ignore
assert.deepStrictEqual(route("test/input/params", "foo/bar", [".md"]), {path: "foo/[file].md", ext: ".md", params: {file: "bar"}}); // prettier-ignore
Expand Down

0 comments on commit 031ac6d

Please sign in to comment.