Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2019-01-27 18:07:00 +0300
committerRuben Bridgewater <ruben@bridgewater.de>2019-03-01 16:55:33 +0300
commit0fd5b458bec739eb8b4cea4dc362a36eb3e61fde (patch)
treec1787778b30a50ad28d1da8e6e1ed1c769e05694 /lib/path.js
parent7cbe29eb4de65e8168f90c994d855486e46dc048 (diff)
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>
Diffstat (limited to 'lib/path.js')
-rw-r--r--lib/path.js128
1 files changed, 63 insertions, 65 deletions
diff --git a/lib/path.js b/lib/path.js
index 64cd5eee92c..6ac65cfe4a7 100644
--- a/lib/path.js
+++ b/lib/path.js
@@ -478,38 +478,12 @@ const win32 = {
const toLen = toEnd - toStart;
// Compare paths to find the longest common path from root
- var length = (fromLen < toLen ? fromLen : toLen);
- var lastCommonSep = -1;
- var i = 0;
- for (; i <= length; ++i) {
- if (i === length) {
- if (toLen > length) {
- if (to.charCodeAt(toStart + i) === CHAR_BACKWARD_SLASH) {
- // We get here if `from` is the exact base path for `to`.
- // For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
- return toOrig.slice(toStart + i + 1);
- } else if (i === 2) {
- // We get here if `from` is the device root.
- // For example: from='C:\\'; to='C:\\foo'
- return toOrig.slice(toStart + i);
- }
- }
- if (fromLen > length) {
- if (from.charCodeAt(fromStart + i) === CHAR_BACKWARD_SLASH) {
- // We get here if `to` is the exact base path for `from`.
- // For example: from='C:\\foo\\bar'; to='C:\\foo'
- lastCommonSep = i;
- } else if (i === 2) {
- // We get here if `to` is the device root.
- // For example: from='C:\\foo\\bar'; to='C:\\'
- lastCommonSep = 3;
- }
- }
- break;
- }
- var fromCode = from.charCodeAt(fromStart + i);
- var toCode = to.charCodeAt(toStart + i);
- if (fromCode !== toCode)
+ const length = fromLen < toLen ? fromLen : toLen;
+ let lastCommonSep = -1;
+ let i = 0;
+ for (; i < length; i++) {
+ const fromCode = from.charCodeAt(fromStart + i);
+ if (fromCode !== to.charCodeAt(toStart + i))
break;
else if (fromCode === CHAR_BACKWARD_SLASH)
lastCommonSep = i;
@@ -517,8 +491,33 @@ const win32 = {
// We found a mismatch before the first common path separator was seen, so
// return the original `to`.
- if (i !== length && lastCommonSep === -1) {
- return toOrig;
+ if (i !== length) {
+ if (lastCommonSep === -1)
+ return toOrig;
+ } else {
+ if (toLen > length) {
+ if (to.charCodeAt(toStart + i) === CHAR_BACKWARD_SLASH) {
+ // We get here if `from` is the exact base path for `to`.
+ // For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
+ return toOrig.slice(toStart + i + 1);
+ }
+ if (i === 2) {
+ // We get here if `from` is the device root.
+ // For example: from='C:\\'; to='C:\\foo'
+ return toOrig.slice(toStart + i);
+ }
+ }
+ if (fromLen > length) {
+ if (from.charCodeAt(fromStart + i) === CHAR_BACKWARD_SLASH) {
+ // We get here if `to` is the exact base path for `from`.
+ // For example: from='C:\\foo\\bar'; to='C:\\foo'
+ lastCommonSep = i;
+ } else if (i === 2) {
+ // We get here if `to` is the device root.
+ // For example: from='C:\\foo\\bar'; to='C:\\'
+ lastCommonSep = 3;
+ }
+ }
}
let out = '';
@@ -1079,41 +1078,40 @@ const posix = {
const toLen = (toEnd - toStart);
// Compare paths to find the longest common path from root
- var length = (fromLen < toLen ? fromLen : toLen);
- var lastCommonSep = -1;
- var i = 0;
- for (; i <= length; ++i) {
- if (i === length) {
- if (toLen > length) {
- if (to.charCodeAt(toStart + i) === CHAR_FORWARD_SLASH) {
- // We get here if `from` is the exact base path for `to`.
- // For example: from='/foo/bar'; to='/foo/bar/baz'
- return to.slice(toStart + i + 1);
- } else if (i === 0) {
- // We get here if `from` is the root
- // For example: from='/'; to='/foo'
- return to.slice(toStart + i);
- }
- } else if (fromLen > length) {
- if (from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) {
- // We get here if `to` is the exact base path for `from`.
- // For example: from='/foo/bar/baz'; to='/foo/bar'
- lastCommonSep = i;
- } else if (i === 0) {
- // We get here if `to` is the root.
- // For example: from='/foo'; to='/'
- lastCommonSep = 0;
- }
- }
- break;
- }
- var fromCode = from.charCodeAt(fromStart + i);
- var toCode = to.charCodeAt(toStart + i);
- if (fromCode !== toCode)
+ const length = (fromLen < toLen ? fromLen : toLen);
+ let lastCommonSep = -1;
+ let i = 0;
+ 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 (toLen > length) {
+ if (to.charCodeAt(toStart + i) === CHAR_FORWARD_SLASH) {
+ // We get here if `from` is the exact base path for `to`.
+ // For example: from='/foo/bar'; to='/foo/bar/baz'
+ return to.slice(toStart + i + 1);
+ }
+ if (i === 0) {
+ // We get here if `from` is the root
+ // For example: from='/'; to='/foo'
+ return to.slice(toStart + i);
+ }
+ } else if (fromLen > length) {
+ if (from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) {
+ // We get here if `to` is the exact base path for `from`.
+ // For example: from='/foo/bar/baz'; to='/foo/bar'
+ lastCommonSep = i;
+ } else if (i === 0) {
+ // We get here if `to` is the root.
+ // For example: from='/foo'; to='/'
+ lastCommonSep = 0;
+ }
+ }
+ }
var out = '';
// Generate the relative path based on the path difference between `to`