@@ -33,12 +33,7 @@ const {
3333CHAR_COLON ,
3434CHAR_QUESTION_MARK ,
3535} = 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' ) ;
4237
4338function isPathSeparator ( code ) {
4439return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH ;
@@ -163,7 +158,7 @@ const win32 = {
163158}
164159}
165160
166- assertPath ( path ) ;
161+ validateString ( path , 'path' ) ;
167162
168163// Skip empty entries
169164if ( path . length === 0 ) {
@@ -282,7 +277,7 @@ const win32 = {
282277} ,
283278
284279normalize : function normalize ( path ) {
285- assertPath ( path ) ;
280+ validateString ( path , 'path' ) ;
286281const len = path . length ;
287282if ( len === 0 )
288283return '.' ;
@@ -401,7 +396,7 @@ const win32 = {
401396
402397
403398isAbsolute : function isAbsolute ( path ) {
404- assertPath ( path ) ;
399+ validateString ( path , 'path' ) ;
405400const len = path . length ;
406401if ( len === 0 )
407402return false ;
@@ -429,7 +424,7 @@ const win32 = {
429424var firstPart ;
430425for ( var i = 0 ; i < arguments . length ; ++ i ) {
431426var arg = arguments [ i ] ;
432- assertPath ( arg ) ;
427+ validateString ( arg , 'path' ) ;
433428if ( arg . length > 0 ) {
434429if ( joined === undefined )
435430joined = firstPart = arg ;
@@ -494,8 +489,8 @@ const win32 = {
494489// to = 'C:\\orandea\\impl\\bbb'
495490// The output of the function should be: '..\\..\\impl\\bbb'
496491relative : function relative ( from , to ) {
497- assertPath ( from ) ;
498- assertPath ( to ) ;
492+ validateString ( from , 'from' ) ;
493+ validateString ( to , 'to' ) ;
499494
500495if ( from === to )
501496return '' ;
@@ -648,7 +643,7 @@ const win32 = {
648643} ,
649644
650645dirname : function dirname ( path ) {
651- assertPath ( path ) ;
646+ validateString ( path , 'path' ) ;
652647const len = path . length ;
653648if ( len === 0 )
654649return '.' ;
@@ -744,9 +739,9 @@ const win32 = {
744739
745740
746741basename : 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' ) ;
750745var start = 0 ;
751746var end = - 1 ;
752747var matchedSlash = true ;
@@ -832,7 +827,7 @@ const win32 = {
832827
833828
834829extname : function extname ( path ) {
835- assertPath ( path ) ;
830+ validateString ( path , 'path' ) ;
836831var start = 0 ;
837832var startDot = - 1 ;
838833var startPart = 0 ;
@@ -905,7 +900,7 @@ const win32 = {
905900
906901
907902parse : function parse ( path ) {
908- assertPath ( path ) ;
903+ validateString ( path , 'path' ) ;
909904
910905var ret = { root : '' , dir : '' , base : '' , ext : '' , name : '' } ;
911906if ( path . length === 0 )
@@ -1082,7 +1077,7 @@ const posix = {
10821077path = process . cwd ( ) ;
10831078}
10841079
1085- assertPath ( path ) ;
1080+ validateString ( path , 'path' ) ;
10861081
10871082// Skip empty entries
10881083if ( path . length === 0 ) {
@@ -1114,7 +1109,7 @@ const posix = {
11141109
11151110
11161111normalize : function normalize ( path ) {
1117- assertPath ( path ) ;
1112+ validateString ( path , 'path' ) ;
11181113
11191114if ( path . length === 0 )
11201115return '.' ;
@@ -1138,7 +1133,7 @@ const posix = {
11381133
11391134
11401135isAbsolute : function isAbsolute ( path ) {
1141- assertPath ( path ) ;
1136+ validateString ( path , 'path' ) ;
11421137return path . length > 0 && path . charCodeAt ( 0 ) === CHAR_FORWARD_SLASH ;
11431138} ,
11441139
@@ -1149,7 +1144,7 @@ const posix = {
11491144var joined ;
11501145for ( var i = 0 ; i < arguments . length ; ++ i ) {
11511146var arg = arguments [ i ] ;
1152- assertPath ( arg ) ;
1147+ validateString ( arg , 'path' ) ;
11531148if ( arg . length > 0 ) {
11541149if ( joined === undefined )
11551150joined = arg ;
@@ -1164,8 +1159,8 @@ const posix = {
11641159
11651160
11661161relative : function relative ( from , to ) {
1167- assertPath ( from ) ;
1168- assertPath ( to ) ;
1162+ validateString ( from , 'from' ) ;
1163+ validateString ( to , 'to' ) ;
11691164
11701165if ( from === to )
11711166return '' ;
@@ -1262,7 +1257,7 @@ const posix = {
12621257} ,
12631258
12641259dirname : function dirname ( path ) {
1265- assertPath ( path ) ;
1260+ validateString ( path , 'path' ) ;
12661261if ( path . length === 0 )
12671262return '.' ;
12681263const hasRoot = path . charCodeAt ( 0 ) === CHAR_FORWARD_SLASH ;
@@ -1289,9 +1284,9 @@ const posix = {
12891284
12901285
12911286basename : 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' ) ;
12951290
12961291var start = 0 ;
12971292var end = - 1 ;
@@ -1367,7 +1362,7 @@ const posix = {
13671362
13681363
13691364extname : function extname ( path ) {
1370- assertPath ( path ) ;
1365+ validateString ( path , 'path' ) ;
13711366var startDot = - 1 ;
13721367var startPart = 0 ;
13731368var end = - 1 ;
@@ -1428,7 +1423,7 @@ const posix = {
14281423
14291424
14301425parse : function parse ( path ) {
1431- assertPath ( path ) ;
1426+ validateString ( path , 'path' ) ;
14321427
14331428var ret = { root : '' , dir : '' , base : '' , ext : '' , name : '' } ;
14341429if ( path . length === 0 )
0 commit comments