Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mceachen committed Jan 9, 2025
1 parent a08cf0a commit edc1a86
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe("mount_point", () => {
});

it("handles multiple trailing slashes", () => {
expect(normalizePosixPath("//")).toBe("/");
expect(normalizePosixPath("/home//")).toBe("/home");
expect(normalizePosixPath("/usr////")).toBe("/usr");
});
Expand Down
17 changes: 12 additions & 5 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ export function normalizePath(
export function normalizePosixPath(
mountPoint: string | undefined,
): string | undefined {
return isBlank(mountPoint)
? undefined
: mountPoint === "/"
? mountPoint
: mountPoint.replace(/\/+$/, "");
if (isBlank(mountPoint)) return undefined;
if (mountPoint === "/") return mountPoint;

// Fast path: check last char only if no trailing slash
if (mountPoint[mountPoint.length - 1] !== "/") return mountPoint;

// Slower path: trim trailing slashes
let end = mountPoint.length - 1;
while (end > 0 && mountPoint[end] === "/") {
end--;
}
return mountPoint.slice(0, end + 1);
}

/**
Expand Down

0 comments on commit edc1a86

Please sign in to comment.