Skip to content

Commit 5b8ac58

Browse files
BridgeARaddaleax
authored andcommitted
path: refactor code for clarity
This moves a condition inside of a for loop which can only be triggered at the very end of the for loop outside of the loop. That way the for loop itself is much simpler and easier to understand and the code itself is less indented which should increase the readability. It also refactors some `var` to `let` and `const`. PR-URL: #25278 Reviewed-By: Michaël Zasso <[email protected]>
1 parent 348f1fb commit 5b8ac58

File tree

1 file changed

+63
-65
lines changed

1 file changed

+63
-65
lines changed

lib/path.js

+63-65
Original file line numberDiff line numberDiff line change
@@ -478,47 +478,46 @@ const win32 = {
478478
const toLen = toEnd - toStart;
479479

480480
// Compare paths to find the longest common path from root
481-
var length = (fromLen < toLen ? fromLen : toLen);
482-
var lastCommonSep = -1;
483-
var i = 0;
484-
for (; i <= length; ++i) {
485-
if (i === length) {
486-
if (toLen > length) {
487-
if (to.charCodeAt(toStart + i) === CHAR_BACKWARD_SLASH) {
488-
// We get here if `from` is the exact base path for `to`.
489-
// For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
490-
return toOrig.slice(toStart + i + 1);
491-
} else if (i === 2) {
492-
// We get here if `from` is the device root.
493-
// For example: from='C:\\'; to='C:\\foo'
494-
return toOrig.slice(toStart + i);
495-
}
496-
}
497-
if (fromLen > length) {
498-
if (from.charCodeAt(fromStart + i) === CHAR_BACKWARD_SLASH) {
499-
// We get here if `to` is the exact base path for `from`.
500-
// For example: from='C:\\foo\\bar'; to='C:\\foo'
501-
lastCommonSep = i;
502-
} else if (i === 2) {
503-
// We get here if `to` is the device root.
504-
// For example: from='C:\\foo\\bar'; to='C:\\'
505-
lastCommonSep = 3;
506-
}
507-
}
508-
break;
509-
}
510-
var fromCode = from.charCodeAt(fromStart + i);
511-
var toCode = to.charCodeAt(toStart + i);
512-
if (fromCode !== toCode)
481+
const length = fromLen < toLen ? fromLen : toLen;
482+
let lastCommonSep = -1;
483+
let i = 0;
484+
for (; i < length; i++) {
485+
const fromCode = from.charCodeAt(fromStart + i);
486+
if (fromCode !== to.charCodeAt(toStart + i))
513487
break;
514488
else if (fromCode === CHAR_BACKWARD_SLASH)
515489
lastCommonSep = i;
516490
}
517491

518492
// We found a mismatch before the first common path separator was seen, so
519493
// return the original `to`.
520-
if (i !== length && lastCommonSep === -1) {
521-
return toOrig;
494+
if (i !== length) {
495+
if (lastCommonSep === -1)
496+
return toOrig;
497+
} else {
498+
if (toLen > length) {
499+
if (to.charCodeAt(toStart + i) === CHAR_BACKWARD_SLASH) {
500+
// We get here if `from` is the exact base path for `to`.
501+
// For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
502+
return toOrig.slice(toStart + i + 1);
503+
}
504+
if (i === 2) {
505+
// We get here if `from` is the device root.
506+
// For example: from='C:\\'; to='C:\\foo'
507+
return toOrig.slice(toStart + i);
508+
}
509+
}
510+
if (fromLen > length) {
511+
if (from.charCodeAt(fromStart + i) === CHAR_BACKWARD_SLASH) {
512+
// We get here if `to` is the exact base path for `from`.
513+
// For example: from='C:\\foo\\bar'; to='C:\\foo'
514+
lastCommonSep = i;
515+
} else if (i === 2) {
516+
// We get here if `to` is the device root.
517+
// For example: from='C:\\foo\\bar'; to='C:\\'
518+
lastCommonSep = 3;
519+
}
520+
}
522521
}
523522

524523
let out = '';
@@ -1079,41 +1078,40 @@ const posix = {
10791078
const toLen = (toEnd - toStart);
10801079

10811080
// Compare paths to find the longest common path from root
1082-
var length = (fromLen < toLen ? fromLen : toLen);
1083-
var lastCommonSep = -1;
1084-
var i = 0;
1085-
for (; i <= length; ++i) {
1086-
if (i === length) {
1087-
if (toLen > length) {
1088-
if (to.charCodeAt(toStart + i) === CHAR_FORWARD_SLASH) {
1089-
// We get here if `from` is the exact base path for `to`.
1090-
// For example: from='/foo/bar'; to='/foo/bar/baz'
1091-
return to.slice(toStart + i + 1);
1092-
} else if (i === 0) {
1093-
// We get here if `from` is the root
1094-
// For example: from='/'; to='/foo'
1095-
return to.slice(toStart + i);
1096-
}
1097-
} else if (fromLen > length) {
1098-
if (from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) {
1099-
// We get here if `to` is the exact base path for `from`.
1100-
// For example: from='/foo/bar/baz'; to='/foo/bar'
1101-
lastCommonSep = i;
1102-
} else if (i === 0) {
1103-
// We get here if `to` is the root.
1104-
// For example: from='/foo'; to='/'
1105-
lastCommonSep = 0;
1106-
}
1107-
}
1108-
break;
1109-
}
1110-
var fromCode = from.charCodeAt(fromStart + i);
1111-
var toCode = to.charCodeAt(toStart + i);
1112-
if (fromCode !== toCode)
1081+
const length = (fromLen < toLen ? fromLen : toLen);
1082+
let lastCommonSep = -1;
1083+
let i = 0;
1084+
for (; i < length; i++) {
1085+
const fromCode = from.charCodeAt(fromStart + i);
1086+
if (fromCode !== to.charCodeAt(toStart + i))
11131087
break;
11141088
else if (fromCode === CHAR_FORWARD_SLASH)
11151089
lastCommonSep = i;
11161090
}
1091+
if (i === length) {
1092+
if (toLen > length) {
1093+
if (to.charCodeAt(toStart + i) === CHAR_FORWARD_SLASH) {
1094+
// We get here if `from` is the exact base path for `to`.
1095+
// For example: from='/foo/bar'; to='/foo/bar/baz'
1096+
return to.slice(toStart + i + 1);
1097+
}
1098+
if (i === 0) {
1099+
// We get here if `from` is the root
1100+
// For example: from='/'; to='/foo'
1101+
return to.slice(toStart + i);
1102+
}
1103+
} else if (fromLen > length) {
1104+
if (from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) {
1105+
// We get here if `to` is the exact base path for `from`.
1106+
// For example: from='/foo/bar/baz'; to='/foo/bar'
1107+
lastCommonSep = i;
1108+
} else if (i === 0) {
1109+
// We get here if `to` is the root.
1110+
// For example: from='/foo'; to='/'
1111+
lastCommonSep = 0;
1112+
}
1113+
}
1114+
}
11171115

11181116
var out = '';
11191117
// Generate the relative path based on the path difference between `to`

0 commit comments

Comments
 (0)