From 29191492ed73c119b808594432ec41e9232a7796 Mon Sep 17 00:00:00 2001 From: Zak Stucke Date: Thu, 30 Nov 2023 15:54:39 +0000 Subject: [PATCH] Fixed relative paths --- js/bitbazaar/utils/genPath.ts | 10 ++++++++-- js/bitbazaar/utils/utils.test.ts | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/js/bitbazaar/utils/genPath.ts b/js/bitbazaar/utils/genPath.ts index 53e06081..09c455d8 100644 --- a/js/bitbazaar/utils/genPath.ts +++ b/js/bitbazaar/utils/genPath.ts @@ -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: }: { @@ -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("/")) { @@ -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 diff --git a/js/bitbazaar/utils/utils.test.ts b/js/bitbazaar/utils/utils.test.ts index 07d416b5..f953c0ec 100644 --- a/js/bitbazaar/utils/utils.test.ts +++ b/js/bitbazaar/utils/utils.test.ts @@ -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");