Skip to content

Commit

Permalink
fix(join)!: preserve normalized unc paths (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Jan 3, 2025
1 parent 0888daf commit b91e344
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,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
5 changes: 5 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ runTest("format", format, [
runTest("join", join, [
["."],
[undefined, "."],
["", "."],
["./", "./"],
["", "/foo", "/foo"],
["/foo", "//bar", "/foo/bar"],
["/", "/path", "/path"],
["/test//", "//path", "/test/path"],
["some/nodejs/deep", "../path", "some/nodejs/path"],
Expand Down Expand Up @@ -187,6 +191,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

0 comments on commit b91e344

Please sign in to comment.