path: more small refactorings
1) Refactor for loops to while loops that were only meant to count up a variable. 2) Refactor some `var` statements to `let` / `const`. 3) Simplify return conditions. 4) Use template strings where possible instead of concat. 5) Use ternary expressions for variable assignments instead of if / else. 6) Use the object shorthand notation for the function declarations. 7) Consolidate if else case where possible. 8) Remove double line breaks. PR-URL: https://github.com/nodejs/node/pull/25278 Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
parent
05a8dbc91b
commit
9d19b1f8cf
227
lib/path.js
227
lib/path.js
@ -143,9 +143,9 @@ const win32 = {
|
||||
// Verify that a cwd was found and that it actually points
|
||||
// to our drive. If not, default to the drive's root.
|
||||
if (path === undefined ||
|
||||
path.slice(0, 3).toLowerCase() !==
|
||||
resolvedDevice.toLowerCase() + '\\') {
|
||||
path = resolvedDevice + '\\';
|
||||
path.slice(0, 2).toLowerCase() !== resolvedDevice.toLowerCase() &&
|
||||
path.charCodeAt(2) === CHAR_BACKWARD_SLASH) {
|
||||
path = `${resolvedDevice}\\`;
|
||||
}
|
||||
}
|
||||
|
||||
@ -173,39 +173,30 @@ const win32 = {
|
||||
|
||||
if (isPathSeparator(path.charCodeAt(1))) {
|
||||
// Matched double path separator at beginning
|
||||
var j = 2;
|
||||
var last = j;
|
||||
let j = 2;
|
||||
let last = j;
|
||||
// Match 1 or more non-path separators
|
||||
for (; j < len; ++j) {
|
||||
if (isPathSeparator(path.charCodeAt(j)))
|
||||
break;
|
||||
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
|
||||
j++;
|
||||
}
|
||||
if (j < len && j !== last) {
|
||||
const firstPart = path.slice(last, j);
|
||||
// Matched!
|
||||
last = j;
|
||||
// Match 1 or more path separators
|
||||
for (; j < len; ++j) {
|
||||
if (!isPathSeparator(path.charCodeAt(j)))
|
||||
break;
|
||||
while (j < len && isPathSeparator(path.charCodeAt(j))) {
|
||||
j++;
|
||||
}
|
||||
if (j < len && j !== last) {
|
||||
// Matched!
|
||||
last = j;
|
||||
// Match 1 or more non-path separators
|
||||
for (; j < len; ++j) {
|
||||
if (isPathSeparator(path.charCodeAt(j)))
|
||||
break;
|
||||
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
|
||||
j++;
|
||||
}
|
||||
if (j === len) {
|
||||
// We matched a UNC root only
|
||||
|
||||
device = '\\\\' + firstPart + '\\' + path.slice(last);
|
||||
rootEnd = j;
|
||||
} else if (j !== last) {
|
||||
// We matched a UNC root with leftovers
|
||||
|
||||
device = '\\\\' + firstPart + '\\' + path.slice(last, j);
|
||||
if (j === len || j !== last) {
|
||||
// We matched a UNC root
|
||||
device = `\\\\${firstPart}\\${path.slice(last, j)}`;
|
||||
rootEnd = j;
|
||||
}
|
||||
}
|
||||
@ -372,28 +363,22 @@ const win32 = {
|
||||
return false;
|
||||
|
||||
const code = path.charCodeAt(0);
|
||||
if (isPathSeparator(code)) {
|
||||
return true;
|
||||
} else if (isWindowsDeviceRoot(code)) {
|
||||
return isPathSeparator(code) ||
|
||||
// Possible device root
|
||||
|
||||
if (len > 2 && path.charCodeAt(1) === CHAR_COLON) {
|
||||
if (isPathSeparator(path.charCodeAt(2)))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
len > 2 &&
|
||||
isWindowsDeviceRoot(code) &&
|
||||
path.charCodeAt(1) === CHAR_COLON &&
|
||||
isPathSeparator(path.charCodeAt(2));
|
||||
},
|
||||
|
||||
|
||||
join: function join() {
|
||||
if (arguments.length === 0)
|
||||
join(...args) {
|
||||
if (args.length === 0)
|
||||
return '.';
|
||||
|
||||
var joined;
|
||||
var firstPart;
|
||||
for (var i = 0; i < arguments.length; ++i) {
|
||||
var arg = arguments[i];
|
||||
let joined;
|
||||
let firstPart;
|
||||
for (var i = 0; i < args.length; ++i) {
|
||||
const arg = args[i];
|
||||
validateString(arg, 'path');
|
||||
if (arg.length > 0) {
|
||||
if (joined === undefined)
|
||||
@ -440,9 +425,9 @@ const win32 = {
|
||||
}
|
||||
if (needsReplace) {
|
||||
// Find any more consecutive slashes we need to replace
|
||||
for (; slashCount < joined.length; ++slashCount) {
|
||||
if (!isPathSeparator(joined.charCodeAt(slashCount)))
|
||||
break;
|
||||
while (slashCount < joined.length &&
|
||||
isPathSeparator(joined.charCodeAt(slashCount))) {
|
||||
slashCount++;
|
||||
}
|
||||
|
||||
// Replace the slashes if needed
|
||||
@ -477,30 +462,30 @@ const win32 = {
|
||||
return '';
|
||||
|
||||
// Trim any leading backslashes
|
||||
var fromStart = 0;
|
||||
for (; fromStart < from.length; ++fromStart) {
|
||||
if (from.charCodeAt(fromStart) !== CHAR_BACKWARD_SLASH)
|
||||
break;
|
||||
let fromStart = 0;
|
||||
while (fromStart < from.length &&
|
||||
from.charCodeAt(fromStart) === CHAR_BACKWARD_SLASH) {
|
||||
fromStart++;
|
||||
}
|
||||
// Trim trailing backslashes (applicable to UNC paths only)
|
||||
var fromEnd = from.length;
|
||||
for (; fromEnd - 1 > fromStart; --fromEnd) {
|
||||
if (from.charCodeAt(fromEnd - 1) !== CHAR_BACKWARD_SLASH)
|
||||
break;
|
||||
let fromEnd = from.length;
|
||||
while (fromEnd - 1 > fromStart &&
|
||||
from.charCodeAt(fromEnd - 1) === CHAR_BACKWARD_SLASH) {
|
||||
fromEnd--;
|
||||
}
|
||||
const fromLen = fromEnd - fromStart;
|
||||
|
||||
// Trim any leading backslashes
|
||||
var toStart = 0;
|
||||
for (; toStart < to.length; ++toStart) {
|
||||
if (to.charCodeAt(toStart) !== CHAR_BACKWARD_SLASH)
|
||||
break;
|
||||
let toStart = 0;
|
||||
while (toStart < to.length &&
|
||||
to.charCodeAt(toStart) === CHAR_BACKWARD_SLASH) {
|
||||
toStart++;
|
||||
}
|
||||
// Trim trailing backslashes (applicable to UNC paths only)
|
||||
var toEnd = to.length;
|
||||
for (; toEnd - 1 > toStart; --toEnd) {
|
||||
if (to.charCodeAt(toEnd - 1) !== CHAR_BACKWARD_SLASH)
|
||||
break;
|
||||
let toEnd = to.length;
|
||||
while (toEnd - 1 > toStart &&
|
||||
to.charCodeAt(toEnd - 1) === CHAR_BACKWARD_SLASH) {
|
||||
toEnd--;
|
||||
}
|
||||
const toLen = toEnd - toStart;
|
||||
|
||||
@ -559,18 +544,17 @@ const win32 = {
|
||||
}
|
||||
}
|
||||
|
||||
toStart += lastCommonSep;
|
||||
|
||||
// Lastly, append the rest of the destination (`to`) path that comes after
|
||||
// the common path parts
|
||||
if (out.length > 0)
|
||||
return out + toOrig.slice(toStart + lastCommonSep, toEnd);
|
||||
else {
|
||||
toStart += lastCommonSep;
|
||||
if (toOrig.charCodeAt(toStart) === CHAR_BACKWARD_SLASH)
|
||||
++toStart;
|
||||
return toOrig.slice(toStart, toEnd);
|
||||
}
|
||||
},
|
||||
return `${out}${toOrig.slice(toStart, toEnd)}`;
|
||||
|
||||
if (toOrig.charCodeAt(toStart) === CHAR_BACKWARD_SLASH)
|
||||
++toStart;
|
||||
return toOrig.slice(toStart, toEnd);
|
||||
},
|
||||
|
||||
toNamespacedPath(path) {
|
||||
// Note: this will *probably* throw somewhere.
|
||||
@ -791,7 +775,7 @@ const win32 = {
|
||||
},
|
||||
|
||||
|
||||
extname: function extname(path) {
|
||||
extname(path) {
|
||||
validateString(path, 'path');
|
||||
var start = 0;
|
||||
var startDot = -1;
|
||||
@ -1020,27 +1004,20 @@ const win32 = {
|
||||
return ret;
|
||||
},
|
||||
|
||||
|
||||
sep: '\\',
|
||||
delimiter: ';',
|
||||
win32: null,
|
||||
posix: null
|
||||
};
|
||||
|
||||
|
||||
const posix = {
|
||||
// path.resolve([from ...], to)
|
||||
resolve: function resolve() {
|
||||
var resolvedPath = '';
|
||||
var resolvedAbsolute = false;
|
||||
resolve(...args) {
|
||||
let resolvedPath = '';
|
||||
let resolvedAbsolute = false;
|
||||
|
||||
for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
|
||||
var path;
|
||||
if (i >= 0)
|
||||
path = arguments[i];
|
||||
else {
|
||||
path = process.cwd();
|
||||
}
|
||||
for (var i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
|
||||
const path = i >= 0 ? args[i] : process.cwd();
|
||||
|
||||
validateString(path, 'path');
|
||||
|
||||
@ -1049,7 +1026,7 @@ const posix = {
|
||||
continue;
|
||||
}
|
||||
|
||||
resolvedPath = path + '/' + resolvedPath;
|
||||
resolvedPath = `${path}/${resolvedPath}`;
|
||||
resolvedAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
|
||||
}
|
||||
|
||||
@ -1061,19 +1038,12 @@ const posix = {
|
||||
isPosixPathSeparator);
|
||||
|
||||
if (resolvedAbsolute) {
|
||||
if (resolvedPath.length > 0)
|
||||
return '/' + resolvedPath;
|
||||
else
|
||||
return '/';
|
||||
} else if (resolvedPath.length > 0) {
|
||||
return resolvedPath;
|
||||
} else {
|
||||
return '.';
|
||||
return `/${resolvedPath}`;
|
||||
}
|
||||
return resolvedPath.length > 0 ? resolvedPath : '.';
|
||||
},
|
||||
|
||||
|
||||
normalize: function normalize(path) {
|
||||
normalize(path) {
|
||||
validateString(path, 'path');
|
||||
|
||||
if (path.length === 0)
|
||||
@ -1091,30 +1061,26 @@ const posix = {
|
||||
if (path.length > 0 && trailingSeparator)
|
||||
path += '/';
|
||||
|
||||
if (isAbsolute)
|
||||
return '/' + path;
|
||||
return path;
|
||||
return isAbsolute ? `/${path}` : path;
|
||||
},
|
||||
|
||||
|
||||
isAbsolute: function isAbsolute(path) {
|
||||
isAbsolute(path) {
|
||||
validateString(path, 'path');
|
||||
return path.length > 0 && path.charCodeAt(0) === CHAR_FORWARD_SLASH;
|
||||
},
|
||||
|
||||
|
||||
join: function join() {
|
||||
if (arguments.length === 0)
|
||||
join(...args) {
|
||||
if (args.length === 0)
|
||||
return '.';
|
||||
var joined;
|
||||
for (var i = 0; i < arguments.length; ++i) {
|
||||
var arg = arguments[i];
|
||||
let joined;
|
||||
for (var i = 0; i < args.length; ++i) {
|
||||
const arg = args[i];
|
||||
validateString(arg, 'path');
|
||||
if (arg.length > 0) {
|
||||
if (joined === undefined)
|
||||
joined = arg;
|
||||
else
|
||||
joined += '/' + arg;
|
||||
joined += `/${arg}`;
|
||||
}
|
||||
}
|
||||
if (joined === undefined)
|
||||
@ -1122,8 +1088,7 @@ const posix = {
|
||||
return posix.normalize(joined);
|
||||
},
|
||||
|
||||
|
||||
relative: function relative(from, to) {
|
||||
relative(from, to) {
|
||||
validateString(from, 'from');
|
||||
validateString(to, 'to');
|
||||
|
||||
@ -1137,19 +1102,19 @@ const posix = {
|
||||
return '';
|
||||
|
||||
// Trim any leading backslashes
|
||||
var fromStart = 1;
|
||||
for (; fromStart < from.length; ++fromStart) {
|
||||
if (from.charCodeAt(fromStart) !== CHAR_FORWARD_SLASH)
|
||||
break;
|
||||
let fromStart = 1;
|
||||
while (fromStart < from.length &&
|
||||
from.charCodeAt(fromStart) === CHAR_FORWARD_SLASH) {
|
||||
fromStart++;
|
||||
}
|
||||
const fromEnd = from.length;
|
||||
const fromLen = (fromEnd - fromStart);
|
||||
|
||||
// Trim any leading backslashes
|
||||
var toStart = 1;
|
||||
for (; toStart < to.length; ++toStart) {
|
||||
if (to.charCodeAt(toStart) !== CHAR_FORWARD_SLASH)
|
||||
break;
|
||||
let toStart = 1;
|
||||
while (toStart < to.length &&
|
||||
to.charCodeAt(toStart) === CHAR_FORWARD_SLASH) {
|
||||
toStart++;
|
||||
}
|
||||
const toEnd = to.length;
|
||||
const toLen = (toEnd - toStart);
|
||||
@ -1196,32 +1161,28 @@ const posix = {
|
||||
// and `from`
|
||||
for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
|
||||
if (i === fromEnd || from.charCodeAt(i) === CHAR_FORWARD_SLASH) {
|
||||
if (out.length === 0)
|
||||
out += '..';
|
||||
else
|
||||
out += '/..';
|
||||
out += out.length === 0 ? '..' : '/..';
|
||||
}
|
||||
}
|
||||
|
||||
toStart += lastCommonSep;
|
||||
|
||||
// Lastly, append the rest of the destination (`to`) path that comes after
|
||||
// the common path parts
|
||||
if (out.length > 0)
|
||||
return out + to.slice(toStart + lastCommonSep);
|
||||
else {
|
||||
toStart += lastCommonSep;
|
||||
if (to.charCodeAt(toStart) === CHAR_FORWARD_SLASH)
|
||||
++toStart;
|
||||
return to.slice(toStart);
|
||||
}
|
||||
return `${out}${to.slice(toStart)}`;
|
||||
|
||||
if (to.charCodeAt(toStart) === CHAR_FORWARD_SLASH)
|
||||
++toStart;
|
||||
return to.slice(toStart);
|
||||
},
|
||||
|
||||
|
||||
toNamespacedPath: function toNamespacedPath(path) {
|
||||
toNamespacedPath(path) {
|
||||
// Non-op on posix systems
|
||||
return path;
|
||||
},
|
||||
|
||||
dirname: function dirname(path) {
|
||||
dirname(path) {
|
||||
validateString(path, 'path');
|
||||
if (path.length === 0)
|
||||
return '.';
|
||||
@ -1247,8 +1208,7 @@ const posix = {
|
||||
return path.slice(0, end);
|
||||
},
|
||||
|
||||
|
||||
basename: function basename(path, ext) {
|
||||
basename(path, ext) {
|
||||
if (ext !== undefined)
|
||||
validateString(ext, 'ext');
|
||||
validateString(path, 'path');
|
||||
@ -1326,7 +1286,7 @@ const posix = {
|
||||
},
|
||||
|
||||
|
||||
extname: function extname(path) {
|
||||
extname(path) {
|
||||
validateString(path, 'path');
|
||||
var startDot = -1;
|
||||
var startPart = 0;
|
||||
@ -1475,14 +1435,12 @@ const posix = {
|
||||
return ret;
|
||||
},
|
||||
|
||||
|
||||
sep: '/',
|
||||
delimiter: ':',
|
||||
win32: null,
|
||||
posix: null
|
||||
};
|
||||
|
||||
|
||||
posix.win32 = win32.win32 = win32;
|
||||
posix.posix = win32.posix = posix;
|
||||
|
||||
@ -1490,7 +1448,4 @@ posix.posix = win32.posix = posix;
|
||||
win32._makeLong = win32.toNamespacedPath;
|
||||
posix._makeLong = posix.toNamespacedPath;
|
||||
|
||||
if (process.platform === 'win32')
|
||||
module.exports = win32;
|
||||
else
|
||||
module.exports = posix;
|
||||
module.exports = process.platform === 'win32' ? win32 : posix;
|
||||
|
Loading…
x
Reference in New Issue
Block a user