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: https://github.com/nodejs/node/pull/25278 Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
parent
7cbe29eb4d
commit
0fd5b458be
64
lib/path.js
64
lib/path.js
@ -478,17 +478,30 @@ const win32 = {
|
|||||||
const 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);
|
const length = fromLen < toLen ? fromLen : toLen;
|
||||||
var lastCommonSep = -1;
|
let lastCommonSep = -1;
|
||||||
var i = 0;
|
let i = 0;
|
||||||
for (; i <= length; ++i) {
|
for (; i < length; i++) {
|
||||||
if (i === length) {
|
const fromCode = from.charCodeAt(fromStart + i);
|
||||||
|
if (fromCode !== to.charCodeAt(toStart + i))
|
||||||
|
break;
|
||||||
|
else if (fromCode === CHAR_BACKWARD_SLASH)
|
||||||
|
lastCommonSep = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We found a mismatch before the first common path separator was seen, so
|
||||||
|
// return the original `to`.
|
||||||
|
if (i !== length) {
|
||||||
|
if (lastCommonSep === -1)
|
||||||
|
return toOrig;
|
||||||
|
} else {
|
||||||
if (toLen > length) {
|
if (toLen > length) {
|
||||||
if (to.charCodeAt(toStart + i) === CHAR_BACKWARD_SLASH) {
|
if (to.charCodeAt(toStart + i) === CHAR_BACKWARD_SLASH) {
|
||||||
// We get here if `from` is the exact base path for `to`.
|
// We get here if `from` is the exact base path for `to`.
|
||||||
// For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
|
// For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
|
||||||
return toOrig.slice(toStart + i + 1);
|
return toOrig.slice(toStart + i + 1);
|
||||||
} else if (i === 2) {
|
}
|
||||||
|
if (i === 2) {
|
||||||
// We get here if `from` is the device root.
|
// We get here if `from` is the device root.
|
||||||
// For example: from='C:\\'; to='C:\\foo'
|
// For example: from='C:\\'; to='C:\\foo'
|
||||||
return toOrig.slice(toStart + i);
|
return toOrig.slice(toStart + i);
|
||||||
@ -505,20 +518,6 @@ const win32 = {
|
|||||||
lastCommonSep = 3;
|
lastCommonSep = 3;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
|
||||||
var fromCode = from.charCodeAt(fromStart + i);
|
|
||||||
var toCode = to.charCodeAt(toStart + i);
|
|
||||||
if (fromCode !== toCode)
|
|
||||||
break;
|
|
||||||
else if (fromCode === CHAR_BACKWARD_SLASH)
|
|
||||||
lastCommonSep = i;
|
|
||||||
}
|
|
||||||
|
|
||||||
// We found a mismatch before the first common path separator was seen, so
|
|
||||||
// return the original `to`.
|
|
||||||
if (i !== length && lastCommonSep === -1) {
|
|
||||||
return toOrig;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let out = '';
|
let out = '';
|
||||||
@ -1079,17 +1078,24 @@ const posix = {
|
|||||||
const 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);
|
const length = (fromLen < toLen ? fromLen : toLen);
|
||||||
var lastCommonSep = -1;
|
let lastCommonSep = -1;
|
||||||
var i = 0;
|
let i = 0;
|
||||||
for (; i <= length; ++i) {
|
for (; i < length; i++) {
|
||||||
|
const fromCode = from.charCodeAt(fromStart + i);
|
||||||
|
if (fromCode !== to.charCodeAt(toStart + i))
|
||||||
|
break;
|
||||||
|
else if (fromCode === CHAR_FORWARD_SLASH)
|
||||||
|
lastCommonSep = i;
|
||||||
|
}
|
||||||
if (i === length) {
|
if (i === length) {
|
||||||
if (toLen > length) {
|
if (toLen > length) {
|
||||||
if (to.charCodeAt(toStart + i) === CHAR_FORWARD_SLASH) {
|
if (to.charCodeAt(toStart + i) === CHAR_FORWARD_SLASH) {
|
||||||
// We get here if `from` is the exact base path for `to`.
|
// We get here if `from` is the exact base path for `to`.
|
||||||
// For example: from='/foo/bar'; to='/foo/bar/baz'
|
// For example: from='/foo/bar'; to='/foo/bar/baz'
|
||||||
return to.slice(toStart + i + 1);
|
return to.slice(toStart + i + 1);
|
||||||
} else if (i === 0) {
|
}
|
||||||
|
if (i === 0) {
|
||||||
// We get here if `from` is the root
|
// We get here if `from` is the root
|
||||||
// For example: from='/'; to='/foo'
|
// For example: from='/'; to='/foo'
|
||||||
return to.slice(toStart + i);
|
return to.slice(toStart + i);
|
||||||
@ -1105,14 +1111,6 @@ const posix = {
|
|||||||
lastCommonSep = 0;
|
lastCommonSep = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
|
||||||
var fromCode = from.charCodeAt(fromStart + i);
|
|
||||||
var toCode = to.charCodeAt(toStart + i);
|
|
||||||
if (fromCode !== toCode)
|
|
||||||
break;
|
|
||||||
else if (fromCode === CHAR_FORWARD_SLASH)
|
|
||||||
lastCommonSep = i;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var out = '';
|
var out = '';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user