Skip to content

Commit b0cde2c

Browse files
BridgeARaddaleax
authored andcommitted
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 <[email protected]>
1 parent d94f4c2 commit b0cde2c

File tree

1 file changed

+41
-76
lines changed

1 file changed

+41
-76
lines changed

lib/path.js

+41-76
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,11 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
9191
}
9292
}
9393
if (allowAboveRoot) {
94-
if (res.length > 0)
95-
res += `${separator}..`;
96-
else
97-
res = '..';
94+
res += res.length > 0 ? `${separator}..` : '..';
9895
lastSegmentLength = 2;
9996
}
10097
} else {
101-
if (res.length > 0)
102-
res += separator + path.slice(lastSlash + 1, i);
103-
else
104-
res = path.slice(lastSlash + 1, i);
98+
res += (res.length > 0 ? separator : '') + path.slice(lastSlash + 1, i);
10599
lastSegmentLength = i - lastSlash - 1;
106100
}
107101
lastSlash = i;
@@ -118,14 +112,11 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
118112
function _format(sep, pathObject) {
119113
const dir = pathObject.dir || pathObject.root;
120114
const base = pathObject.base ||
121-
((pathObject.name || '') + (pathObject.ext || ''));
115+
`${pathObject.name || ''}${pathObject.ext || ''}`;
122116
if (!dir) {
123117
return base;
124118
}
125-
if (dir === pathObject.root) {
126-
return dir + base;
127-
}
128-
return dir + sep + base;
119+
return dir === pathObject.root ? `${dir}${base}` : `${dir}${sep}${base}`;
129120
}
130121

131122
const win32 = {
@@ -147,7 +138,7 @@ const win32 = {
147138
// absolute path, get cwd for that drive, or the process cwd if
148139
// the drive cwd is not available. We're sure the device is not
149140
// a UNC path at this points, because UNC paths are always absolute.
150-
path = process.env['=' + resolvedDevice] || process.cwd();
141+
path = process.env[`=${resolvedDevice}`] || process.cwd();
151142

152143
// Verify that a cwd was found and that it actually points
153144
// to our drive. If not, default to the drive's root.
@@ -272,18 +263,19 @@ const win32 = {
272263
resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\',
273264
isPathSeparator);
274265

275-
return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) ||
276-
'.';
266+
return resolvedAbsolute ?
267+
`${resolvedDevice}\\${resolvedTail}` :
268+
`${resolvedDevice}${resolvedTail}` || '.';
277269
},
278270

279-
normalize: function normalize(path) {
271+
normalize(path) {
280272
validateString(path, 'path');
281273
const len = path.length;
282274
if (len === 0)
283275
return '.';
284-
var rootEnd = 0;
285-
var device;
286-
var isAbsolute = false;
276+
let rootEnd = 0;
277+
let device;
278+
let isAbsolute = false;
287279
const code = path.charCodeAt(0);
288280

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

363-
var tail;
364-
if (rootEnd < len) {
365-
tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\',
366-
isPathSeparator);
367-
} else {
368-
tail = '';
369-
}
355+
let tail = rootEnd < len ?
356+
normalizeString(path.slice(rootEnd), !isAbsolute, '\\', isPathSeparator) :
357+
'';
370358
if (tail.length === 0 && !isAbsolute)
371359
tail = '.';
372360
if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1)))
373361
tail += '\\';
374362
if (device === undefined) {
375-
if (isAbsolute) {
376-
if (tail.length > 0)
377-
return '\\' + tail;
378-
else
379-
return '\\';
380-
} else if (tail.length > 0) {
381-
return tail;
382-
} else {
383-
return '';
384-
}
385-
} else if (isAbsolute) {
386-
if (tail.length > 0)
387-
return device + '\\' + tail;
388-
else
389-
return device + '\\';
390-
} else if (tail.length > 0) {
391-
return device + tail;
392-
} else {
393-
return device;
363+
return isAbsolute ? `\\${tail}` : tail;
394364
}
365+
return isAbsolute ? `${device}\\${tail}` : `${device}${tail}`;
395366
},
396367

397-
398-
isAbsolute: function isAbsolute(path) {
368+
isAbsolute(path) {
399369
validateString(path, 'path');
400370
const len = path.length;
401371
if (len === 0)
@@ -429,7 +399,7 @@ const win32 = {
429399
if (joined === undefined)
430400
joined = firstPart = arg;
431401
else
432-
joined += '\\' + arg;
402+
joined += `\\${arg}`;
433403
}
434404
}
435405

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

478448
// Replace the slashes if needed
479449
if (slashCount >= 2)
480-
joined = '\\' + joined.slice(slashCount);
450+
joined = `\\${joined.slice(slashCount)}`;
481451
}
482452

483453
return win32.normalize(joined);
484454
},
485455

486-
487456
// It will solve the relative path from `from` to `to`, for instance:
488457
// from = 'C:\\orandea\\test\\aaa'
489458
// to = 'C:\\orandea\\impl\\bbb'
490459
// The output of the function should be: '..\\..\\impl\\bbb'
491-
relative: function relative(from, to) {
460+
relative(from, to) {
492461
validateString(from, 'from');
493462
validateString(to, 'to');
494463

495464
if (from === to)
496465
return '';
497466

498-
var fromOrig = win32.resolve(from);
499-
var toOrig = win32.resolve(to);
467+
const fromOrig = win32.resolve(from);
468+
const toOrig = win32.resolve(to);
500469

501470
if (fromOrig === toOrig)
502471
return '';
@@ -519,7 +488,7 @@ const win32 = {
519488
if (from.charCodeAt(fromEnd - 1) !== CHAR_BACKWARD_SLASH)
520489
break;
521490
}
522-
var fromLen = (fromEnd - fromStart);
491+
const fromLen = fromEnd - fromStart;
523492

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

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

582-
var out = '';
551+
let out = '';
583552
if (lastCommonSep === -1)
584553
lastCommonSep = 0;
585554
// Generate the relative path based on the path difference between `to` and
586555
// `from`
587556
for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
588557
if (i === fromEnd || from.charCodeAt(i) === CHAR_BACKWARD_SLASH) {
589-
if (out.length === 0)
590-
out += '..';
591-
else
592-
out += '\\..';
558+
out += out.length === 0 ? '..' : '\\..';
593559
}
594560
}
595561

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

608574

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

645-
dirname: function dirname(path) {
611+
dirname(path) {
646612
validateString(path, 'path');
647613
const len = path.length;
648614
if (len === 0)
@@ -731,14 +697,13 @@ const win32 = {
731697
if (end === -1) {
732698
if (rootEnd === -1)
733699
return '.';
734-
else
735-
end = rootEnd;
700+
701+
end = rootEnd;
736702
}
737703
return path.slice(0, end);
738704
},
739705

740-
741-
basename: function basename(path, ext) {
706+
basename(path, ext) {
742707
if (ext !== undefined)
743708
validateString(ext, 'ext');
744709
validateString(path, 'path');
@@ -902,7 +867,7 @@ const win32 = {
902867
parse: function parse(path) {
903868
validateString(path, 'path');
904869

905-
var ret = { root: '', dir: '', base: '', ext: '', name: '' };
870+
const ret = { root: '', dir: '', base: '', ext: '', name: '' };
906871
if (path.length === 0)
907872
return ret;
908873

@@ -1177,17 +1142,17 @@ const posix = {
11771142
if (from.charCodeAt(fromStart) !== CHAR_FORWARD_SLASH)
11781143
break;
11791144
}
1180-
var fromEnd = from.length;
1181-
var fromLen = (fromEnd - fromStart);
1145+
const fromEnd = from.length;
1146+
const fromLen = (fromEnd - fromStart);
11821147

11831148
// Trim any leading backslashes
11841149
var toStart = 1;
11851150
for (; toStart < to.length; ++toStart) {
11861151
if (to.charCodeAt(toStart) !== CHAR_FORWARD_SLASH)
11871152
break;
11881153
}
1189-
var toEnd = to.length;
1190-
var toLen = (toEnd - toStart);
1154+
const toEnd = to.length;
1155+
const toLen = (toEnd - toStart);
11911156

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

1428-
var ret = { root: '', dir: '', base: '', ext: '', name: '' };
1393+
const ret = { root: '', dir: '', base: '', ext: '', name: '' };
14291394
if (path.length === 0)
14301395
return ret;
1431-
var isAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
1396+
const isAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
14321397
var start;
14331398
if (isAbsolute) {
14341399
ret.root = '/';

0 commit comments

Comments
 (0)