Skip to content
Merged
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
4 changes: 3 additions & 1 deletion src/pwd.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,7 @@ export function locate(cwd = process.cwd()) {

// Collapse $HOME to `~` for tidy display.
export function tilde(p, home) {
return home && p.startsWith(home) ? "~" + p.slice(home.length) : p;
if (!home || p === home) return home ? "~" : p;
const relative = p.slice(home.length);
return p.startsWith(home) && relative.startsWith(path.sep) ? "~" + relative : p;
}
25 changes: 25 additions & 0 deletions test/pwd.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import assert from "node:assert/strict";
import path from "node:path";
import test from "node:test";

import { tilde } from "../src/pwd.mjs";

test("tilde shortens the home directory itself", () => {
const home = path.join("C:", "Users", "mosh");

assert.equal(tilde(home, home), "~");
});

test("tilde shortens paths inside home", () => {
const home = path.join("C:", "Users", "mosh");
const project = path.join(home, "repo");

assert.equal(tilde(project, home), `~${path.sep}repo`);
});

test("tilde does not shorten sibling paths with the same prefix", () => {
const home = path.join("C:", "Users", "mosh");
const sibling = path.join("C:", "Users", "mosh-other", "repo");

assert.equal(tilde(sibling, home), sibling);
});
Loading