@@ -33,12 +33,7 @@ const {
33
33
CHAR_COLON ,
34
34
CHAR_QUESTION_MARK ,
35
35
} = require ( 'internal/constants' ) ;
36
-
37
- function assertPath ( path ) {
38
- if ( typeof path !== 'string' ) {
39
- throw new ERR_INVALID_ARG_TYPE ( 'path' , 'string' , path ) ;
40
- }
41
- }
36
+ const { validateString } = require ( 'internal/validators' ) ;
42
37
43
38
function isPathSeparator ( code ) {
44
39
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH ;
@@ -163,7 +158,7 @@ const win32 = {
163
158
}
164
159
}
165
160
166
- assertPath ( path ) ;
161
+ validateString ( path , 'path' ) ;
167
162
168
163
// Skip empty entries
169
164
if ( path . length === 0 ) {
@@ -282,7 +277,7 @@ const win32 = {
282
277
} ,
283
278
284
279
normalize : function normalize ( path ) {
285
- assertPath ( path ) ;
280
+ validateString ( path , 'path' ) ;
286
281
const len = path . length ;
287
282
if ( len === 0 )
288
283
return '.' ;
@@ -401,7 +396,7 @@ const win32 = {
401
396
402
397
403
398
isAbsolute : function isAbsolute ( path ) {
404
- assertPath ( path ) ;
399
+ validateString ( path , 'path' ) ;
405
400
const len = path . length ;
406
401
if ( len === 0 )
407
402
return false ;
@@ -429,7 +424,7 @@ const win32 = {
429
424
var firstPart ;
430
425
for ( var i = 0 ; i < arguments . length ; ++ i ) {
431
426
var arg = arguments [ i ] ;
432
- assertPath ( arg ) ;
427
+ validateString ( arg , 'path' ) ;
433
428
if ( arg . length > 0 ) {
434
429
if ( joined === undefined )
435
430
joined = firstPart = arg ;
@@ -494,8 +489,8 @@ const win32 = {
494
489
// to = 'C:\\orandea\\impl\\bbb'
495
490
// The output of the function should be: '..\\..\\impl\\bbb'
496
491
relative : function relative ( from , to ) {
497
- assertPath ( from ) ;
498
- assertPath ( to ) ;
492
+ validateString ( from , 'from' ) ;
493
+ validateString ( to , 'to' ) ;
499
494
500
495
if ( from === to )
501
496
return '' ;
@@ -648,7 +643,7 @@ const win32 = {
648
643
} ,
649
644
650
645
dirname : function dirname ( path ) {
651
- assertPath ( path ) ;
646
+ validateString ( path , 'path' ) ;
652
647
const len = path . length ;
653
648
if ( len === 0 )
654
649
return '.' ;
@@ -744,9 +739,9 @@ const win32 = {
744
739
745
740
746
741
basename : function basename ( path , ext ) {
747
- if ( ext !== undefined && typeof ext !== 'string' )
748
- throw new ERR_INVALID_ARG_TYPE ( ' ext' , 'string' , ext ) ;
749
- assertPath ( path ) ;
742
+ if ( ext !== undefined )
743
+ validateString ( ext , 'ext' ) ;
744
+ validateString ( path , 'path' ) ;
750
745
var start = 0 ;
751
746
var end = - 1 ;
752
747
var matchedSlash = true ;
@@ -832,7 +827,7 @@ const win32 = {
832
827
833
828
834
829
extname : function extname ( path ) {
835
- assertPath ( path ) ;
830
+ validateString ( path , 'path' ) ;
836
831
var start = 0 ;
837
832
var startDot = - 1 ;
838
833
var startPart = 0 ;
@@ -905,7 +900,7 @@ const win32 = {
905
900
906
901
907
902
parse : function parse ( path ) {
908
- assertPath ( path ) ;
903
+ validateString ( path , 'path' ) ;
909
904
910
905
var ret = { root : '' , dir : '' , base : '' , ext : '' , name : '' } ;
911
906
if ( path . length === 0 )
@@ -1082,7 +1077,7 @@ const posix = {
1082
1077
path = process . cwd ( ) ;
1083
1078
}
1084
1079
1085
- assertPath ( path ) ;
1080
+ validateString ( path , 'path' ) ;
1086
1081
1087
1082
// Skip empty entries
1088
1083
if ( path . length === 0 ) {
@@ -1114,7 +1109,7 @@ const posix = {
1114
1109
1115
1110
1116
1111
normalize : function normalize ( path ) {
1117
- assertPath ( path ) ;
1112
+ validateString ( path , 'path' ) ;
1118
1113
1119
1114
if ( path . length === 0 )
1120
1115
return '.' ;
@@ -1138,7 +1133,7 @@ const posix = {
1138
1133
1139
1134
1140
1135
isAbsolute : function isAbsolute ( path ) {
1141
- assertPath ( path ) ;
1136
+ validateString ( path , 'path' ) ;
1142
1137
return path . length > 0 && path . charCodeAt ( 0 ) === CHAR_FORWARD_SLASH ;
1143
1138
} ,
1144
1139
@@ -1149,7 +1144,7 @@ const posix = {
1149
1144
var joined ;
1150
1145
for ( var i = 0 ; i < arguments . length ; ++ i ) {
1151
1146
var arg = arguments [ i ] ;
1152
- assertPath ( arg ) ;
1147
+ validateString ( arg , 'path' ) ;
1153
1148
if ( arg . length > 0 ) {
1154
1149
if ( joined === undefined )
1155
1150
joined = arg ;
@@ -1164,8 +1159,8 @@ const posix = {
1164
1159
1165
1160
1166
1161
relative : function relative ( from , to ) {
1167
- assertPath ( from ) ;
1168
- assertPath ( to ) ;
1162
+ validateString ( from , 'from' ) ;
1163
+ validateString ( to , 'to' ) ;
1169
1164
1170
1165
if ( from === to )
1171
1166
return '' ;
@@ -1262,7 +1257,7 @@ const posix = {
1262
1257
} ,
1263
1258
1264
1259
dirname : function dirname ( path ) {
1265
- assertPath ( path ) ;
1260
+ validateString ( path , 'path' ) ;
1266
1261
if ( path . length === 0 )
1267
1262
return '.' ;
1268
1263
const hasRoot = path . charCodeAt ( 0 ) === CHAR_FORWARD_SLASH ;
@@ -1289,9 +1284,9 @@ const posix = {
1289
1284
1290
1285
1291
1286
basename : function basename ( path , ext ) {
1292
- if ( ext !== undefined && typeof ext !== 'string' )
1293
- throw new ERR_INVALID_ARG_TYPE ( ' ext' , 'string' , ext ) ;
1294
- assertPath ( path ) ;
1287
+ if ( ext !== undefined )
1288
+ validateString ( ext , 'ext' ) ;
1289
+ validateString ( path , 'path' ) ;
1295
1290
1296
1291
var start = 0 ;
1297
1292
var end = - 1 ;
@@ -1367,7 +1362,7 @@ const posix = {
1367
1362
1368
1363
1369
1364
extname : function extname ( path ) {
1370
- assertPath ( path ) ;
1365
+ validateString ( path , 'path' ) ;
1371
1366
var startDot = - 1 ;
1372
1367
var startPart = 0 ;
1373
1368
var end = - 1 ;
@@ -1428,7 +1423,7 @@ const posix = {
1428
1423
1429
1424
1430
1425
parse : function parse ( path ) {
1431
- assertPath ( path ) ;
1426
+ validateString ( path , 'path' ) ;
1432
1427
1433
1428
var ret = { root : '' , dir : '' , base : '' , ext : '' , name : '' } ;
1434
1429
if ( path . length === 0 )
0 commit comments