diff --git a/lib/path.js b/lib/path.js index 001152866ffc35..019843f3eb48fe 100644 --- a/lib/path.js +++ b/lib/path.js @@ -788,7 +788,9 @@ const win32 = { } } } else if (code === 47/*/*/ || code === 92/*\*/) { - return path[0]; + // `path` contains just a path separator, exit early to avoid + // unnecessary work + return path; } for (var i = len - 1; i >= offset; --i) { @@ -1040,7 +1042,7 @@ const win32 = { if (len === 3) { // `path` contains just a drive root, exit early to avoid // unnecessary work - ret.root = ret.dir = path.slice(0, 3); + ret.root = ret.dir = path; return ret; } isAbsolute = true; @@ -1049,7 +1051,7 @@ const win32 = { } else { // `path` contains just a drive root, exit early to avoid // unnecessary work - ret.root = ret.dir = path.slice(0, 2); + ret.root = ret.dir = path; return ret; } } @@ -1057,7 +1059,7 @@ const win32 = { } else if (code === 47/*/*/ || code === 92/*\*/) { // `path` contains just a path separator, exit early to avoid // unnecessary work - ret.root = ret.dir = path[0]; + ret.root = ret.dir = path; return ret; } diff --git a/test/parallel/test-path-parse-format.js b/test/parallel/test-path-parse-format.js index 5d78a6f52643ad..0f70d8ae4e7a23 100644 --- a/test/parallel/test-path-parse-format.js +++ b/test/parallel/test-path-parse-format.js @@ -32,6 +32,8 @@ const winPaths = [ 'file', '.\\file', 'C:\\', + 'C:', + '\\', '', // unc @@ -42,7 +44,9 @@ const winPaths = [ ]; const winSpecialCaseParseTests = [ - ['/foo/bar', { root: '/' }] + ['/foo/bar', { root: '/' }], + ['C:', { root: 'C:', dir: 'C:', base: '' }], + ['C:\\', { root: 'C:\\', dir: 'C:\\', base: '' }] ]; const winSpecialCaseFormatTests = [