path: minor refactoring

1) This uses some ternary expressions instead of if else to assign
   some variables.
2) Use template strings instead of concat.
3) Use the object shortand notation.
4) Some var to let / const.
5) Removed some double line breaks.
6) Less brackets around statements if not necessary.

PR-URL: https://github.com/nodejs/node/pull/25278
Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
Ruben Bridgewater 2019-01-27 04:17:08 +01:00
parent 410eb97bce
commit 05a8dbc91b
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762

View File

@ -91,17 +91,11 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
} }
} }
if (allowAboveRoot) { if (allowAboveRoot) {
if (res.length > 0) res += res.length > 0 ? `${separator}..` : '..';
res += `${separator}..`;
else
res = '..';
lastSegmentLength = 2; lastSegmentLength = 2;
} }
} else { } else {
if (res.length > 0) res += (res.length > 0 ? separator : '') + path.slice(lastSlash + 1, i);
res += separator + path.slice(lastSlash + 1, i);
else
res = path.slice(lastSlash + 1, i);
lastSegmentLength = i - lastSlash - 1; lastSegmentLength = i - lastSlash - 1;
} }
lastSlash = i; lastSlash = i;
@ -118,14 +112,11 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
function _format(sep, pathObject) { function _format(sep, pathObject) {
const dir = pathObject.dir || pathObject.root; const dir = pathObject.dir || pathObject.root;
const base = pathObject.base || const base = pathObject.base ||
((pathObject.name || '') + (pathObject.ext || '')); `${pathObject.name || ''}${pathObject.ext || ''}`;
if (!dir) { if (!dir) {
return base; return base;
} }
if (dir === pathObject.root) { return dir === pathObject.root ? `${dir}${base}` : `${dir}${sep}${base}`;
return dir + base;
}
return dir + sep + base;
} }
const win32 = { const win32 = {
@ -147,7 +138,7 @@ const win32 = {
// absolute path, get cwd for that drive, or the process cwd if // absolute path, get cwd for that drive, or the process cwd if
// the drive cwd is not available. We're sure the device is not // the drive cwd is not available. We're sure the device is not
// a UNC path at this points, because UNC paths are always absolute. // a UNC path at this points, because UNC paths are always absolute.
path = process.env['=' + resolvedDevice] || process.cwd(); path = process.env[`=${resolvedDevice}`] || process.cwd();
// 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.
@ -272,18 +263,19 @@ const win32 = {
resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\', resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\',
isPathSeparator); isPathSeparator);
return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) || return resolvedAbsolute ?
'.'; `${resolvedDevice}\\${resolvedTail}` :
`${resolvedDevice}${resolvedTail}` || '.';
}, },
normalize: function normalize(path) { normalize(path) {
validateString(path, 'path'); validateString(path, 'path');
const len = path.length; const len = path.length;
if (len === 0) if (len === 0)
return '.'; return '.';
var rootEnd = 0; let rootEnd = 0;
var device; let device;
var isAbsolute = false; let isAbsolute = false;
const code = path.charCodeAt(0); const code = path.charCodeAt(0);
// Try to match a root // Try to match a root
@ -360,42 +352,20 @@ const win32 = {
return '\\'; return '\\';
} }
var tail; let tail = rootEnd < len ?
if (rootEnd < len) { normalizeString(path.slice(rootEnd), !isAbsolute, '\\', isPathSeparator) :
tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\', '';
isPathSeparator);
} else {
tail = '';
}
if (tail.length === 0 && !isAbsolute) if (tail.length === 0 && !isAbsolute)
tail = '.'; tail = '.';
if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1))) if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1)))
tail += '\\'; tail += '\\';
if (device === undefined) { if (device === undefined) {
if (isAbsolute) { return isAbsolute ? `\\${tail}` : tail;
if (tail.length > 0)
return '\\' + tail;
else
return '\\';
} else if (tail.length > 0) {
return tail;
} else {
return '';
}
} else if (isAbsolute) {
if (tail.length > 0)
return device + '\\' + tail;
else
return device + '\\';
} else if (tail.length > 0) {
return device + tail;
} else {
return device;
} }
return isAbsolute ? `${device}\\${tail}` : `${device}${tail}`;
}, },
isAbsolute(path) {
isAbsolute: function isAbsolute(path) {
validateString(path, 'path'); validateString(path, 'path');
const len = path.length; const len = path.length;
if (len === 0) if (len === 0)
@ -429,7 +399,7 @@ const win32 = {
if (joined === undefined) if (joined === undefined)
joined = firstPart = arg; joined = firstPart = arg;
else else
joined += '\\' + arg; joined += `\\${arg}`;
} }
} }
@ -449,8 +419,8 @@ const win32 = {
// This means that the user can use join to construct UNC paths from // This means that the user can use join to construct UNC paths from
// a server name and a share name; for example: // a server name and a share name; for example:
// path.join('//server', 'share') -> '\\\\server\\share\\') // path.join('//server', 'share') -> '\\\\server\\share\\')
var needsReplace = true; let needsReplace = true;
var slashCount = 0; let slashCount = 0;
if (isPathSeparator(firstPart.charCodeAt(0))) { if (isPathSeparator(firstPart.charCodeAt(0))) {
++slashCount; ++slashCount;
const firstLen = firstPart.length; const firstLen = firstPart.length;
@ -477,26 +447,25 @@ const win32 = {
// Replace the slashes if needed // Replace the slashes if needed
if (slashCount >= 2) if (slashCount >= 2)
joined = '\\' + joined.slice(slashCount); joined = `\\${joined.slice(slashCount)}`;
} }
return win32.normalize(joined); return win32.normalize(joined);
}, },
// It will solve the relative path from `from` to `to`, for instance: // It will solve the relative path from `from` to `to`, for instance:
// from = 'C:\\orandea\\test\\aaa' // from = 'C:\\orandea\\test\\aaa'
// to = 'C:\\orandea\\impl\\bbb' // to = 'C:\\orandea\\impl\\bbb'
// The output of the function should be: '..\\..\\impl\\bbb' // The output of the function should be: '..\\..\\impl\\bbb'
relative: function relative(from, to) { relative(from, to) {
validateString(from, 'from'); validateString(from, 'from');
validateString(to, 'to'); validateString(to, 'to');
if (from === to) if (from === to)
return ''; return '';
var fromOrig = win32.resolve(from); const fromOrig = win32.resolve(from);
var toOrig = win32.resolve(to); const toOrig = win32.resolve(to);
if (fromOrig === toOrig) if (fromOrig === toOrig)
return ''; return '';
@ -519,7 +488,7 @@ const win32 = {
if (from.charCodeAt(fromEnd - 1) !== CHAR_BACKWARD_SLASH) if (from.charCodeAt(fromEnd - 1) !== CHAR_BACKWARD_SLASH)
break; break;
} }
var fromLen = (fromEnd - fromStart); const fromLen = fromEnd - fromStart;
// Trim any leading backslashes // Trim any leading backslashes
var toStart = 0; var toStart = 0;
@ -533,7 +502,7 @@ const win32 = {
if (to.charCodeAt(toEnd - 1) !== CHAR_BACKWARD_SLASH) if (to.charCodeAt(toEnd - 1) !== CHAR_BACKWARD_SLASH)
break; break;
} }
var toLen = (toEnd - toStart); const toLen = toEnd - toStart;
// Compare paths to find the longest common path from root // Compare paths to find the longest common path from root
var length = (fromLen < toLen ? fromLen : toLen); var length = (fromLen < toLen ? fromLen : toLen);
@ -579,17 +548,14 @@ const win32 = {
return toOrig; return toOrig;
} }
var out = ''; let out = '';
if (lastCommonSep === -1) if (lastCommonSep === -1)
lastCommonSep = 0; lastCommonSep = 0;
// Generate the relative path based on the path difference between `to` and // Generate the relative path based on the path difference between `to` and
// `from` // `from`
for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) { for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
if (i === fromEnd || from.charCodeAt(i) === CHAR_BACKWARD_SLASH) { if (i === fromEnd || from.charCodeAt(i) === CHAR_BACKWARD_SLASH) {
if (out.length === 0) out += out.length === 0 ? '..' : '\\..';
out += '..';
else
out += '\\..';
} }
} }
@ -606,7 +572,7 @@ const win32 = {
}, },
toNamespacedPath: function toNamespacedPath(path) { toNamespacedPath(path) {
// Note: this will *probably* throw somewhere. // Note: this will *probably* throw somewhere.
if (typeof path !== 'string') if (typeof path !== 'string')
return path; return path;
@ -642,7 +608,7 @@ const win32 = {
return path; return path;
}, },
dirname: function dirname(path) { dirname(path) {
validateString(path, 'path'); validateString(path, 'path');
const len = path.length; const len = path.length;
if (len === 0) if (len === 0)
@ -731,14 +697,13 @@ const win32 = {
if (end === -1) { if (end === -1) {
if (rootEnd === -1) if (rootEnd === -1)
return '.'; return '.';
else
end = rootEnd; end = rootEnd;
} }
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');
@ -902,7 +867,7 @@ const win32 = {
parse: function parse(path) { parse: function parse(path) {
validateString(path, 'path'); validateString(path, 'path');
var ret = { root: '', dir: '', base: '', ext: '', name: '' }; const ret = { root: '', dir: '', base: '', ext: '', name: '' };
if (path.length === 0) if (path.length === 0)
return ret; return ret;
@ -1177,8 +1142,8 @@ const posix = {
if (from.charCodeAt(fromStart) !== CHAR_FORWARD_SLASH) if (from.charCodeAt(fromStart) !== CHAR_FORWARD_SLASH)
break; break;
} }
var fromEnd = from.length; const fromEnd = from.length;
var fromLen = (fromEnd - fromStart); const fromLen = (fromEnd - fromStart);
// Trim any leading backslashes // Trim any leading backslashes
var toStart = 1; var toStart = 1;
@ -1186,8 +1151,8 @@ const posix = {
if (to.charCodeAt(toStart) !== CHAR_FORWARD_SLASH) if (to.charCodeAt(toStart) !== CHAR_FORWARD_SLASH)
break; break;
} }
var toEnd = to.length; const toEnd = to.length;
var toLen = (toEnd - toStart); const toLen = (toEnd - toStart);
// Compare paths to find the longest common path from root // Compare paths to find the longest common path from root
var length = (fromLen < toLen ? fromLen : toLen); var length = (fromLen < toLen ? fromLen : toLen);
@ -1425,10 +1390,10 @@ const posix = {
parse: function parse(path) { parse: function parse(path) {
validateString(path, 'path'); validateString(path, 'path');
var ret = { root: '', dir: '', base: '', ext: '', name: '' }; const ret = { root: '', dir: '', base: '', ext: '', name: '' };
if (path.length === 0) if (path.length === 0)
return ret; return ret;
var isAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH; const isAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
var start; var start;
if (isAbsolute) { if (isAbsolute) {
ret.root = '/'; ret.root = '/';