Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/utils/normalize-path.ts
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down
43 changes: 43 additions & 0 deletions tests/utils/normalize-path.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, expect, it } from "bun:test"
import { normalizePath } from "../../lib/utils/normalize-path"

describe("normalizePath", () => {
it("collapses multiple internal repeated-slash groups", () => {
expect(normalizePath("a///b///c.txt")).toBe("a/b/c.txt")
})

it("collapses every repeated-slash group", () => {
expect(normalizePath("a//b//c//d")).toBe("a/b/c/d")
})

it("handles repeated leading and trailing slashes", () => {
expect(normalizePath("///a///b///")).toBe("a/b")
})

it("normalizes three repeated-slash groups", () => {
expect(normalizePath("a////b//c///d")).toBe("a/b/c/d")
})

it("is idempotent", () => {
const once = normalizePath("a///b///c.txt")
expect(normalizePath(once)).toBe(once)
})

it("keeps ordinary paths unchanged", () => {
expect(normalizePath("a/b/c.txt")).toBe("a/b/c.txt")
})

it("handles empty and root paths", () => {
expect(normalizePath("")).toBe("")
expect(normalizePath("/")).toBe("")
expect(normalizePath("////")).toBe("")
})

it("normalizes Windows-style separators", () => {
expect(normalizePath(String.raw`a\b\c.txt`)).toBe("a/b/c.txt")
})

it("removes a single leading and trailing slash", () => {
expect(normalizePath("/a/b/")).toBe("a/b")
})
})
Loading