@@ -110,6 +110,9 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
110
110
}
111
111
112
112
function _format ( sep , pathObject ) {
113
+ if ( pathObject === null || typeof pathObject !== 'object' ) {
114
+ throw new ERR_INVALID_ARG_TYPE ( 'pathObject' , 'Object' , pathObject ) ;
115
+ }
113
116
const dir = pathObject . dir || pathObject . root ;
114
117
const base = pathObject . base ||
115
118
`${ pathObject . name || '' } ${ pathObject . ext || '' } ` ;
@@ -121,16 +124,22 @@ function _format(sep, pathObject) {
121
124
122
125
const win32 = {
123
126
// path.resolve([from ...], to)
124
- resolve : function resolve ( ) {
125
- var resolvedDevice = '' ;
126
- var resolvedTail = '' ;
127
- var resolvedAbsolute = false ;
127
+ resolve ( ... args ) {
128
+ let resolvedDevice = '' ;
129
+ let resolvedTail = '' ;
130
+ let resolvedAbsolute = false ;
128
131
129
- for ( var i = arguments . length - 1 ; i >= - 1 ; i -- ) {
130
- var path ;
132
+ for ( var i = args . length - 1 ; i >= - 1 ; i -- ) {
133
+ let path ;
131
134
if ( i >= 0 ) {
132
- path = arguments [ i ] ;
133
- } else if ( ! resolvedDevice ) {
135
+ path = args [ i ] ;
136
+ validateString ( path , 'path' ) ;
137
+
138
+ // Skip empty entries
139
+ if ( path . length === 0 ) {
140
+ continue ;
141
+ }
142
+ } else if ( resolvedDevice . length === 0 ) {
134
143
path = process . cwd ( ) ;
135
144
} else {
136
145
// Windows has the concept of drive-specific current working
@@ -149,17 +158,10 @@ const win32 = {
149
158
}
150
159
}
151
160
152
- validateString ( path , 'path' ) ;
153
-
154
- // Skip empty entries
155
- if ( path . length === 0 ) {
156
- continue ;
157
- }
158
-
159
- var len = path . length ;
160
- var rootEnd = 0 ;
161
- var device = '' ;
162
- var isAbsolute = false ;
161
+ const len = path . length ;
162
+ let rootEnd = 0 ;
163
+ let device = '' ;
164
+ let isAbsolute = false ;
163
165
const code = path . charCodeAt ( 0 ) ;
164
166
165
167
// Try to match a root
@@ -409,16 +411,14 @@ const win32 = {
409
411
if ( isPathSeparator ( firstPart . charCodeAt ( 0 ) ) ) {
410
412
++ slashCount ;
411
413
const firstLen = firstPart . length ;
412
- if ( firstLen > 1 ) {
413
- if ( isPathSeparator ( firstPart . charCodeAt ( 1 ) ) ) {
414
- ++ slashCount ;
415
- if ( firstLen > 2 ) {
416
- if ( isPathSeparator ( firstPart . charCodeAt ( 2 ) ) )
417
- ++ slashCount ;
418
- else {
419
- // We matched a UNC path in the first part
420
- needsReplace = false ;
421
- }
414
+ if ( firstLen > 1 && isPathSeparator ( firstPart . charCodeAt ( 1 ) ) ) {
415
+ ++ slashCount ;
416
+ if ( firstLen > 2 ) {
417
+ if ( isPathSeparator ( firstPart . charCodeAt ( 2 ) ) )
418
+ ++ slashCount ;
419
+ else {
420
+ // We matched a UNC path in the first part
421
+ needsReplace = false ;
422
422
}
423
423
}
424
424
}
@@ -699,16 +699,14 @@ const win32 = {
699
699
// Check for a drive letter prefix so as not to mistake the following
700
700
// path separator as an extra separator at the end of the path that can be
701
701
// disregarded
702
- if ( path . length >= 2 ) {
703
- const drive = path . charCodeAt ( 0 ) ;
704
- if ( isWindowsDeviceRoot ( drive ) ) {
705
- if ( path . charCodeAt ( 1 ) === CHAR_COLON )
706
- start = 2 ;
707
- }
702
+ if ( path . length >= 2 &&
703
+ isWindowsDeviceRoot ( path . charCodeAt ( 0 ) ) &&
704
+ path . charCodeAt ( 1 ) === CHAR_COLON ) {
705
+ start = 2 ;
708
706
}
709
707
710
708
if ( ext !== undefined && ext . length > 0 && ext . length <= path . length ) {
711
- if ( ext . length === path . length && ext === path )
709
+ if ( ext === path )
712
710
return '' ;
713
711
var extIdx = ext . length - 1 ;
714
712
var firstNonSlashEnd = - 1 ;
@@ -839,16 +837,9 @@ const win32 = {
839
837
return path . slice ( startDot , end ) ;
840
838
} ,
841
839
840
+ format : _format . bind ( null , '\\' ) ,
842
841
843
- format : function format ( pathObject ) {
844
- if ( pathObject === null || typeof pathObject !== 'object' ) {
845
- throw new ERR_INVALID_ARG_TYPE ( 'pathObject' , 'Object' , pathObject ) ;
846
- }
847
- return _format ( '\\' , pathObject ) ;
848
- } ,
849
-
850
-
851
- parse : function parse ( path ) {
842
+ parse ( path ) {
852
843
validateString ( path , 'path' ) ;
853
844
854
845
const ret = { root : '' , dir : '' , base : '' , ext : '' , name : '' } ;
@@ -1056,9 +1047,12 @@ const posix = {
1056
1047
// Normalize the path
1057
1048
path = normalizeString ( path , ! isAbsolute , '/' , isPosixPathSeparator ) ;
1058
1049
1059
- if ( path . length === 0 && ! isAbsolute )
1060
- path = '.' ;
1061
- if ( path . length > 0 && trailingSeparator )
1050
+ if ( path . length === 0 ) {
1051
+ if ( isAbsolute )
1052
+ return '/' ;
1053
+ return trailingSeparator ? './' : '.' ;
1054
+ }
1055
+ if ( trailingSeparator )
1062
1056
path += '/' ;
1063
1057
1064
1058
return isAbsolute ? `/${ path } ` : path ;
@@ -1219,7 +1213,7 @@ const posix = {
1219
1213
var i ;
1220
1214
1221
1215
if ( ext !== undefined && ext . length > 0 && ext . length <= path . length ) {
1222
- if ( ext . length === path . length && ext === path )
1216
+ if ( ext === path )
1223
1217
return '' ;
1224
1218
var extIdx = ext . length - 1 ;
1225
1219
var firstNonSlashEnd = - 1 ;
@@ -1338,16 +1332,9 @@ const posix = {
1338
1332
return path . slice ( startDot , end ) ;
1339
1333
} ,
1340
1334
1335
+ format : _format . bind ( null , '/' ) ,
1341
1336
1342
- format : function format ( pathObject ) {
1343
- if ( pathObject === null || typeof pathObject !== 'object' ) {
1344
- throw new ERR_INVALID_ARG_TYPE ( 'pathObject' , 'Object' , pathObject ) ;
1345
- }
1346
- return _format ( '/' , pathObject ) ;
1347
- } ,
1348
-
1349
-
1350
- parse : function parse ( path ) {
1337
+ parse ( path ) {
1351
1338
validateString ( path , 'path' ) ;
1352
1339
1353
1340
const ret = { root : '' , dir : '' , base : '' , ext : '' , name : '' } ;
0 commit comments