Skip to content

Commit

Permalink
Fixed relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
zakstucke committed Nov 30, 2023
1 parent e9c4081 commit 2919149
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 8 additions & 2 deletions js/bitbazaar/utils/genPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
export const genPath = (
path: string,
{
sShlash = undefined, // If isn't a root url (e.g. starting with http), then will default to true, otherwise false
sShlash = undefined, // If isn't a root url (e.g. starting with http), or a relative path, then will default to true, otherwise false
eSlash = undefined, // NOTE! defaults to true if dir, false if file
extra = undefined, // Extra sections to add to the end of the path, the sSlash/eSlash applies after this has been added:
}: {
Expand All @@ -14,6 +14,12 @@ export const genPath = (
extra?: string[];
} = {},
): string => {
// Strip any leading or trailing whitespace:
path = path.trim();
if (extra) {
extra = extra.map((e) => e.trim());
}

if (extra) {
// Adding sections, start with / at end:
if (!path.endsWith("/")) {
Expand All @@ -28,7 +34,7 @@ export const genPath = (

// Decide whether sSlash should default to true or false depending on whether it looks like a root url:
if (sShlash === undefined) {
sShlash = !path.startsWith("http");
sShlash = !path.startsWith("http") && !path.startsWith(".");
}

// Decide whether eSlash should default to true or false
Expand Down
4 changes: 4 additions & 0 deletions js/bitbazaar/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ describe("Utils", () => {
assert.equal(genPath("test.txt"), "/test.txt");
// Url looking path no where:
assert.equal(genPath("http://test.com"), "http://test.com");
// Relative path should add at end but not beginning:
assert.equal(genPath("./test"), "./test/");
// Double for good measure:
assert.equal(genPath("../test/"), "../test/");
});
it("Overrides", () => {
assert.equal(genPath("/test/", { eSlash: false, sShlash: false }), "test");
Expand Down

0 comments on commit 2919149

Please sign in to comment.