From e0694b4bff0c3a978ac879864513d58da4f82a80 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sun, 17 Dec 2023 11:43:21 +0100 Subject: [PATCH] fix: PathRef#withExtname should allow setting no extension --- src/path.test.ts | 6 ++++++ src/path.ts | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/path.test.ts b/src/path.test.ts index 561c71a..0076d56 100644 --- a/src/path.test.ts +++ b/src/path.test.ts @@ -278,6 +278,12 @@ Deno.test("withExtname", () => { path = path.withExtname(".txt"); assertEquals(path.basename(), "other.txt"); assertEquals(path.extname(), ".txt"); + path = path.withExtname(""); + assertEquals(path.basename(), "other"); + assertEquals(path.extname(), undefined); + path = path.withExtname("txt"); + assertEquals(path.basename(), "other.txt"); + assertEquals(path.extname(), ".txt"); }); Deno.test("withBasename", () => { diff --git a/src/path.ts b/src/path.ts index da054ae..59aa57a 100644 --- a/src/path.ts +++ b/src/path.ts @@ -380,7 +380,7 @@ export class PathRef { withExtname(ext: string): PathRef { const currentExt = this.extname(); const hasLeadingPeriod = ext.charCodeAt(0) === PERIOD_CHAR_CODE; - if (!hasLeadingPeriod) { + if (!hasLeadingPeriod && ext.length !== 0) { ext = "." + ext; } return new PathRef(this.#path.substring(0, this.#path.length - (currentExt?.length ?? 0)) + ext);