path: refactor for less indentation
This moves the `if (len === 1)` case to the top of the function. That way it is possible to reduce the indentation level due to returning early in that case. On top of that the following was done: 1) For clarity refactored for loops which were meant to count up a variable into a while loop. 2) Used template strings instead of string concat. 3) Consolidating nested if statements. 4) Using tenary expressions if applicable when assigning variables to reduce the code overhead. PR-URL: https://github.com/nodejs/node/pull/25278 Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
parent
e68b0d6fb3
commit
7cbe29eb4d
317
lib/path.js
317
lib/path.js
@ -270,77 +270,67 @@ const win32 = {
|
|||||||
const code = path.charCodeAt(0);
|
const code = path.charCodeAt(0);
|
||||||
|
|
||||||
// Try to match a root
|
// Try to match a root
|
||||||
if (len > 1) {
|
if (len === 1) {
|
||||||
if (isPathSeparator(code)) {
|
// `path` contains just a single char, exit early to avoid
|
||||||
// Possible UNC root
|
// unnecessary work
|
||||||
|
return isPosixPathSeparator(code) ? '\\' : path;
|
||||||
|
}
|
||||||
|
if (isPathSeparator(code)) {
|
||||||
|
// Possible UNC root
|
||||||
|
|
||||||
// If we started with a separator, we know we at least have an absolute
|
// If we started with a separator, we know we at least have an absolute
|
||||||
// path of some kind (UNC or otherwise)
|
// path of some kind (UNC or otherwise)
|
||||||
isAbsolute = true;
|
isAbsolute = true;
|
||||||
|
|
||||||
if (isPathSeparator(path.charCodeAt(1))) {
|
if (isPathSeparator(path.charCodeAt(1))) {
|
||||||
// Matched double path separator at beginning
|
// Matched double path separator at beginning
|
||||||
var j = 2;
|
let j = 2;
|
||||||
var last = j;
|
let last = j;
|
||||||
// Match 1 or more non-path separators
|
// Match 1 or more non-path separators
|
||||||
for (; j < len; ++j) {
|
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
|
||||||
if (isPathSeparator(path.charCodeAt(j)))
|
j++;
|
||||||
break;
|
}
|
||||||
|
if (j < len && j !== last) {
|
||||||
|
const firstPart = path.slice(last, j);
|
||||||
|
// Matched!
|
||||||
|
last = j;
|
||||||
|
// Match 1 or more path separators
|
||||||
|
while (j < len && isPathSeparator(path.charCodeAt(j))) {
|
||||||
|
j++;
|
||||||
}
|
}
|
||||||
if (j < len && j !== last) {
|
if (j < len && j !== last) {
|
||||||
const firstPart = path.slice(last, j);
|
|
||||||
// Matched!
|
// Matched!
|
||||||
last = j;
|
last = j;
|
||||||
// Match 1 or more path separators
|
// Match 1 or more non-path separators
|
||||||
for (; j < len; ++j) {
|
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
|
||||||
if (!isPathSeparator(path.charCodeAt(j)))
|
j++;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
if (j < len && j !== last) {
|
if (j === len) {
|
||||||
// Matched!
|
// We matched a UNC root only
|
||||||
last = j;
|
// Return the normalized version of the UNC root since there
|
||||||
// Match 1 or more non-path separators
|
// is nothing left to process
|
||||||
for (; j < len; ++j) {
|
return `\\\\${firstPart}\\${path.slice(last)}\\`;
|
||||||
if (isPathSeparator(path.charCodeAt(j)))
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (j === len) {
|
|
||||||
// We matched a UNC root only
|
|
||||||
// Return the normalized version of the UNC root since there
|
|
||||||
// is nothing left to process
|
|
||||||
|
|
||||||
return '\\\\' + firstPart + '\\' + path.slice(last) + '\\';
|
|
||||||
} else if (j !== last) {
|
|
||||||
// We matched a UNC root with leftovers
|
|
||||||
|
|
||||||
device = '\\\\' + firstPart + '\\' + path.slice(last, j);
|
|
||||||
rootEnd = j;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
if (j !== last) {
|
||||||
} else {
|
// We matched a UNC root with leftovers
|
||||||
rootEnd = 1;
|
device = `\\\\${firstPart}\\${path.slice(last, j)}`;
|
||||||
}
|
rootEnd = j;
|
||||||
} else if (isWindowsDeviceRoot(code)) {
|
|
||||||
// Possible device root
|
|
||||||
|
|
||||||
if (path.charCodeAt(1) === CHAR_COLON) {
|
|
||||||
device = path.slice(0, 2);
|
|
||||||
rootEnd = 2;
|
|
||||||
if (len > 2) {
|
|
||||||
if (isPathSeparator(path.charCodeAt(2))) {
|
|
||||||
// Treat separator following drive name as an absolute path
|
|
||||||
// indicator
|
|
||||||
isAbsolute = true;
|
|
||||||
rootEnd = 3;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
rootEnd = 1;
|
||||||
|
}
|
||||||
|
} else if (isWindowsDeviceRoot(code) && path.charCodeAt(1) === CHAR_COLON) {
|
||||||
|
// Possible device root
|
||||||
|
device = path.slice(0, 2);
|
||||||
|
rootEnd = 2;
|
||||||
|
if (len > 2 && isPathSeparator(path.charCodeAt(2))) {
|
||||||
|
// Treat separator following drive name as an absolute path
|
||||||
|
// indicator
|
||||||
|
isAbsolute = true;
|
||||||
|
rootEnd = 3;
|
||||||
}
|
}
|
||||||
} else if (isPathSeparator(code)) {
|
|
||||||
// `path` contains just a path separator, exit early to avoid unnecessary
|
|
||||||
// work
|
|
||||||
return '\\';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let tail = rootEnd < len ?
|
let tail = rootEnd < len ?
|
||||||
@ -592,75 +582,66 @@ const win32 = {
|
|||||||
const len = path.length;
|
const len = path.length;
|
||||||
if (len === 0)
|
if (len === 0)
|
||||||
return '.';
|
return '.';
|
||||||
var rootEnd = -1;
|
let rootEnd = -1;
|
||||||
var end = -1;
|
let offset = 0;
|
||||||
var matchedSlash = true;
|
|
||||||
var offset = 0;
|
|
||||||
const code = path.charCodeAt(0);
|
const code = path.charCodeAt(0);
|
||||||
|
|
||||||
|
if (len === 1) {
|
||||||
|
// `path` contains just a path separator, exit early to avoid
|
||||||
|
// unnecessary work or a dot.
|
||||||
|
return isPathSeparator(code) ? path : '.';
|
||||||
|
}
|
||||||
|
|
||||||
// Try to match a root
|
// Try to match a root
|
||||||
if (len > 1) {
|
if (isPathSeparator(code)) {
|
||||||
if (isPathSeparator(code)) {
|
// Possible UNC root
|
||||||
// Possible UNC root
|
|
||||||
|
|
||||||
rootEnd = offset = 1;
|
rootEnd = offset = 1;
|
||||||
|
|
||||||
if (isPathSeparator(path.charCodeAt(1))) {
|
if (isPathSeparator(path.charCodeAt(1))) {
|
||||||
// Matched double path separator at beginning
|
// Matched double path separator at beginning
|
||||||
var j = 2;
|
let j = 2;
|
||||||
var last = j;
|
let last = j;
|
||||||
// Match 1 or more non-path separators
|
// Match 1 or more non-path separators
|
||||||
for (; j < len; ++j) {
|
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
|
||||||
if (isPathSeparator(path.charCodeAt(j)))
|
j++;
|
||||||
break;
|
}
|
||||||
|
if (j < len && j !== last) {
|
||||||
|
// Matched!
|
||||||
|
last = j;
|
||||||
|
// Match 1 or more path separators
|
||||||
|
while (j < len && isPathSeparator(path.charCodeAt(j))) {
|
||||||
|
j++;
|
||||||
}
|
}
|
||||||
if (j < len && j !== last) {
|
if (j < len && j !== last) {
|
||||||
// Matched!
|
// Matched!
|
||||||
last = j;
|
last = j;
|
||||||
// Match 1 or more path separators
|
// Match 1 or more non-path separators
|
||||||
for (; j < len; ++j) {
|
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
|
||||||
if (!isPathSeparator(path.charCodeAt(j)))
|
j++;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
if (j < len && j !== last) {
|
if (j === len) {
|
||||||
// Matched!
|
// We matched a UNC root only
|
||||||
last = j;
|
return path;
|
||||||
// Match 1 or more non-path separators
|
|
||||||
for (; j < len; ++j) {
|
|
||||||
if (isPathSeparator(path.charCodeAt(j)))
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (j === len) {
|
|
||||||
// We matched a UNC root only
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
if (j !== last) {
|
|
||||||
// We matched a UNC root with leftovers
|
|
||||||
|
|
||||||
// Offset by 1 to include the separator after the UNC root to
|
|
||||||
// treat it as a "normal root" on top of a (UNC) root
|
|
||||||
rootEnd = offset = j + 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
if (j !== last) {
|
||||||
}
|
// We matched a UNC root with leftovers
|
||||||
} else if (isWindowsDeviceRoot(code)) {
|
|
||||||
// Possible device root
|
|
||||||
|
|
||||||
if (path.charCodeAt(1) === CHAR_COLON) {
|
// Offset by 1 to include the separator after the UNC root to
|
||||||
rootEnd = offset = 2;
|
// treat it as a "normal root" on top of a (UNC) root
|
||||||
if (len > 2) {
|
rootEnd = offset = j + 1;
|
||||||
if (isPathSeparator(path.charCodeAt(2)))
|
}
|
||||||
rootEnd = offset = 3;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (isPathSeparator(code)) {
|
// Possible device root
|
||||||
// `path` contains just a path separator, exit early to avoid
|
} else if (isWindowsDeviceRoot(code) && path.charCodeAt(1) === CHAR_COLON) {
|
||||||
// unnecessary work
|
rootEnd = len > 2 && isPathSeparator(path.charCodeAt(2)) ? 3 : 2;
|
||||||
return path;
|
offset = rootEnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let end = -1;
|
||||||
|
let matchedSlash = true;
|
||||||
for (var i = len - 1; i >= offset; --i) {
|
for (var i = len - 1; i >= offset; --i) {
|
||||||
if (isPathSeparator(path.charCodeAt(i))) {
|
if (isPathSeparator(path.charCodeAt(i))) {
|
||||||
if (!matchedSlash) {
|
if (!matchedSlash) {
|
||||||
@ -843,79 +824,71 @@ const win32 = {
|
|||||||
var rootEnd = 0;
|
var rootEnd = 0;
|
||||||
let code = path.charCodeAt(0);
|
let code = path.charCodeAt(0);
|
||||||
|
|
||||||
// Try to match a root
|
if (len === 1) {
|
||||||
if (len > 1) {
|
|
||||||
if (isPathSeparator(code)) {
|
if (isPathSeparator(code)) {
|
||||||
// Possible UNC root
|
// `path` contains just a path separator, exit early to avoid
|
||||||
|
// unnecessary work
|
||||||
|
ret.root = ret.dir = path;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
// Try to match a root
|
||||||
|
if (isPathSeparator(code)) {
|
||||||
|
// Possible UNC root
|
||||||
|
|
||||||
rootEnd = 1;
|
rootEnd = 1;
|
||||||
if (isPathSeparator(path.charCodeAt(1))) {
|
if (isPathSeparator(path.charCodeAt(1))) {
|
||||||
// Matched double path separator at beginning
|
// Matched double path separator at beginning
|
||||||
var j = 2;
|
let j = 2;
|
||||||
var last = j;
|
let last = j;
|
||||||
// Match 1 or more non-path separators
|
// Match 1 or more non-path separators
|
||||||
for (; j < len; ++j) {
|
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
|
||||||
if (isPathSeparator(path.charCodeAt(j)))
|
j++;
|
||||||
break;
|
}
|
||||||
|
if (j < len && j !== last) {
|
||||||
|
// Matched!
|
||||||
|
last = j;
|
||||||
|
// Match 1 or more path separators
|
||||||
|
while (j < len && isPathSeparator(path.charCodeAt(j))) {
|
||||||
|
j++;
|
||||||
}
|
}
|
||||||
if (j < len && j !== last) {
|
if (j < len && j !== last) {
|
||||||
// Matched!
|
// Matched!
|
||||||
last = j;
|
last = j;
|
||||||
// Match 1 or more path separators
|
// Match 1 or more non-path separators
|
||||||
for (; j < len; ++j) {
|
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
|
||||||
if (!isPathSeparator(path.charCodeAt(j)))
|
j++;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
if (j < len && j !== last) {
|
if (j === len) {
|
||||||
// Matched!
|
// We matched a UNC root only
|
||||||
last = j;
|
rootEnd = j;
|
||||||
// Match 1 or more non-path separators
|
} else if (j !== last) {
|
||||||
for (; j < len; ++j) {
|
// We matched a UNC root with leftovers
|
||||||
if (isPathSeparator(path.charCodeAt(j)))
|
rootEnd = j + 1;
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (j === len) {
|
|
||||||
// We matched a UNC root only
|
|
||||||
|
|
||||||
rootEnd = j;
|
|
||||||
} else if (j !== last) {
|
|
||||||
// We matched a UNC root with leftovers
|
|
||||||
|
|
||||||
rootEnd = j + 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (isWindowsDeviceRoot(code)) {
|
|
||||||
// Possible device root
|
|
||||||
|
|
||||||
if (path.charCodeAt(1) === CHAR_COLON) {
|
|
||||||
rootEnd = 2;
|
|
||||||
if (len > 2) {
|
|
||||||
if (isPathSeparator(path.charCodeAt(2))) {
|
|
||||||
if (len === 3) {
|
|
||||||
// `path` contains just a drive root, exit early to avoid
|
|
||||||
// unnecessary work
|
|
||||||
ret.root = ret.dir = path;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
rootEnd = 3;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// `path` contains just a drive root, exit early to avoid
|
|
||||||
// unnecessary work
|
|
||||||
ret.root = ret.dir = path;
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (isPathSeparator(code)) {
|
} else if (isWindowsDeviceRoot(code) && path.charCodeAt(1) === CHAR_COLON) {
|
||||||
// `path` contains just a path separator, exit early to avoid
|
// Possible device root
|
||||||
// unnecessary work
|
if (len <= 2) {
|
||||||
ret.root = ret.dir = path;
|
// `path` contains just a drive root, exit early to avoid
|
||||||
return ret;
|
// unnecessary work
|
||||||
|
ret.root = ret.dir = path;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
rootEnd = 2;
|
||||||
|
if (isPathSeparator(path.charCodeAt(2))) {
|
||||||
|
if (len === 3) {
|
||||||
|
// `path` contains just a drive root, exit early to avoid
|
||||||
|
// unnecessary work
|
||||||
|
ret.root = ret.dir = path;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
rootEnd = 3;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rootEnd > 0)
|
if (rootEnd > 0)
|
||||||
ret.root = path.slice(0, rootEnd);
|
ret.root = path.slice(0, rootEnd);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user