diff --git a/lib/utils/normalize-path.ts b/lib/utils/normalize-path.ts index debc468..ab05b07 100644 --- a/lib/utils/normalize-path.ts +++ b/lib/utils/normalize-path.ts @@ -1,6 +1,6 @@ export function normalizePath(path: string): string { if (!path || path === "/") return "" - let normalized = path.replace(/\\+/g, "/").replace(/\/\/+/, "/") + let normalized = path.replace(/\\+/g, "/").replace(/\/+/g, "/") if (normalized.startsWith("/")) { normalized = normalized.slice(1) } diff --git a/tests/normalize-path.test.ts b/tests/normalize-path.test.ts new file mode 100644 index 0000000..920416d --- /dev/null +++ b/tests/normalize-path.test.ts @@ -0,0 +1,27 @@ +import { expect, test, describe } from "bun:test" +import { normalizePath } from "../lib/utils/normalize-path" + +describe("normalizePath", () => { + test("collapses multiple repeated slashes", () => { + expect(normalizePath("a///b///c.txt")).toBe("a/b/c.txt") + expect(normalizePath("a//b//c//d")).toBe("a/b/c/d") + }) + + test("is idempotent across multiple calls", () => { + const raw = "a///b//c////d//e.txt" + const pass1 = normalizePath(raw) + const pass2 = normalizePath(pass1) + expect(pass1).toBe(pass2) + expect(pass1).toBe("a/b/c/d/e.txt") + }) + + test("handles mixed and windows slashes", () => { + expect(normalizePath("a\\\\b\\\\c\\d")).toBe("a/b/c/d") + expect(normalizePath("a\\\\//b//\\c")).toBe("a/b/c") + }) + + test("handles empty and root", () => { + expect(normalizePath("")).toBe("") + expect(normalizePath("/")).toBe("") + }) +})