Skip to content

Commit 348f1fb

Browse files
BridgeARaddaleax
authored andcommitted
path: refactor for less indentation
This moves the `if (len === 1)` case to the top of the function. That way it is possible to reduce the indentation level due to returning early in that case. On top of that the following was done: 1) For clarity refactored for loops which were meant to count up a variable into a while loop. 2) Used template strings instead of string concat. 3) Consolidating nested if statements. 4) Using tenary expressions if applicable when assigning variables to reduce the code overhead. PR-URL: #25278 Reviewed-By: Michaël Zasso <[email protected]>
1 parent e00c8cd commit 348f1fb

File tree

1 file changed

+147
-174
lines changed

1 file changed

+147
-174
lines changed

lib/path.js

+147-174
Original file line numberDiff line numberDiff line change
@@ -270,77 +270,67 @@ const win32 = {
270270
const code = path.charCodeAt(0);
271271

272272
// Try to match a root
273-
if (len > 1) {
274-
if (isPathSeparator(code)) {
275-
// Possible UNC root
276-
277-
// If we started with a separator, we know we at least have an absolute
278-
// path of some kind (UNC or otherwise)
279-
isAbsolute = true;
273+
if (len === 1) {
274+
// `path` contains just a single char, exit early to avoid
275+
// unnecessary work
276+
return isPosixPathSeparator(code) ? '\\' : path;
277+
}
278+
if (isPathSeparator(code)) {
279+
// Possible UNC root
280280

281-
if (isPathSeparator(path.charCodeAt(1))) {
282-
// Matched double path separator at beginning
283-
var j = 2;
284-
var last = j;
285-
// Match 1 or more non-path separators
286-
for (; j < len; ++j) {
287-
if (isPathSeparator(path.charCodeAt(j)))
288-
break;
281+
// If we started with a separator, we know we at least have an absolute
282+
// path of some kind (UNC or otherwise)
283+
isAbsolute = true;
284+
285+
if (isPathSeparator(path.charCodeAt(1))) {
286+
// Matched double path separator at beginning
287+
let j = 2;
288+
let last = j;
289+
// Match 1 or more non-path separators
290+
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
291+
j++;
292+
}
293+
if (j < len && j !== last) {
294+
const firstPart = path.slice(last, j);
295+
// Matched!
296+
last = j;
297+
// Match 1 or more path separators
298+
while (j < len && isPathSeparator(path.charCodeAt(j))) {
299+
j++;
289300
}
290301
if (j < len && j !== last) {
291-
const firstPart = path.slice(last, j);
292302
// Matched!
293303
last = j;
294-
// Match 1 or more path separators
295-
for (; j < len; ++j) {
296-
if (!isPathSeparator(path.charCodeAt(j)))
297-
break;
304+
// Match 1 or more non-path separators
305+
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
306+
j++;
298307
}
299-
if (j < len && j !== last) {
300-
// Matched!
301-
last = j;
302-
// Match 1 or more non-path separators
303-
for (; j < len; ++j) {
304-
if (isPathSeparator(path.charCodeAt(j)))
305-
break;
306-
}
307-
if (j === len) {
308-
// We matched a UNC root only
309-
// Return the normalized version of the UNC root since there
310-
// is nothing left to process
311-
312-
return '\\\\' + firstPart + '\\' + path.slice(last) + '\\';
313-
} else if (j !== last) {
314-
// We matched a UNC root with leftovers
315-
316-
device = '\\\\' + firstPart + '\\' + path.slice(last, j);
317-
rootEnd = j;
318-
}
308+
if (j === len) {
309+
// We matched a UNC root only
310+
// Return the normalized version of the UNC root since there
311+
// is nothing left to process
312+
return `\\\\${firstPart}\\${path.slice(last)}\\`;
319313
}
320-
}
321-
} else {
322-
rootEnd = 1;
323-
}
324-
} else if (isWindowsDeviceRoot(code)) {
325-
// Possible device root
326-
327-
if (path.charCodeAt(1) === CHAR_COLON) {
328-
device = path.slice(0, 2);
329-
rootEnd = 2;
330-
if (len > 2) {
331-
if (isPathSeparator(path.charCodeAt(2))) {
332-
// Treat separator following drive name as an absolute path
333-
// indicator
334-
isAbsolute = true;
335-
rootEnd = 3;
314+
if (j !== last) {
315+
// We matched a UNC root with leftovers
316+
device = `\\\\${firstPart}\\${path.slice(last, j)}`;
317+
rootEnd = j;
336318
}
337319
}
338320
}
321+
} else {
322+
rootEnd = 1;
323+
}
324+
} else if (isWindowsDeviceRoot(code) && path.charCodeAt(1) === CHAR_COLON) {
325+
// Possible device root
326+
device = path.slice(0, 2);
327+
rootEnd = 2;
328+
if (len > 2 && isPathSeparator(path.charCodeAt(2))) {
329+
// Treat separator following drive name as an absolute path
330+
// indicator
331+
isAbsolute = true;
332+
rootEnd = 3;
339333
}
340-
} else if (isPathSeparator(code)) {
341-
// `path` contains just a path separator, exit early to avoid unnecessary
342-
// work
343-
return '\\';
344334
}
345335

346336
let tail = rootEnd < len ?
@@ -592,75 +582,66 @@ const win32 = {
592582
const len = path.length;
593583
if (len === 0)
594584
return '.';
595-
var rootEnd = -1;
596-
var end = -1;
597-
var matchedSlash = true;
598-
var offset = 0;
585+
let rootEnd = -1;
586+
let offset = 0;
599587
const code = path.charCodeAt(0);
600588

589+
if (len === 1) {
590+
// `path` contains just a path separator, exit early to avoid
591+
// unnecessary work or a dot.
592+
return isPathSeparator(code) ? path : '.';
593+
}
594+
601595
// Try to match a root
602-
if (len > 1) {
603-
if (isPathSeparator(code)) {
604-
// Possible UNC root
605-
606-
rootEnd = offset = 1;
607-
608-
if (isPathSeparator(path.charCodeAt(1))) {
609-
// Matched double path separator at beginning
610-
var j = 2;
611-
var last = j;
612-
// Match 1 or more non-path separators
613-
for (; j < len; ++j) {
614-
if (isPathSeparator(path.charCodeAt(j)))
615-
break;
596+
if (isPathSeparator(code)) {
597+
// Possible UNC root
598+
599+
rootEnd = offset = 1;
600+
601+
if (isPathSeparator(path.charCodeAt(1))) {
602+
// Matched double path separator at beginning
603+
let j = 2;
604+
let last = j;
605+
// Match 1 or more non-path separators
606+
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
607+
j++;
608+
}
609+
if (j < len && j !== last) {
610+
// Matched!
611+
last = j;
612+
// Match 1 or more path separators
613+
while (j < len && isPathSeparator(path.charCodeAt(j))) {
614+
j++;
616615
}
617616
if (j < len && j !== last) {
618617
// Matched!
619618
last = j;
620-
// Match 1 or more path separators
621-
for (; j < len; ++j) {
622-
if (!isPathSeparator(path.charCodeAt(j)))
623-
break;
619+
// Match 1 or more non-path separators
620+
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
621+
j++;
624622
}
625-
if (j < len && j !== last) {
626-
// Matched!
627-
last = j;
628-
// Match 1 or more non-path separators
629-
for (; j < len; ++j) {
630-
if (isPathSeparator(path.charCodeAt(j)))
631-
break;
632-
}
633-
if (j === len) {
634-
// We matched a UNC root only
635-
return path;
636-
}
637-
if (j !== last) {
638-
// We matched a UNC root with leftovers
623+
if (j === len) {
624+
// We matched a UNC root only
625+
return path;
626+
}
627+
if (j !== last) {
628+
// We matched a UNC root with leftovers
639629

640-
// Offset by 1 to include the separator after the UNC root to
641-
// treat it as a "normal root" on top of a (UNC) root
642-
rootEnd = offset = j + 1;
643-
}
630+
// Offset by 1 to include the separator after the UNC root to
631+
// treat it as a "normal root" on top of a (UNC) root
632+
rootEnd = offset = j + 1;
644633
}
645634
}
646635
}
647-
} else if (isWindowsDeviceRoot(code)) {
648-
// Possible device root
649-
650-
if (path.charCodeAt(1) === CHAR_COLON) {
651-
rootEnd = offset = 2;
652-
if (len > 2) {
653-
if (isPathSeparator(path.charCodeAt(2)))
654-
rootEnd = offset = 3;
655-
}
656-
}
657636
}
658-
} else if (isPathSeparator(code)) {
659-
// `path` contains just a path separator, exit early to avoid
660-
// unnecessary work
661-
return path;
637+
// Possible device root
638+
} else if (isWindowsDeviceRoot(code) && path.charCodeAt(1) === CHAR_COLON) {
639+
rootEnd = len > 2 && isPathSeparator(path.charCodeAt(2)) ? 3 : 2;
640+
offset = rootEnd;
662641
}
663642

643+
let end = -1;
644+
let matchedSlash = true;
664645
for (var i = len - 1; i >= offset; --i) {
665646
if (isPathSeparator(path.charCodeAt(i))) {
666647
if (!matchedSlash) {
@@ -843,79 +824,71 @@ const win32 = {
843824
var rootEnd = 0;
844825
let code = path.charCodeAt(0);
845826

846-
// Try to match a root
847-
if (len > 1) {
827+
if (len === 1) {
848828
if (isPathSeparator(code)) {
849-
// Possible UNC root
829+
// `path` contains just a path separator, exit early to avoid
830+
// unnecessary work
831+
ret.root = ret.dir = path;
832+
return ret;
833+
}
834+
return ret;
835+
}
836+
// Try to match a root
837+
if (isPathSeparator(code)) {
838+
// Possible UNC root
850839

851-
rootEnd = 1;
852-
if (isPathSeparator(path.charCodeAt(1))) {
853-
// Matched double path separator at beginning
854-
var j = 2;
855-
var last = j;
856-
// Match 1 or more non-path separators
857-
for (; j < len; ++j) {
858-
if (isPathSeparator(path.charCodeAt(j)))
859-
break;
840+
rootEnd = 1;
841+
if (isPathSeparator(path.charCodeAt(1))) {
842+
// Matched double path separator at beginning
843+
let j = 2;
844+
let last = j;
845+
// Match 1 or more non-path separators
846+
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
847+
j++;
848+
}
849+
if (j < len && j !== last) {
850+
// Matched!
851+
last = j;
852+
// Match 1 or more path separators
853+
while (j < len && isPathSeparator(path.charCodeAt(j))) {
854+
j++;
860855
}
861856
if (j < len && j !== last) {
862857
// Matched!
863858
last = j;
864-
// Match 1 or more path separators
865-
for (; j < len; ++j) {
866-
if (!isPathSeparator(path.charCodeAt(j)))
867-
break;
859+
// Match 1 or more non-path separators
860+
while (j < len && !isPathSeparator(path.charCodeAt(j))) {
861+
j++;
868862
}
869-
if (j < len && j !== last) {
870-
// Matched!
871-
last = j;
872-
// Match 1 or more non-path separators
873-
for (; j < len; ++j) {
874-
if (isPathSeparator(path.charCodeAt(j)))
875-
break;
876-
}
877-
if (j === len) {
878-
// We matched a UNC root only
879-
880-
rootEnd = j;
881-
} else if (j !== last) {
882-
// We matched a UNC root with leftovers
883-
884-
rootEnd = j + 1;
885-
}
863+
if (j === len) {
864+
// We matched a UNC root only
865+
rootEnd = j;
866+
} else if (j !== last) {
867+
// We matched a UNC root with leftovers
868+
rootEnd = j + 1;
886869
}
887870
}
888871
}
889-
} else if (isWindowsDeviceRoot(code)) {
890-
// Possible device root
891-
892-
if (path.charCodeAt(1) === CHAR_COLON) {
893-
rootEnd = 2;
894-
if (len > 2) {
895-
if (isPathSeparator(path.charCodeAt(2))) {
896-
if (len === 3) {
897-
// `path` contains just a drive root, exit early to avoid
898-
// unnecessary work
899-
ret.root = ret.dir = path;
900-
return ret;
901-
}
902-
rootEnd = 3;
903-
}
904-
} else {
905-
// `path` contains just a drive root, exit early to avoid
906-
// unnecessary work
907-
ret.root = ret.dir = path;
908-
return ret;
909-
}
872+
}
873+
} else if (isWindowsDeviceRoot(code) && path.charCodeAt(1) === CHAR_COLON) {
874+
// Possible device root
875+
if (len <= 2) {
876+
// `path` contains just a drive root, exit early to avoid
877+
// unnecessary work
878+
ret.root = ret.dir = path;
879+
return ret;
880+
}
881+
rootEnd = 2;
882+
if (isPathSeparator(path.charCodeAt(2))) {
883+
if (len === 3) {
884+
// `path` contains just a drive root, exit early to avoid
885+
// unnecessary work
886+
ret.root = ret.dir = path;
887+
return ret;
910888
}
889+
rootEnd = 3;
911890
}
912-
} else if (isPathSeparator(code)) {
913-
// `path` contains just a path separator, exit early to avoid
914-
// unnecessary work
915-
ret.root = ret.dir = path;
916-
return ret;
917891
}
918-
919892
if (rootEnd > 0)
920893
ret.root = path.slice(0, rootEnd);
921894

0 commit comments

Comments
 (0)