From fd8de13bbec3e18bcb937a9be0901ab2a332378e Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 26 Mar 2019 07:18:17 +0100 Subject: [PATCH] path: refactor for less indentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This just switches the statements in a way that it reduces the overall indentation. The function has a very deep indentation in general and this should improve the readability. PR-URL: https://github.com/nodejs/node/pull/26917 Reviewed-By: Michaƫl Zasso Reviewed-By: Matteo Collina Signed-off-by: Beth Griggs --- lib/path.js | 86 ++++++++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/lib/path.js b/lib/path.js index 3bfb746251b05b..e376afae7c2daa 100644 --- a/lib/path.js +++ b/lib/path.js @@ -165,63 +165,63 @@ const win32 = { const code = path.charCodeAt(0); // Try to match a root - if (len > 1) { + if (len === 1) { if (isPathSeparator(code)) { - // Possible UNC root - - // If we started with a separator, we know we at least have an - // absolute path of some kind (UNC or otherwise) + // `path` contains just a path separator + rootEnd = 1; isAbsolute = true; + } + } else if (isPathSeparator(code)) { + // Possible UNC root - if (isPathSeparator(path.charCodeAt(1))) { - // Matched double path separator at beginning - let j = 2; - let last = j; - // Match 1 or more non-path separators - while (j < len && !isPathSeparator(path.charCodeAt(j))) { + // If we started with a separator, we know we at least have an + // absolute path of some kind (UNC or otherwise) + isAbsolute = true; + + if (isPathSeparator(path.charCodeAt(1))) { + // Matched double path separator at beginning + let j = 2; + let last = j; + // Match 1 or more non-path separators + while (j < len && !isPathSeparator(path.charCodeAt(j))) { + j++; + } + if (j < len && j !== last) { + const firstPart = path.slice(last, j); + // Matched! + last = j; + // Match 1 or more path separators + while (j < len && isPathSeparator(path.charCodeAt(j))) { j++; } if (j < len && j !== last) { - const firstPart = path.slice(last, j); // Matched! last = j; - // Match 1 or more path separators - while (j < len && isPathSeparator(path.charCodeAt(j))) { + // Match 1 or more non-path separators + while (j < len && !isPathSeparator(path.charCodeAt(j))) { j++; } - if (j < len && j !== last) { - // Matched! - last = j; - // Match 1 or more non-path separators - while (j < len && !isPathSeparator(path.charCodeAt(j))) { - j++; - } - if (j === len || j !== last) { - // We matched a UNC root - device = `\\\\${firstPart}\\${path.slice(last, j)}`; - rootEnd = j; - } + if (j === len || j !== last) { + // We matched a UNC root + device = `\\\\${firstPart}\\${path.slice(last, j)}`; + rootEnd = j; } } - } else { - rootEnd = 1; - } - } else if (isWindowsDeviceRoot(code) && - path.charCodeAt(1) === CHAR_COLON) { - // Possible device root - device = path.slice(0, 2); - rootEnd = 2; - if (len > 2 && isPathSeparator(path.charCodeAt(2))) { - // Treat separator following drive name as an absolute path - // indicator - isAbsolute = true; - rootEnd = 3; } + } else { + rootEnd = 1; + } + } else if (isWindowsDeviceRoot(code) && + path.charCodeAt(1) === CHAR_COLON) { + // Possible device root + device = path.slice(0, 2); + rootEnd = 2; + if (len > 2 && isPathSeparator(path.charCodeAt(2))) { + // Treat separator following drive name as an absolute path + // indicator + isAbsolute = true; + rootEnd = 3; } - } else if (isPathSeparator(code)) { - // `path` contains just a path separator - rootEnd = 1; - isAbsolute = true; } if (device.length > 0) {