Skip to content
Permalink
Browse files

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: #25278
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information...
BridgeAR authored and addaleax committed Jan 27, 2019
1 parent d94f4c2 commit b0cde2c4cf79f0a3abd3b94e38e34ab35294b604
Showing with 41 additions and 76 deletions.
  1. +41 −76 lib/path.js
@@ -91,17 +91,11 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
}
}
if (allowAboveRoot) {
if (res.length > 0)
res += `${separator}..`;
else
res = '..';
res += res.length > 0 ? `${separator}..` : '..';
lastSegmentLength = 2;
}
} else {
if (res.length > 0)
res += separator + path.slice(lastSlash + 1, i);
else
res = path.slice(lastSlash + 1, i);
res += (res.length > 0 ? separator : '') + path.slice(lastSlash + 1, i);
lastSegmentLength = i - lastSlash - 1;
}
lastSlash = i;
@@ -118,14 +112,11 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
function _format(sep, pathObject) {
const dir = pathObject.dir || pathObject.root;
const base = pathObject.base ||
((pathObject.name || '') + (pathObject.ext || ''));
`${pathObject.name || ''}${pathObject.ext || ''}`;
if (!dir) {
return base;
}
if (dir === pathObject.root) {
return dir + base;
}
return dir + sep + base;
return dir === pathObject.root ? `${dir}${base}` : `${dir}${sep}${base}`;
}

const win32 = {
@@ -147,7 +138,7 @@ const win32 = {
// 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
// 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
// to our drive. If not, default to the drive's root.
@@ -272,18 +263,19 @@ const win32 = {
resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\',
isPathSeparator);

return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) ||
'.';
return resolvedAbsolute ?
`${resolvedDevice}\\${resolvedTail}` :
`${resolvedDevice}${resolvedTail}` || '.';
},

normalize: function normalize(path) {
normalize(path) {
validateString(path, 'path');
const len = path.length;
if (len === 0)
return '.';
var rootEnd = 0;
var device;
var isAbsolute = false;
let rootEnd = 0;
let device;
let isAbsolute = false;
const code = path.charCodeAt(0);

// Try to match a root
@@ -360,42 +352,20 @@ const win32 = {
return '\\';
}

var tail;
if (rootEnd < len) {
tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\',
isPathSeparator);
} else {
tail = '';
}
let tail = rootEnd < len ?
normalizeString(path.slice(rootEnd), !isAbsolute, '\\', isPathSeparator) :
'';
if (tail.length === 0 && !isAbsolute)
tail = '.';
if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1)))
tail += '\\';
if (device === undefined) {
if (isAbsolute) {
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 ? `\\${tail}` : tail;
}
return isAbsolute ? `${device}\\${tail}` : `${device}${tail}`;
},


isAbsolute: function isAbsolute(path) {
isAbsolute(path) {
validateString(path, 'path');
const len = path.length;
if (len === 0)
@@ -429,7 +399,7 @@ const win32 = {
if (joined === undefined)
joined = firstPart = arg;
else
joined += '\\' + arg;
joined += `\\${arg}`;
}
}

@@ -449,8 +419,8 @@ const win32 = {
// This means that the user can use join to construct UNC paths from
// a server name and a share name; for example:
// path.join('//server', 'share') -> '\\\\server\\share\\')
var needsReplace = true;
var slashCount = 0;
let needsReplace = true;
let slashCount = 0;
if (isPathSeparator(firstPart.charCodeAt(0))) {
++slashCount;
const firstLen = firstPart.length;
@@ -477,26 +447,25 @@ const win32 = {

// Replace the slashes if needed
if (slashCount >= 2)
joined = '\\' + joined.slice(slashCount);
joined = `\\${joined.slice(slashCount)}`;
}

return win32.normalize(joined);
},


// It will solve the relative path from `from` to `to`, for instance:
// from = 'C:\\orandea\\test\\aaa'
// to = 'C:\\orandea\\impl\\bbb'
// The output of the function should be: '..\\..\\impl\\bbb'
relative: function relative(from, to) {
relative(from, to) {
validateString(from, 'from');
validateString(to, 'to');

if (from === to)
return '';

var fromOrig = win32.resolve(from);
var toOrig = win32.resolve(to);
const fromOrig = win32.resolve(from);
const toOrig = win32.resolve(to);

if (fromOrig === toOrig)
return '';
@@ -519,7 +488,7 @@ const win32 = {
if (from.charCodeAt(fromEnd - 1) !== CHAR_BACKWARD_SLASH)
break;
}
var fromLen = (fromEnd - fromStart);
const fromLen = fromEnd - fromStart;

// Trim any leading backslashes
var toStart = 0;
@@ -533,7 +502,7 @@ const win32 = {
if (to.charCodeAt(toEnd - 1) !== CHAR_BACKWARD_SLASH)
break;
}
var toLen = (toEnd - toStart);
const toLen = toEnd - toStart;

// Compare paths to find the longest common path from root
var length = (fromLen < toLen ? fromLen : toLen);
@@ -579,17 +548,14 @@ const win32 = {
return toOrig;
}

var out = '';
let out = '';
if (lastCommonSep === -1)
lastCommonSep = 0;
// Generate the relative path based on the path difference between `to` and
// `from`
for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
if (i === fromEnd || from.charCodeAt(i) === CHAR_BACKWARD_SLASH) {
if (out.length === 0)
out += '..';
else
out += '\\..';
out += out.length === 0 ? '..' : '\\..';
}
}

@@ -606,7 +572,7 @@ const win32 = {
},


toNamespacedPath: function toNamespacedPath(path) {
toNamespacedPath(path) {
// Note: this will *probably* throw somewhere.
if (typeof path !== 'string')
return path;
@@ -642,7 +608,7 @@ const win32 = {
return path;
},

dirname: function dirname(path) {
dirname(path) {
validateString(path, 'path');
const len = path.length;
if (len === 0)
@@ -731,14 +697,13 @@ const win32 = {
if (end === -1) {
if (rootEnd === -1)
return '.';
else
end = rootEnd;

end = rootEnd;
}
return path.slice(0, end);
},


basename: function basename(path, ext) {
basename(path, ext) {
if (ext !== undefined)
validateString(ext, 'ext');
validateString(path, 'path');
@@ -902,7 +867,7 @@ const win32 = {
parse: function parse(path) {
validateString(path, 'path');

var ret = { root: '', dir: '', base: '', ext: '', name: '' };
const ret = { root: '', dir: '', base: '', ext: '', name: '' };
if (path.length === 0)
return ret;

@@ -1177,17 +1142,17 @@ const posix = {
if (from.charCodeAt(fromStart) !== CHAR_FORWARD_SLASH)
break;
}
var fromEnd = from.length;
var fromLen = (fromEnd - 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;
}
var toEnd = to.length;
var toLen = (toEnd - toStart);
const toEnd = to.length;
const toLen = (toEnd - toStart);

// Compare paths to find the longest common path from root
var length = (fromLen < toLen ? fromLen : toLen);
@@ -1425,10 +1390,10 @@ const posix = {
parse: function parse(path) {
validateString(path, 'path');

var ret = { root: '', dir: '', base: '', ext: '', name: '' };
const ret = { root: '', dir: '', base: '', ext: '', name: '' };
if (path.length === 0)
return ret;
var isAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
const isAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
var start;
if (isAbsolute) {
ret.root = '/';

0 comments on commit b0cde2c

Please sign in to comment.
You can’t perform that action at this time.