Skip to content

Commit

Permalink
path: remove redundant function
Browse files Browse the repository at this point in the history
PR-URL: #19237
Reviewed-By: Weijia Wang <starkwang@126.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
daynin authored and MylesBorins committed Mar 20, 2018
1 parent 3f7c4ee commit a9bd8bf
Showing 1 changed file with 9 additions and 77 deletions.
86 changes: 9 additions & 77 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -340,7 +272,7 @@ const win32 = {
// fails)

// Normalize the tail path
resolvedTail = normalizeStringWin32(resolvedTail, !resolvedAbsolute);
resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\');

return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) ||
'.';
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1164,7 +1096,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)
Expand All @@ -1190,7 +1122,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 = '.';
Expand Down

0 comments on commit a9bd8bf

Please sign in to comment.