Skip to content

Commit 7eda47e

Browse files
cjihrigBethGriggs
authored andcommitted
path: replace assertPath() with validator
The path module's assertPath() does exactly what the validateString() validator does, so this commit updates path to use validateString() instead. A couple drive by updates to validateString() outside of assertPath() are also included. PR-URL: #24840 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
1 parent fd8a481 commit 7eda47e

1 file changed

Lines changed: 25 additions & 30 deletions

File tree

‎lib/path.js‎

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,7 @@ const {
3333
CHAR_COLON,
3434
CHAR_QUESTION_MARK,
3535
}=require('internal/constants');
36-
37-
functionassertPath(path){
38-
if(typeofpath!=='string'){
39-
thrownewERR_INVALID_ARG_TYPE('path','string',path);
40-
}
41-
}
36+
const{ validateString }=require('internal/validators');
4237

4338
functionisPathSeparator(code){
4439
returncode===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
169164
if(path.length===0){
@@ -282,7 +277,7 @@ const win32 = {
282277
},
283278

284279
normalize: functionnormalize(path){
285-
assertPath(path);
280+
validateString(path,'path');
286281
constlen=path.length;
287282
if(len===0)
288283
return'.';
@@ -401,7 +396,7 @@ const win32 = {
401396

402397

403398
isAbsolute: functionisAbsolute(path){
404-
assertPath(path);
399+
validateString(path,'path');
405400
constlen=path.length;
406401
if(len===0)
407402
returnfalse;
@@ -429,7 +424,7 @@ const win32 = {
429424
varfirstPart;
430425
for(vari=0;i<arguments.length;++i){
431426
vararg=arguments[i];
432-
assertPath(arg);
427+
validateString(arg,'path');
433428
if(arg.length>0){
434429
if(joined===undefined)
435430
joined=firstPart=arg;
@@ -494,8 +489,8 @@ const win32 = {
494489
// to = 'C:\\orandea\\impl\\bbb'
495490
// The output of the function should be: '..\\..\\impl\\bbb'
496491
relative: functionrelative(from,to){
497-
assertPath(from);
498-
assertPath(to);
492+
validateString(from,'from');
493+
validateString(to,'to');
499494

500495
if(from===to)
501496
return'';
@@ -648,7 +643,7 @@ const win32 = {
648643
},
649644

650645
dirname: functiondirname(path){
651-
assertPath(path);
646+
validateString(path,'path');
652647
constlen=path.length;
653648
if(len===0)
654649
return'.';
@@ -744,9 +739,9 @@ const win32 = {
744739

745740

746741
basename: functionbasename(path,ext){
747-
if(ext!==undefined&&typeofext!=='string')
748-
thrownewERR_INVALID_ARG_TYPE('ext','string',ext);
749-
assertPath(path);
742+
if(ext!==undefined)
743+
validateString(ext,'ext');
744+
validateString(path,'path');
750745
varstart=0;
751746
varend=-1;
752747
varmatchedSlash=true;
@@ -832,7 +827,7 @@ const win32 = {
832827

833828

834829
extname: functionextname(path){
835-
assertPath(path);
830+
validateString(path,'path');
836831
varstart=0;
837832
varstartDot=-1;
838833
varstartPart=0;
@@ -905,7 +900,7 @@ const win32 = {
905900

906901

907902
parse: functionparse(path){
908-
assertPath(path);
903+
validateString(path,'path');
909904

910905
varret={root: '',dir: '',base: '',ext: '',name: ''};
911906
if(path.length===0)
@@ -1082,7 +1077,7 @@ const posix = {
10821077
path=process.cwd();
10831078
}
10841079

1085-
assertPath(path);
1080+
validateString(path,'path');
10861081

10871082
// Skip empty entries
10881083
if(path.length===0){
@@ -1114,7 +1109,7 @@ const posix = {
11141109

11151110

11161111
normalize: functionnormalize(path){
1117-
assertPath(path);
1112+
validateString(path,'path');
11181113

11191114
if(path.length===0)
11201115
return'.';
@@ -1138,7 +1133,7 @@ const posix = {
11381133

11391134

11401135
isAbsolute: functionisAbsolute(path){
1141-
assertPath(path);
1136+
validateString(path,'path');
11421137
returnpath.length>0&&path.charCodeAt(0)===CHAR_FORWARD_SLASH;
11431138
},
11441139

@@ -1149,7 +1144,7 @@ const posix = {
11491144
varjoined;
11501145
for(vari=0;i<arguments.length;++i){
11511146
vararg=arguments[i];
1152-
assertPath(arg);
1147+
validateString(arg,'path');
11531148
if(arg.length>0){
11541149
if(joined===undefined)
11551150
joined=arg;
@@ -1164,8 +1159,8 @@ const posix = {
11641159

11651160

11661161
relative: functionrelative(from,to){
1167-
assertPath(from);
1168-
assertPath(to);
1162+
validateString(from,'from');
1163+
validateString(to,'to');
11691164

11701165
if(from===to)
11711166
return'';
@@ -1262,7 +1257,7 @@ const posix = {
12621257
},
12631258

12641259
dirname: functiondirname(path){
1265-
assertPath(path);
1260+
validateString(path,'path');
12661261
if(path.length===0)
12671262
return'.';
12681263
consthasRoot=path.charCodeAt(0)===CHAR_FORWARD_SLASH;
@@ -1289,9 +1284,9 @@ const posix = {
12891284

12901285

12911286
basename: functionbasename(path,ext){
1292-
if(ext!==undefined&&typeofext!=='string')
1293-
thrownewERR_INVALID_ARG_TYPE('ext','string',ext);
1294-
assertPath(path);
1287+
if(ext!==undefined)
1288+
validateString(ext,'ext');
1289+
validateString(path,'path');
12951290

12961291
varstart=0;
12971292
varend=-1;
@@ -1367,7 +1362,7 @@ const posix = {
13671362

13681363

13691364
extname: functionextname(path){
1370-
assertPath(path);
1365+
validateString(path,'path');
13711366
varstartDot=-1;
13721367
varstartPart=0;
13731368
varend=-1;
@@ -1428,7 +1423,7 @@ const posix = {
14281423

14291424

14301425
parse: functionparse(path){
1431-
assertPath(path);
1426+
validateString(path,'path');
14321427

14331428
varret={root: '',dir: '',base: '',ext: '',name: ''};
14341429
if(path.length===0)

0 commit comments

Comments
 (0)