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
|
// Verify that a cwd was found and that it actually points
|
||||||
// to our drive. If not, default to the drive's root.
|
// to our drive. If not, default to the drive's root.
|
||||||
if (path === undefined ||
|
if (path === undefined ||
|
||||||
path.slice(0, 3).toLowerCase() !==
|
path.slice(0, 2).toLowerCase() !== resolvedDevice.toLowerCase() &&
|
||||||
resolvedDevice.toLowerCase() + '\\') {
|
path.charCodeAt(2) === CHAR_BACKWARD_SLASH) {
|
||||||
path = resolvedDevice + '\\';
|
path = `${resolvedDevice}\\`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,39 +173,30 @@ const win32 = {
|
|||||||
|
|
||||||
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) {
|
if (j < len && j !== last) {
|
||||||
const firstPart = path.slice(last, j);
|
const firstPart = path.slice(last, j);
|
||||||
// Matched!
|
// Matched!
|
||||||
last = j;
|
last = j;
|
||||||
// Match 1 or more path separators
|
// Match 1 or more 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 && j !== last) {
|
||||||
// Matched!
|
// Matched!
|
||||||
last = j;
|
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) {
|
if (j === len || j !== last) {
|
||||||
// We matched a UNC root only
|
// We matched a UNC root
|
||||||
|
device = `\\\\${firstPart}\\${path.slice(last, j)}`;
|
||||||
device = '\\\\' + firstPart + '\\' + path.slice(last);
|
|
||||||
rootEnd = j;
|
|
||||||
} else if (j !== last) {
|
|
||||||
// We matched a UNC root with leftovers
|
|
||||||
|
|
||||||
device = '\\\\' + firstPart + '\\' + path.slice(last, j);
|
|
||||||
rootEnd = j;
|
rootEnd = j;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -372,28 +363,22 @@ const win32 = {
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
const code = path.charCodeAt(0);
|
const code = path.charCodeAt(0);
|
||||||
if (isPathSeparator(code)) {
|
return isPathSeparator(code) ||
|
||||||
return true;
|
|
||||||
} else if (isWindowsDeviceRoot(code)) {
|
|
||||||
// Possible device root
|
// Possible device root
|
||||||
|
len > 2 &&
|
||||||
if (len > 2 && path.charCodeAt(1) === CHAR_COLON) {
|
isWindowsDeviceRoot(code) &&
|
||||||
if (isPathSeparator(path.charCodeAt(2)))
|
path.charCodeAt(1) === CHAR_COLON &&
|
||||||
return true;
|
isPathSeparator(path.charCodeAt(2));
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
join(...args) {
|
||||||
join: function join() {
|
if (args.length === 0)
|
||||||
if (arguments.length === 0)
|
|
||||||
return '.';
|
return '.';
|
||||||
|
|
||||||
var joined;
|
let joined;
|
||||||
var firstPart;
|
let firstPart;
|
||||||
for (var i = 0; i < arguments.length; ++i) {
|
for (var i = 0; i < args.length; ++i) {
|
||||||
var arg = arguments[i];
|
const arg = args[i];
|
||||||
validateString(arg, 'path');
|
validateString(arg, 'path');
|
||||||
if (arg.length > 0) {
|
if (arg.length > 0) {
|
||||||
if (joined === undefined)
|
if (joined === undefined)
|
||||||
@ -440,9 +425,9 @@ const win32 = {
|
|||||||
}
|
}
|
||||||
if (needsReplace) {
|
if (needsReplace) {
|
||||||
// Find any more consecutive slashes we need to replace
|
// Find any more consecutive slashes we need to replace
|
||||||
for (; slashCount < joined.length; ++slashCount) {
|
while (slashCount < joined.length &&
|
||||||
if (!isPathSeparator(joined.charCodeAt(slashCount)))
|
isPathSeparator(joined.charCodeAt(slashCount))) {
|
||||||
break;
|
slashCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace the slashes if needed
|
// Replace the slashes if needed
|
||||||
@ -477,30 +462,30 @@ const win32 = {
|
|||||||
return '';
|
return '';
|
||||||
|
|
||||||
// Trim any leading backslashes
|
// Trim any leading backslashes
|
||||||
var fromStart = 0;
|
let fromStart = 0;
|
||||||
for (; fromStart < from.length; ++fromStart) {
|
while (fromStart < from.length &&
|
||||||
if (from.charCodeAt(fromStart) !== CHAR_BACKWARD_SLASH)
|
from.charCodeAt(fromStart) === CHAR_BACKWARD_SLASH) {
|
||||||
break;
|
fromStart++;
|
||||||
}
|
}
|
||||||
// Trim trailing backslashes (applicable to UNC paths only)
|
// Trim trailing backslashes (applicable to UNC paths only)
|
||||||
var fromEnd = from.length;
|
let fromEnd = from.length;
|
||||||
for (; fromEnd - 1 > fromStart; --fromEnd) {
|
while (fromEnd - 1 > fromStart &&
|
||||||
if (from.charCodeAt(fromEnd - 1) !== CHAR_BACKWARD_SLASH)
|
from.charCodeAt(fromEnd - 1) === CHAR_BACKWARD_SLASH) {
|
||||||
break;
|
fromEnd--;
|
||||||
}
|
}
|
||||||
const fromLen = fromEnd - fromStart;
|
const fromLen = fromEnd - fromStart;
|
||||||
|
|
||||||
// Trim any leading backslashes
|
// Trim any leading backslashes
|
||||||
var toStart = 0;
|
let toStart = 0;
|
||||||
for (; toStart < to.length; ++toStart) {
|
while (toStart < to.length &&
|
||||||
if (to.charCodeAt(toStart) !== CHAR_BACKWARD_SLASH)
|
to.charCodeAt(toStart) === CHAR_BACKWARD_SLASH) {
|
||||||
break;
|
toStart++;
|
||||||
}
|
}
|
||||||
// Trim trailing backslashes (applicable to UNC paths only)
|
// Trim trailing backslashes (applicable to UNC paths only)
|
||||||
var toEnd = to.length;
|
let toEnd = to.length;
|
||||||
for (; toEnd - 1 > toStart; --toEnd) {
|
while (toEnd - 1 > toStart &&
|
||||||
if (to.charCodeAt(toEnd - 1) !== CHAR_BACKWARD_SLASH)
|
to.charCodeAt(toEnd - 1) === CHAR_BACKWARD_SLASH) {
|
||||||
break;
|
toEnd--;
|
||||||
}
|
}
|
||||||
const toLen = toEnd - toStart;
|
const toLen = toEnd - toStart;
|
||||||
|
|
||||||
@ -559,18 +544,17 @@ const win32 = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toStart += lastCommonSep;
|
||||||
|
|
||||||
// Lastly, append the rest of the destination (`to`) path that comes after
|
// Lastly, append the rest of the destination (`to`) path that comes after
|
||||||
// the common path parts
|
// the common path parts
|
||||||
if (out.length > 0)
|
if (out.length > 0)
|
||||||
return out + toOrig.slice(toStart + lastCommonSep, toEnd);
|
return `${out}${toOrig.slice(toStart, toEnd)}`;
|
||||||
else {
|
|
||||||
toStart += lastCommonSep;
|
|
||||||
if (toOrig.charCodeAt(toStart) === CHAR_BACKWARD_SLASH)
|
|
||||||
++toStart;
|
|
||||||
return toOrig.slice(toStart, toEnd);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
|
if (toOrig.charCodeAt(toStart) === CHAR_BACKWARD_SLASH)
|
||||||
|
++toStart;
|
||||||
|
return toOrig.slice(toStart, toEnd);
|
||||||
|
},
|
||||||
|
|
||||||
toNamespacedPath(path) {
|
toNamespacedPath(path) {
|
||||||
// Note: this will *probably* throw somewhere.
|
// Note: this will *probably* throw somewhere.
|
||||||
@ -791,7 +775,7 @@ const win32 = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
extname: function extname(path) {
|
extname(path) {
|
||||||
validateString(path, 'path');
|
validateString(path, 'path');
|
||||||
var start = 0;
|
var start = 0;
|
||||||
var startDot = -1;
|
var startDot = -1;
|
||||||
@ -1020,27 +1004,20 @@ const win32 = {
|
|||||||
return ret;
|
return ret;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
sep: '\\',
|
sep: '\\',
|
||||||
delimiter: ';',
|
delimiter: ';',
|
||||||
win32: null,
|
win32: null,
|
||||||
posix: null
|
posix: null
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const posix = {
|
const posix = {
|
||||||
// path.resolve([from ...], to)
|
// path.resolve([from ...], to)
|
||||||
resolve: function resolve() {
|
resolve(...args) {
|
||||||
var resolvedPath = '';
|
let resolvedPath = '';
|
||||||
var resolvedAbsolute = false;
|
let resolvedAbsolute = false;
|
||||||
|
|
||||||
for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
|
for (var i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
|
||||||
var path;
|
const path = i >= 0 ? args[i] : process.cwd();
|
||||||
if (i >= 0)
|
|
||||||
path = arguments[i];
|
|
||||||
else {
|
|
||||||
path = process.cwd();
|
|
||||||
}
|
|
||||||
|
|
||||||
validateString(path, 'path');
|
validateString(path, 'path');
|
||||||
|
|
||||||
@ -1049,7 +1026,7 @@ const posix = {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
resolvedPath = path + '/' + resolvedPath;
|
resolvedPath = `${path}/${resolvedPath}`;
|
||||||
resolvedAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
|
resolvedAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1061,19 +1038,12 @@ const posix = {
|
|||||||
isPosixPathSeparator);
|
isPosixPathSeparator);
|
||||||
|
|
||||||
if (resolvedAbsolute) {
|
if (resolvedAbsolute) {
|
||||||
if (resolvedPath.length > 0)
|
return `/${resolvedPath}`;
|
||||||
return '/' + resolvedPath;
|
|
||||||
else
|
|
||||||
return '/';
|
|
||||||
} else if (resolvedPath.length > 0) {
|
|
||||||
return resolvedPath;
|
|
||||||
} else {
|
|
||||||
return '.';
|
|
||||||
}
|
}
|
||||||
|
return resolvedPath.length > 0 ? resolvedPath : '.';
|
||||||
},
|
},
|
||||||
|
|
||||||
|
normalize(path) {
|
||||||
normalize: function normalize(path) {
|
|
||||||
validateString(path, 'path');
|
validateString(path, 'path');
|
||||||
|
|
||||||
if (path.length === 0)
|
if (path.length === 0)
|
||||||
@ -1091,30 +1061,26 @@ const posix = {
|
|||||||
if (path.length > 0 && trailingSeparator)
|
if (path.length > 0 && trailingSeparator)
|
||||||
path += '/';
|
path += '/';
|
||||||
|
|
||||||
if (isAbsolute)
|
return isAbsolute ? `/${path}` : path;
|
||||||
return '/' + path;
|
|
||||||
return path;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
isAbsolute(path) {
|
||||||
isAbsolute: function isAbsolute(path) {
|
|
||||||
validateString(path, 'path');
|
validateString(path, 'path');
|
||||||
return path.length > 0 && path.charCodeAt(0) === CHAR_FORWARD_SLASH;
|
return path.length > 0 && path.charCodeAt(0) === CHAR_FORWARD_SLASH;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
join(...args) {
|
||||||
join: function join() {
|
if (args.length === 0)
|
||||||
if (arguments.length === 0)
|
|
||||||
return '.';
|
return '.';
|
||||||
var joined;
|
let joined;
|
||||||
for (var i = 0; i < arguments.length; ++i) {
|
for (var i = 0; i < args.length; ++i) {
|
||||||
var arg = arguments[i];
|
const arg = args[i];
|
||||||
validateString(arg, 'path');
|
validateString(arg, 'path');
|
||||||
if (arg.length > 0) {
|
if (arg.length > 0) {
|
||||||
if (joined === undefined)
|
if (joined === undefined)
|
||||||
joined = arg;
|
joined = arg;
|
||||||
else
|
else
|
||||||
joined += '/' + arg;
|
joined += `/${arg}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (joined === undefined)
|
if (joined === undefined)
|
||||||
@ -1122,8 +1088,7 @@ const posix = {
|
|||||||
return posix.normalize(joined);
|
return posix.normalize(joined);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
relative(from, to) {
|
||||||
relative: function relative(from, to) {
|
|
||||||
validateString(from, 'from');
|
validateString(from, 'from');
|
||||||
validateString(to, 'to');
|
validateString(to, 'to');
|
||||||
|
|
||||||
@ -1137,19 +1102,19 @@ const posix = {
|
|||||||
return '';
|
return '';
|
||||||
|
|
||||||
// Trim any leading backslashes
|
// Trim any leading backslashes
|
||||||
var fromStart = 1;
|
let fromStart = 1;
|
||||||
for (; fromStart < from.length; ++fromStart) {
|
while (fromStart < from.length &&
|
||||||
if (from.charCodeAt(fromStart) !== CHAR_FORWARD_SLASH)
|
from.charCodeAt(fromStart) === CHAR_FORWARD_SLASH) {
|
||||||
break;
|
fromStart++;
|
||||||
}
|
}
|
||||||
const fromEnd = from.length;
|
const fromEnd = from.length;
|
||||||
const fromLen = (fromEnd - fromStart);
|
const fromLen = (fromEnd - fromStart);
|
||||||
|
|
||||||
// Trim any leading backslashes
|
// Trim any leading backslashes
|
||||||
var toStart = 1;
|
let toStart = 1;
|
||||||
for (; toStart < to.length; ++toStart) {
|
while (toStart < to.length &&
|
||||||
if (to.charCodeAt(toStart) !== CHAR_FORWARD_SLASH)
|
to.charCodeAt(toStart) === CHAR_FORWARD_SLASH) {
|
||||||
break;
|
toStart++;
|
||||||
}
|
}
|
||||||
const toEnd = to.length;
|
const toEnd = to.length;
|
||||||
const toLen = (toEnd - toStart);
|
const toLen = (toEnd - toStart);
|
||||||
@ -1196,32 +1161,28 @@ const posix = {
|
|||||||
// and `from`
|
// and `from`
|
||||||
for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
|
for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
|
||||||
if (i === fromEnd || from.charCodeAt(i) === CHAR_FORWARD_SLASH) {
|
if (i === fromEnd || from.charCodeAt(i) === CHAR_FORWARD_SLASH) {
|
||||||
if (out.length === 0)
|
out += out.length === 0 ? '..' : '/..';
|
||||||
out += '..';
|
|
||||||
else
|
|
||||||
out += '/..';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toStart += lastCommonSep;
|
||||||
|
|
||||||
// Lastly, append the rest of the destination (`to`) path that comes after
|
// Lastly, append the rest of the destination (`to`) path that comes after
|
||||||
// the common path parts
|
// the common path parts
|
||||||
if (out.length > 0)
|
if (out.length > 0)
|
||||||
return out + to.slice(toStart + lastCommonSep);
|
return `${out}${to.slice(toStart)}`;
|
||||||
else {
|
|
||||||
toStart += lastCommonSep;
|
if (to.charCodeAt(toStart) === CHAR_FORWARD_SLASH)
|
||||||
if (to.charCodeAt(toStart) === CHAR_FORWARD_SLASH)
|
++toStart;
|
||||||
++toStart;
|
return to.slice(toStart);
|
||||||
return to.slice(toStart);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
|
toNamespacedPath(path) {
|
||||||
toNamespacedPath: function toNamespacedPath(path) {
|
|
||||||
// Non-op on posix systems
|
// Non-op on posix systems
|
||||||
return path;
|
return path;
|
||||||
},
|
},
|
||||||
|
|
||||||
dirname: function dirname(path) {
|
dirname(path) {
|
||||||
validateString(path, 'path');
|
validateString(path, 'path');
|
||||||
if (path.length === 0)
|
if (path.length === 0)
|
||||||
return '.';
|
return '.';
|
||||||
@ -1247,8 +1208,7 @@ const posix = {
|
|||||||
return path.slice(0, end);
|
return path.slice(0, end);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
basename(path, ext) {
|
||||||
basename: function basename(path, ext) {
|
|
||||||
if (ext !== undefined)
|
if (ext !== undefined)
|
||||||
validateString(ext, 'ext');
|
validateString(ext, 'ext');
|
||||||
validateString(path, 'path');
|
validateString(path, 'path');
|
||||||
@ -1326,7 +1286,7 @@ const posix = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
extname: function extname(path) {
|
extname(path) {
|
||||||
validateString(path, 'path');
|
validateString(path, 'path');
|
||||||
var startDot = -1;
|
var startDot = -1;
|
||||||
var startPart = 0;
|
var startPart = 0;
|
||||||
@ -1475,14 +1435,12 @@ const posix = {
|
|||||||
return ret;
|
return ret;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
sep: '/',
|
sep: '/',
|
||||||
delimiter: ':',
|
delimiter: ':',
|
||||||
win32: null,
|
win32: null,
|
||||||
posix: null
|
posix: null
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
posix.win32 = win32.win32 = win32;
|
posix.win32 = win32.win32 = win32;
|
||||||
posix.posix = win32.posix = posix;
|
posix.posix = win32.posix = posix;
|
||||||
|
|
||||||
@ -1490,7 +1448,4 @@ posix.posix = win32.posix = posix;
|
|||||||
win32._makeLong = win32.toNamespacedPath;
|
win32._makeLong = win32.toNamespacedPath;
|
||||||
posix._makeLong = posix.toNamespacedPath;
|
posix._makeLong = posix.toNamespacedPath;
|
||||||
|
|
||||||
if (process.platform === 'win32')
|
module.exports = process.platform === 'win32' ? win32 : posix;
|
||||||
module.exports = win32;
|
|
||||||
else
|
|
||||||
module.exports = posix;
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user