From 4353c6aa69db3ec90adc4a30fd54bbe1f5f0b675 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Thu, 9 Jan 2025 15:43:41 +0000 Subject: [PATCH] fix(format): allow calls with partial arguments (#198) --- src/_path.ts | 5 ++++- test/index.spec.ts | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/_path.ts b/src/_path.ts index 84eb4c9..f4be9b2 100644 --- a/src/_path.ts +++ b/src/_path.ts @@ -250,7 +250,10 @@ export const dirname: typeof path.dirname = function (p) { }; export const format: typeof path.format = function (p) { - const segments = [p.root, p.dir, p.base ?? p.name + p.ext].filter(Boolean); + const ext = p.ext ? (p.ext.startsWith(".") ? p.ext : `.${p.ext}`) : ""; + const segments = [p.root, p.dir, p.base ?? (p.name ?? "") + ext].filter( + Boolean, + ) as string[]; return normalizeWindowsPath( p.root ? resolve(...segments) : segments.join("/"), ); diff --git a/test/index.spec.ts b/test/index.spec.ts index 4dca383..9c1f798 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -153,6 +153,10 @@ runTest("format", format, [ [{ root: "/", base: "file.txt", ext: "ignored" }, "/file.txt"], [{ root: "/", name: "file", ext: ".txt" }, "/file.txt"], [{ name: "file", ext: ".txt" }, "file.txt"], + [{ name: "file" }, "file"], + [{ ext: "ext" }, ".ext"], + [{ ext: ".ext" }, ".ext"], + [{ dir: "/foo", name: "file" }, "/foo/file"], // Windows [{ name: "file", base: "file.txt" }, "file.txt"],