Skip to content

Commit 4ae320f

Browse files
dayninstarkwang
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 fa85947 commit 4ae320f

1 file changed

Lines changed: 9 additions & 77 deletions

File tree

‎lib/path.js‎

Lines changed: 9 additions & 77 deletions
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-
functionnormalizeStringWin32(path,allowAboveRoot){
53+
functionnormalizeString(path,allowAboveRoot,separator){
5454
varres='';
5555
varlastSegmentLength=0;
5656
varlastSlash=-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-
constlastSlashIndex=res.lastIndexOf('\\');
75+
constlastSlashIndex=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-
}elseif(code===CHAR_DOT&&dots!==-1){
113-
++dots;
114-
}else{
115-
dots=-1;
116-
}
117-
}
118-
returnres;
119-
}
120-
121-
// Resolves . and .. elements in a path with directory names
122-
functionnormalizeStringPosix(path,allowAboveRoot){
123-
varres='';
124-
varlastSegmentLength=0;
125-
varlastSlash=-1;
126-
vardots=0;
127-
varcode;
128-
for(vari=0;i<=path.length;++i){
129-
if(i<path.length)
130-
code=path.charCodeAt(i);
131-
elseif(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-
}elseif(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-
constlastSlashIndex=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-
}elseif(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
vartail;
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)
@@ -1163,7 +1095,7 @@ const posix = {
11631095
// handle relative paths to be safe (might happen when process.cwd() fails)
11641096

11651097
// Normalize the path
1166-
resolvedPath=normalizeStringPosix(resolvedPath,!resolvedAbsolute);
1098+
resolvedPath=normalizeString(resolvedPath,!resolvedAbsolute,'/');
11671099

11681100
if(resolvedAbsolute){
11691101
if(resolvedPath.length>0)
@@ -1189,7 +1121,7 @@ const posix = {
11891121
path.charCodeAt(path.length-1)===CHAR_FORWARD_SLASH;
11901122

11911123
// Normalize the path
1192-
path=normalizeStringPosix(path,!isAbsolute);
1124+
path=normalizeString(path,!isAbsolute,'/');
11931125

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

0 commit comments

Comments
 (0)