Skip to content

Commit

Permalink
fix: PathRef#withExtname should allow setting no extension (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Dec 17, 2023
1 parent a41ffe6 commit 0c7dc79
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 0c7dc79

Please sign in to comment.