Skip to content

Commit

Permalink
fix(joinURL): handle leading dot (resolves #35)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 28, 2023
1 parent b35d93d commit 0e060c7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,19 @@ export function isNonEmptyURL(url: string) {
return url && url !== "/";
}

const JOIN_LEADING_SLASH_RE = /^\.?\//;

export function joinURL(base: string, ...input: string[]): string {
let url = base || "";

for (const index of input.filter((url) => isNonEmptyURL(url))) {
url = url ? withTrailingSlash(url) + withoutLeadingSlash(index) : index;
for (const segment of input.filter((url) => isNonEmptyURL(url))) {
if (!url) {

Check failure on line 155 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci

Unexpected negated condition
url = segment;
} else {
// TODO: Handle .. when joining
const _segment = segment.replace(JOIN_LEADING_SLASH_RE, "");
url = withTrailingSlash(url) + _segment;
}
}

return url;
Expand Down
6 changes: 5 additions & 1 deletion test/join.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ describe("joinURL", () => {
{ input: ["/", "/b"], out: "/b" },
{ input: ["a", "b/", "c"], out: "a/b/c" },
{ input: ["a", "b/", "/c"], out: "a/b/c" },
{ input: ["/", "./"], out: "/" },
{ input: ["/", "./foo"], out: "/foo" },
{ input: ["/", "./foo/"], out: "/foo/" },
{ input: ["/", "./foo", "bar"], out: "/foo/bar" },
];

for (const t of tests) {
test(t.input.toString(), () => {
test(JSON.stringify(t.input), () => {
expect(joinURL(...t.input)).toBe(t.out);
});
}
Expand Down

0 comments on commit 0e060c7

Please sign in to comment.