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);