diff options
author | Sergey Golovin <golovim@gmail.com> | 2018-03-08 16:44:23 +0300 |
---|---|---|
committer | Weijia Wang <381152119@qq.com> | 2018-03-14 17:04:00 +0300 |
commit | 4ae320f2b3c745402955019d6a57a22ee2b8d3bd (patch) | |
tree | bd3bc7731b8164c0fe88fec5e4224646a920fd28 /lib/path.js | |
parent | fa8594779aa985c754a3defbad030e938c089ddf (diff) |
path: remove redundant function
PR-URL: https://github.com/nodejs/node/pull/19237
Reviewed-By: Weijia Wang <starkwang@126.com>
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Diffstat (limited to 'lib/path.js')
-rw-r--r-- | lib/path.js | 86 |
1 files changed, 9 insertions, 77 deletions
diff --git a/lib/path.js b/lib/path.js index 9f33cb87bc5..7a486885269 100644 --- a/lib/path.js +++ b/lib/path.js @@ -50,7 +50,7 @@ function isWindowsDeviceRoot(code) { } // Resolves . and .. elements in a path with directory names -function normalizeStringWin32(path, allowAboveRoot) { +function normalizeString(path, allowAboveRoot, separator) { var res = ''; var lastSegmentLength = 0; var lastSlash = -1; @@ -72,14 +72,14 @@ function normalizeStringWin32(path, allowAboveRoot) { res.charCodeAt(res.length - 1) !== CHAR_DOT || res.charCodeAt(res.length - 2) !== CHAR_DOT) { if (res.length > 2) { - const lastSlashIndex = res.lastIndexOf('\\'); + const lastSlashIndex = res.lastIndexOf(separator); if (lastSlashIndex !== res.length - 1) { if (lastSlashIndex === -1) { res = ''; lastSegmentLength = 0; } else { res = res.slice(0, lastSlashIndex); - lastSegmentLength = res.length - 1 - res.lastIndexOf('\\'); + lastSegmentLength = res.length - 1 - res.lastIndexOf(separator); } lastSlash = i; dots = 0; @@ -95,82 +95,14 @@ function normalizeStringWin32(path, allowAboveRoot) { } if (allowAboveRoot) { if (res.length > 0) - res += '\\..'; + res += `${separator}..`; else res = '..'; lastSegmentLength = 2; } } else { if (res.length > 0) - res += '\\' + path.slice(lastSlash + 1, i); - else - res = path.slice(lastSlash + 1, i); - lastSegmentLength = i - lastSlash - 1; - } - lastSlash = i; - dots = 0; - } else if (code === CHAR_DOT && dots !== -1) { - ++dots; - } else { - dots = -1; - } - } - return res; -} - -// Resolves . and .. elements in a path with directory names -function normalizeStringPosix(path, allowAboveRoot) { - var res = ''; - var lastSegmentLength = 0; - var lastSlash = -1; - var dots = 0; - var code; - for (var i = 0; i <= path.length; ++i) { - if (i < path.length) - code = path.charCodeAt(i); - else if (code === CHAR_FORWARD_SLASH) - break; - else - code = CHAR_FORWARD_SLASH; - if (code === CHAR_FORWARD_SLASH) { - if (lastSlash === i - 1 || dots === 1) { - // NOOP - } else if (lastSlash !== i - 1 && dots === 2) { - if (res.length < 2 || lastSegmentLength !== 2 || - res.charCodeAt(res.length - 1) !== CHAR_DOT || - res.charCodeAt(res.length - 2) !== CHAR_DOT) { - if (res.length > 2) { - const lastSlashIndex = res.lastIndexOf('/'); - if (lastSlashIndex !== res.length - 1) { - if (lastSlashIndex === -1) { - res = ''; - lastSegmentLength = 0; - } else { - res = res.slice(0, lastSlashIndex); - lastSegmentLength = res.length - 1 - res.lastIndexOf('/'); - } - lastSlash = i; - dots = 0; - continue; - } - } else if (res.length === 2 || res.length === 1) { - res = ''; - lastSegmentLength = 0; - lastSlash = i; - dots = 0; - continue; - } - } - if (allowAboveRoot) { - if (res.length > 0) - res += '/..'; - else - res = '..'; - lastSegmentLength = 2; - } - } else { - if (res.length > 0) - res += '/' + path.slice(lastSlash + 1, i); + res += separator + path.slice(lastSlash + 1, i); else res = path.slice(lastSlash + 1, i); lastSegmentLength = i - lastSlash - 1; @@ -340,7 +272,7 @@ const win32 = { // fails) // Normalize the tail path - resolvedTail = normalizeStringWin32(resolvedTail, !resolvedAbsolute); + resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\'); return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) || '.'; @@ -432,7 +364,7 @@ const win32 = { var tail; if (rootEnd < len) - tail = normalizeStringWin32(path.slice(rootEnd), !isAbsolute); + tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\'); else tail = ''; if (tail.length === 0 && !isAbsolute) @@ -1163,7 +1095,7 @@ const posix = { // handle relative paths to be safe (might happen when process.cwd() fails) // Normalize the path - resolvedPath = normalizeStringPosix(resolvedPath, !resolvedAbsolute); + resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/'); if (resolvedAbsolute) { if (resolvedPath.length > 0) @@ -1189,7 +1121,7 @@ const posix = { path.charCodeAt(path.length - 1) === CHAR_FORWARD_SLASH; // Normalize the path - path = normalizeStringPosix(path, !isAbsolute); + path = normalizeString(path, !isAbsolute, '/'); if (path.length === 0 && !isAbsolute) path = '.'; |