Skip to content

Commit a9bd8bf

Browse files
dayninMylesBorins
authored andcommitted
path: remove redundant function
PR-URL: #19237 Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent 3f7c4ee commit a9bd8bf

File tree

1 file changed

+9
-77
lines changed

1 file changed

+9
-77
lines changed

lib/path.js

+9-77
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function isWindowsDeviceRoot(code) {
5050
}
5151

5252
// Resolves . and .. elements in a path with directory names
53-
function normalizeStringWin32(path, allowAboveRoot) {
53+
function normalizeString(path, allowAboveRoot, separator) {
5454
var res = '';
5555
var lastSegmentLength = 0;
5656
var lastSlash = -1;
@@ -72,14 +72,14 @@ function normalizeStringWin32(path, allowAboveRoot) {
7272
res.charCodeAt(res.length - 1) !== CHAR_DOT ||
7373
res.charCodeAt(res.length - 2) !== CHAR_DOT) {
7474
if (res.length > 2) {
75-
const lastSlashIndex = res.lastIndexOf('\\');
75+
const lastSlashIndex = res.lastIndexOf(separator);
7676
if (lastSlashIndex !== res.length - 1) {
7777
if (lastSlashIndex === -1) {
7878
res = '';
7979
lastSegmentLength = 0;
8080
} else {
8181
res = res.slice(0, lastSlashIndex);
82-
lastSegmentLength = res.length - 1 - res.lastIndexOf('\\');
82+
lastSegmentLength = res.length - 1 - res.lastIndexOf(separator);
8383
}
8484
lastSlash = i;
8585
dots = 0;
@@ -95,82 +95,14 @@ function normalizeStringWin32(path, allowAboveRoot) {
9595
}
9696
if (allowAboveRoot) {
9797
if (res.length > 0)
98-
res += '\\..';
98+
res += `${separator}..`;
9999
else
100100
res = '..';
101101
lastSegmentLength = 2;
102102
}
103103
} else {
104104
if (res.length > 0)
105-
res += '\\' + path.slice(lastSlash + 1, i);
106-
else
107-
res = path.slice(lastSlash + 1, i);
108-
lastSegmentLength = i - lastSlash - 1;
109-
}
110-
lastSlash = i;
111-
dots = 0;
112-
} else if (code === CHAR_DOT && dots !== -1) {
113-
++dots;
114-
} else {
115-
dots = -1;
116-
}
117-
}
118-
return res;
119-
}
120-
121-
// Resolves . and .. elements in a path with directory names
122-
function normalizeStringPosix(path, allowAboveRoot) {
123-
var res = '';
124-
var lastSegmentLength = 0;
125-
var lastSlash = -1;
126-
var dots = 0;
127-
var code;
128-
for (var i = 0; i <= path.length; ++i) {
129-
if (i < path.length)
130-
code = path.charCodeAt(i);
131-
else if (code === CHAR_FORWARD_SLASH)
132-
break;
133-
else
134-
code = CHAR_FORWARD_SLASH;
135-
if (code === CHAR_FORWARD_SLASH) {
136-
if (lastSlash === i - 1 || dots === 1) {
137-
// NOOP
138-
} else if (lastSlash !== i - 1 && dots === 2) {
139-
if (res.length < 2 || lastSegmentLength !== 2 ||
140-
res.charCodeAt(res.length - 1) !== CHAR_DOT ||
141-
res.charCodeAt(res.length - 2) !== CHAR_DOT) {
142-
if (res.length > 2) {
143-
const lastSlashIndex = res.lastIndexOf('/');
144-
if (lastSlashIndex !== res.length - 1) {
145-
if (lastSlashIndex === -1) {
146-
res = '';
147-
lastSegmentLength = 0;
148-
} else {
149-
res = res.slice(0, lastSlashIndex);
150-
lastSegmentLength = res.length - 1 - res.lastIndexOf('/');
151-
}
152-
lastSlash = i;
153-
dots = 0;
154-
continue;
155-
}
156-
} else if (res.length === 2 || res.length === 1) {
157-
res = '';
158-
lastSegmentLength = 0;
159-
lastSlash = i;
160-
dots = 0;
161-
continue;
162-
}
163-
}
164-
if (allowAboveRoot) {
165-
if (res.length > 0)
166-
res += '/..';
167-
else
168-
res = '..';
169-
lastSegmentLength = 2;
170-
}
171-
} else {
172-
if (res.length > 0)
173-
res += '/' + path.slice(lastSlash + 1, i);
105+
res += separator + path.slice(lastSlash + 1, i);
174106
else
175107
res = path.slice(lastSlash + 1, i);
176108
lastSegmentLength = i - lastSlash - 1;
@@ -340,7 +272,7 @@ const win32 = {
340272
// fails)
341273

342274
// Normalize the tail path
343-
resolvedTail = normalizeStringWin32(resolvedTail, !resolvedAbsolute);
275+
resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\');
344276

345277
return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) ||
346278
'.';
@@ -432,7 +364,7 @@ const win32 = {
432364

433365
var tail;
434366
if (rootEnd < len)
435-
tail = normalizeStringWin32(path.slice(rootEnd), !isAbsolute);
367+
tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\');
436368
else
437369
tail = '';
438370
if (tail.length === 0 && !isAbsolute)
@@ -1164,7 +1096,7 @@ const posix = {
11641096
// handle relative paths to be safe (might happen when process.cwd() fails)
11651097

11661098
// Normalize the path
1167-
resolvedPath = normalizeStringPosix(resolvedPath, !resolvedAbsolute);
1099+
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/');
11681100

11691101
if (resolvedAbsolute) {
11701102
if (resolvedPath.length > 0)
@@ -1190,7 +1122,7 @@ const posix = {
11901122
path.charCodeAt(path.length - 1) === CHAR_FORWARD_SLASH;
11911123

11921124
// Normalize the path
1193-
path = normalizeStringPosix(path, !isAbsolute);
1125+
path = normalizeString(path, !isAbsolute, '/');
11941126

11951127
if (path.length === 0 && !isAbsolute)
11961128
path = '.';

0 commit comments

Comments
 (0)