Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(join)!: preserve normalized unc paths #196

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix(join)!: preserve normalized unc paths
  • Loading branch information
pi0 committed Jan 3, 2025
commit 4bf450758521bdfb296ccd2533cbfc19617e6592
30 changes: 16 additions & 14 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,28 @@ export const normalize: typeof path.normalize = function (path: string) {
return isPathAbsolute && !isAbsolute(path) ? `/${path}` : path;
};

export const join: typeof path.join = function (...arguments_) {
if (arguments_.length === 0) {
return ".";
}
export const join: typeof path.join = function (...segments) {
let path = "";

let joined: string;
for (const argument of arguments_) {
if (argument && argument.length > 0) {
if (joined === undefined) {
joined = argument;
for (const seg of segments) {
if (!seg) {
continue;
}
if (path.length > 0) {
const pathTrailing = path[path.length - 1] === "/";
const segLeading = seg[0] === "/";
const both = pathTrailing && segLeading;
if (both) {
path += seg.slice(1);
} else {
joined += `/${argument}`;
path += pathTrailing || segLeading ? seg : `/${seg}`;
}
} else {
path += seg;
}
}
if (joined === undefined) {
return ".";
}

return normalize(joined.replace(/\/\/+/g, "/"));
return normalize(path);
};

function cwd() {
Expand Down
4 changes: 4 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ runTest("format", format, [
runTest("join", join, [
["."],
[undefined, "."],
["", "."],
["./", "./"],
["", "/foo", "/foo"],
["/", "/path", "/path"],
["/test//", "//path", "/test/path"],
["some/nodejs/deep", "../path", "some/nodejs/path"],
Expand Down Expand Up @@ -186,6 +189,7 @@ runTest("join", join, [
[String.raw`\\server\share\file`, String.raw`..\path`, "//server/share/path"],
[String.raw`\\.\c:\temp\file`, String.raw`..\path`, "//./c:/temp/path"],
[String.raw`\\server/share/file`, "../path", "//server/share/path"],
[String.raw`//server/share/file`, "../path", "//server/share/path"],
]);

runTest("normalize", normalize, {
Expand Down