@@ -50,7 +50,7 @@ function isWindowsDeviceRoot(code) {
50
50
}
51
51
52
52
// Resolves . and .. elements in a path with directory names
53
- function normalizeStringWin32 ( path , allowAboveRoot ) {
53
+ function normalizeString ( path , allowAboveRoot , separator ) {
54
54
var res = '' ;
55
55
var lastSegmentLength = 0 ;
56
56
var lastSlash = - 1 ;
@@ -72,14 +72,14 @@ function normalizeStringWin32(path, allowAboveRoot) {
72
72
res . charCodeAt ( res . length - 1 ) !== CHAR_DOT ||
73
73
res . charCodeAt ( res . length - 2 ) !== CHAR_DOT ) {
74
74
if ( res . length > 2 ) {
75
- const lastSlashIndex = res . lastIndexOf ( '\\' ) ;
75
+ const lastSlashIndex = res . lastIndexOf ( separator ) ;
76
76
if ( lastSlashIndex !== res . length - 1 ) {
77
77
if ( lastSlashIndex === - 1 ) {
78
78
res = '' ;
79
79
lastSegmentLength = 0 ;
80
80
} else {
81
81
res = res . slice ( 0 , lastSlashIndex ) ;
82
- lastSegmentLength = res . length - 1 - res . lastIndexOf ( '\\' ) ;
82
+ lastSegmentLength = res . length - 1 - res . lastIndexOf ( separator ) ;
83
83
}
84
84
lastSlash = i ;
85
85
dots = 0 ;
@@ -95,82 +95,14 @@ function normalizeStringWin32(path, allowAboveRoot) {
95
95
}
96
96
if ( allowAboveRoot ) {
97
97
if ( res . length > 0 )
98
- res += '\\..' ;
98
+ res += ` ${ separator } ..` ;
99
99
else
100
100
res = '..' ;
101
101
lastSegmentLength = 2 ;
102
102
}
103
103
} else {
104
104
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 ) ;
174
106
else
175
107
res = path . slice ( lastSlash + 1 , i ) ;
176
108
lastSegmentLength = i - lastSlash - 1 ;
@@ -340,7 +272,7 @@ const win32 = {
340
272
// fails)
341
273
342
274
// Normalize the tail path
343
- resolvedTail = normalizeStringWin32 ( resolvedTail , ! resolvedAbsolute ) ;
275
+ resolvedTail = normalizeString ( resolvedTail , ! resolvedAbsolute , '\\' ) ;
344
276
345
277
return ( resolvedDevice + ( resolvedAbsolute ? '\\' : '' ) + resolvedTail ) ||
346
278
'.' ;
@@ -432,7 +364,7 @@ const win32 = {
432
364
433
365
var tail ;
434
366
if ( rootEnd < len )
435
- tail = normalizeStringWin32 ( path . slice ( rootEnd ) , ! isAbsolute ) ;
367
+ tail = normalizeString ( path . slice ( rootEnd ) , ! isAbsolute , '\\' ) ;
436
368
else
437
369
tail = '' ;
438
370
if ( tail . length === 0 && ! isAbsolute )
@@ -1164,7 +1096,7 @@ const posix = {
1164
1096
// handle relative paths to be safe (might happen when process.cwd() fails)
1165
1097
1166
1098
// Normalize the path
1167
- resolvedPath = normalizeStringPosix ( resolvedPath , ! resolvedAbsolute ) ;
1099
+ resolvedPath = normalizeString ( resolvedPath , ! resolvedAbsolute , '/' ) ;
1168
1100
1169
1101
if ( resolvedAbsolute ) {
1170
1102
if ( resolvedPath . length > 0 )
@@ -1190,7 +1122,7 @@ const posix = {
1190
1122
path . charCodeAt ( path . length - 1 ) === CHAR_FORWARD_SLASH ;
1191
1123
1192
1124
// Normalize the path
1193
- path = normalizeStringPosix ( path , ! isAbsolute ) ;
1125
+ path = normalizeString ( path , ! isAbsolute , '/' ) ;
1194
1126
1195
1127
if ( path . length === 0 && ! isAbsolute )
1196
1128
path = '.' ;
0 commit comments