path: minor refactoring
1) Consolidate nested if statements if possible `if (foo) { if bar () { /* do stuff */ } }`) to reduce indentation depth. 2) Remove obsolete else cases to reduce indentation. PR-URL: https://github.com/nodejs/node/pull/25278 Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
parent
a019d7bd0b
commit
f59fdd9d65
133
lib/path.js
133
lib/path.js
@ -206,20 +206,16 @@ const win32 = {
|
|||||||
} else {
|
} else {
|
||||||
rootEnd = 1;
|
rootEnd = 1;
|
||||||
}
|
}
|
||||||
} else if (isWindowsDeviceRoot(code)) {
|
} else if (isWindowsDeviceRoot(code) &&
|
||||||
|
path.charCodeAt(1) === CHAR_COLON) {
|
||||||
// Possible device root
|
// Possible device root
|
||||||
|
device = path.slice(0, 2);
|
||||||
if (path.charCodeAt(1) === CHAR_COLON) {
|
rootEnd = 2;
|
||||||
device = path.slice(0, 2);
|
if (len > 2 && isPathSeparator(path.charCodeAt(2))) {
|
||||||
rootEnd = 2;
|
// Treat separator following drive name as an absolute path
|
||||||
if (len > 2) {
|
// indicator
|
||||||
if (isPathSeparator(path.charCodeAt(2))) {
|
isAbsolute = true;
|
||||||
// Treat separator following drive name as an absolute path
|
rootEnd = 3;
|
||||||
// indicator
|
|
||||||
isAbsolute = true;
|
|
||||||
rootEnd = 3;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (isPathSeparator(code)) {
|
} else if (isPathSeparator(code)) {
|
||||||
@ -567,26 +563,23 @@ const win32 = {
|
|||||||
|
|
||||||
const resolvedPath = win32.resolve(path);
|
const resolvedPath = win32.resolve(path);
|
||||||
|
|
||||||
if (resolvedPath.length >= 3) {
|
if (resolvedPath.length <= 2)
|
||||||
if (resolvedPath.charCodeAt(0) === CHAR_BACKWARD_SLASH) {
|
return path;
|
||||||
// Possible UNC root
|
|
||||||
|
|
||||||
if (resolvedPath.charCodeAt(1) === CHAR_BACKWARD_SLASH) {
|
if (resolvedPath.charCodeAt(0) === CHAR_BACKWARD_SLASH) {
|
||||||
const code = resolvedPath.charCodeAt(2);
|
// Possible UNC root
|
||||||
if (code !== CHAR_QUESTION_MARK && code !== CHAR_DOT) {
|
if (resolvedPath.charCodeAt(1) === CHAR_BACKWARD_SLASH) {
|
||||||
// Matched non-long UNC root, convert the path to a long UNC path
|
const code = resolvedPath.charCodeAt(2);
|
||||||
return '\\\\?\\UNC\\' + resolvedPath.slice(2);
|
if (code !== CHAR_QUESTION_MARK && code !== CHAR_DOT) {
|
||||||
}
|
// Matched non-long UNC root, convert the path to a long UNC path
|
||||||
}
|
return `\\\\?\\UNC\\${resolvedPath.slice(2)}`;
|
||||||
} else if (isWindowsDeviceRoot(resolvedPath.charCodeAt(0))) {
|
|
||||||
// Possible device root
|
|
||||||
|
|
||||||
if (resolvedPath.charCodeAt(1) === CHAR_COLON &&
|
|
||||||
resolvedPath.charCodeAt(2) === CHAR_BACKWARD_SLASH) {
|
|
||||||
// Matched device root, convert the path to a long UNC path
|
|
||||||
return '\\\\?\\' + resolvedPath;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (isWindowsDeviceRoot(resolvedPath.charCodeAt(0)) &&
|
||||||
|
resolvedPath.charCodeAt(1) === CHAR_COLON &&
|
||||||
|
resolvedPath.charCodeAt(2) === CHAR_BACKWARD_SLASH) {
|
||||||
|
// Matched device root, convert the path to a long UNC path
|
||||||
|
return `\\\\?\\${resolvedPath}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return path;
|
return path;
|
||||||
@ -749,29 +742,27 @@ const win32 = {
|
|||||||
else if (end === -1)
|
else if (end === -1)
|
||||||
end = path.length;
|
end = path.length;
|
||||||
return path.slice(start, end);
|
return path.slice(start, end);
|
||||||
} else {
|
|
||||||
for (i = path.length - 1; i >= start; --i) {
|
|
||||||
if (isPathSeparator(path.charCodeAt(i))) {
|
|
||||||
// If we reached a path separator that was not part of a set of path
|
|
||||||
// separators at the end of the string, stop now
|
|
||||||
if (!matchedSlash) {
|
|
||||||
start = i + 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else if (end === -1) {
|
|
||||||
// We saw the first non-path separator, mark this as the end of our
|
|
||||||
// path component
|
|
||||||
matchedSlash = false;
|
|
||||||
end = i + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (end === -1)
|
|
||||||
return '';
|
|
||||||
return path.slice(start, end);
|
|
||||||
}
|
}
|
||||||
},
|
for (i = path.length - 1; i >= start; --i) {
|
||||||
|
if (isPathSeparator(path.charCodeAt(i))) {
|
||||||
|
// If we reached a path separator that was not part of a set of path
|
||||||
|
// separators at the end of the string, stop now
|
||||||
|
if (!matchedSlash) {
|
||||||
|
start = i + 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (end === -1) {
|
||||||
|
// We saw the first non-path separator, mark this as the end of our
|
||||||
|
// path component
|
||||||
|
matchedSlash = false;
|
||||||
|
end = i + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end === -1)
|
||||||
|
return '';
|
||||||
|
return path.slice(start, end);
|
||||||
|
},
|
||||||
|
|
||||||
extname(path) {
|
extname(path) {
|
||||||
validateString(path, 'path');
|
validateString(path, 'path');
|
||||||
@ -1256,29 +1247,27 @@ const posix = {
|
|||||||
else if (end === -1)
|
else if (end === -1)
|
||||||
end = path.length;
|
end = path.length;
|
||||||
return path.slice(start, end);
|
return path.slice(start, end);
|
||||||
} else {
|
|
||||||
for (i = path.length - 1; i >= 0; --i) {
|
|
||||||
if (path.charCodeAt(i) === CHAR_FORWARD_SLASH) {
|
|
||||||
// If we reached a path separator that was not part of a set of path
|
|
||||||
// separators at the end of the string, stop now
|
|
||||||
if (!matchedSlash) {
|
|
||||||
start = i + 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else if (end === -1) {
|
|
||||||
// We saw the first non-path separator, mark this as the end of our
|
|
||||||
// path component
|
|
||||||
matchedSlash = false;
|
|
||||||
end = i + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (end === -1)
|
|
||||||
return '';
|
|
||||||
return path.slice(start, end);
|
|
||||||
}
|
}
|
||||||
},
|
for (i = path.length - 1; i >= 0; --i) {
|
||||||
|
if (path.charCodeAt(i) === CHAR_FORWARD_SLASH) {
|
||||||
|
// If we reached a path separator that was not part of a set of path
|
||||||
|
// separators at the end of the string, stop now
|
||||||
|
if (!matchedSlash) {
|
||||||
|
start = i + 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else if (end === -1) {
|
||||||
|
// We saw the first non-path separator, mark this as the end of our
|
||||||
|
// path component
|
||||||
|
matchedSlash = false;
|
||||||
|
end = i + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end === -1)
|
||||||
|
return '';
|
||||||
|
return path.slice(start, end);
|
||||||
|
},
|
||||||
|
|
||||||
extname(path) {
|
extname(path) {
|
||||||
validateString(path, 'path');
|
validateString(path, 'path');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user