Skip to content

Commit

Permalink
fix joinURIAndPath()
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper committed Jan 9, 2025
1 parent 4fc9cbe commit 8e73119
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/request.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as vitest from "vitest";

import { joinURIAndPath } from "./request.js";

vitest.test("joinBaseURIAndPath()", () => {
vitest.expect(joinURIAndPath("https://example.com", "/hi")).toBe("https://example.com/hi");
vitest.expect(joinURIAndPath("https://example.com/", "/hi")).toBe("https://example.com/hi");
vitest.expect(joinURIAndPath("https://example.com/", "hi")).toBe("https://example.com/hi");
vitest.expect(joinURIAndPath("https://example.com", "hi")).toBe("https://example.com/hi");
vitest.expect(joinURIAndPath("https://example.com", "/hi/")).toBe("https://example.com/hi/");

vitest
.expect(joinURIAndPath("https://example.com", "/hi", "/bye"))
.toBe("https://example.com/hi/bye");
vitest
.expect(joinURIAndPath("https://example.com", "hi", "bye"))
.toBe("https://example.com/hi/bye");
vitest
.expect(joinURIAndPath("https://example.com", "/hi/", "/bye/"))
.toBe("https://example.com/hi/bye/");
});
3 changes: 1 addition & 2 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { trimLeft, trimRight } from "./utils.js";
export function joinURIAndPath(base: string, ...path: string[]): string {
let joined = trimRight(base, "/");
for (const part of path) {
joined += "/";
joined += trimRight(trimLeft(part, "/"), "/");
joined = trimRight(joined, "/") + "/" + trimLeft(part, "/");
}
return joined;
}
Expand Down

0 comments on commit 8e73119

Please sign in to comment.