@@ -19,7 +19,6 @@ const {
19
19
ArrayPrototypeIndexOf,
20
20
ArrayPrototypeJoin,
21
21
ArrayPrototypeMap,
22
- ArrayPrototypePop,
23
22
ArrayPrototypePush,
24
23
ArrayPrototypeSlice,
25
24
ArrayPrototypeSplice,
@@ -895,6 +894,19 @@ function determineSpecificType(value) {
895
894
return `type ${ typeof value } (${ inspected } )` ;
896
895
}
897
896
897
+ /**
898
+ * Create a list string in the form like 'A and B' or 'A, B, ..., and Z'.
899
+ * We cannot use Intl.ListFormat because it's not available in
900
+ * --without-intl builds.
901
+ * @param {string[] } array An array of strings.
902
+ * @param {string } [type] The list type to be inserted before the last element.
903
+ * @returns {string }
904
+ */
905
+ function formatList ( array , type = 'and' ) {
906
+ return array . length < 3 ? ArrayPrototypeJoin ( array , ` ${ type } ` ) :
907
+ `${ ArrayPrototypeJoin ( ArrayPrototypeSlice ( array , 0 , - 1 ) , ', ' ) } , ${ type } ${ array [ array . length - 1 ] } ` ;
908
+ }
909
+
898
910
module . exports = {
899
911
AbortError,
900
912
aggregateTwoErrors,
@@ -909,6 +921,7 @@ module.exports = {
909
921
errnoException,
910
922
exceptionWithHostPort,
911
923
fatalExceptionStackEnhancers,
924
+ formatList,
912
925
genericNodeError,
913
926
getMessage,
914
927
hideInternalStackFrames,
@@ -1240,39 +1253,20 @@ E('ERR_INVALID_ARG_TYPE',
1240
1253
}
1241
1254
1242
1255
if ( types . length > 0 ) {
1243
- if ( types . length > 2 ) {
1244
- const last = ArrayPrototypePop ( types ) ;
1245
- msg += `one of type ${ ArrayPrototypeJoin ( types , ', ' ) } , or ${ last } ` ;
1246
- } else if ( types . length === 2 ) {
1247
- msg += `one of type ${ types [ 0 ] } or ${ types [ 1 ] } ` ;
1248
- } else {
1249
- msg += `of type ${ types [ 0 ] } ` ;
1250
- }
1256
+ msg += `${ types . length > 1 ? 'one of type' : 'of type' } ${ formatList ( types , 'or' ) } ` ;
1251
1257
if ( instances . length > 0 || other . length > 0 )
1252
1258
msg += ' or ' ;
1253
1259
}
1254
1260
1255
1261
if ( instances . length > 0 ) {
1256
- if ( instances . length > 2 ) {
1257
- const last = ArrayPrototypePop ( instances ) ;
1258
- msg +=
1259
- `an instance of ${ ArrayPrototypeJoin ( instances , ', ' ) } , or ${ last } ` ;
1260
- } else {
1261
- msg += `an instance of ${ instances [ 0 ] } ` ;
1262
- if ( instances . length === 2 ) {
1263
- msg += ` or ${ instances [ 1 ] } ` ;
1264
- }
1265
- }
1262
+ msg += `an instance of ${ formatList ( instances , 'or' ) } ` ;
1266
1263
if ( other . length > 0 )
1267
1264
msg += ' or ' ;
1268
1265
}
1269
1266
1270
1267
if ( other . length > 0 ) {
1271
- if ( other . length > 2 ) {
1272
- const last = ArrayPrototypePop ( other ) ;
1273
- msg += `one of ${ ArrayPrototypeJoin ( other , ', ' ) } , or ${ last } ` ;
1274
- } else if ( other . length === 2 ) {
1275
- msg += `one of ${ other [ 0 ] } or ${ other [ 1 ] } ` ;
1268
+ if ( other . length > 1 ) {
1269
+ msg += `one of ${ formatList ( other , 'or' ) } ` ;
1276
1270
} else {
1277
1271
if ( StringPrototypeToLowerCase ( other [ 0 ] ) !== other [ 0 ] )
1278
1272
msg += 'an ' ;
@@ -1452,18 +1446,7 @@ E('ERR_MISSING_ARGS',
1452
1446
ArrayPrototypeJoin ( ArrayPrototypeMap ( a , wrap ) , ' or ' ) :
1453
1447
wrap ( a ) )
1454
1448
) ;
1455
- switch ( len ) {
1456
- case 1 :
1457
- msg += `${ args [ 0 ] } argument` ;
1458
- break ;
1459
- case 2 :
1460
- msg += `${ args [ 0 ] } and ${ args [ 1 ] } arguments` ;
1461
- break ;
1462
- default :
1463
- msg += ArrayPrototypeJoin ( ArrayPrototypeSlice ( args , 0 , len - 1 ) , ', ' ) ;
1464
- msg += `, and ${ args [ len - 1 ] } arguments` ;
1465
- break ;
1466
- }
1449
+ msg += `${ formatList ( args ) } argument${ len > 1 ? 's' : '' } ` ;
1467
1450
return `${ msg } must be specified` ;
1468
1451
} , TypeError ) ;
1469
1452
E ( 'ERR_MISSING_OPTION' , '%s is required' , TypeError ) ;
@@ -1696,7 +1679,7 @@ E('ERR_UNKNOWN_SIGNAL', 'Unknown signal: %s', TypeError);
1696
1679
E ( 'ERR_UNSUPPORTED_DIR_IMPORT' , "Directory import '%s' is not supported " +
1697
1680
'resolving ES modules imported from %s' , Error ) ;
1698
1681
E ( 'ERR_UNSUPPORTED_ESM_URL_SCHEME' , ( url , supported ) => {
1699
- let msg = `Only URLs with a scheme in: ${ ArrayPrototypeJoin ( supported , ', ' ) } are supported by the default ESM loader` ;
1682
+ let msg = `Only URLs with a scheme in: ${ formatList ( supported ) } are supported by the default ESM loader` ;
1700
1683
if ( isWindows && url . protocol . length === 2 ) {
1701
1684
msg +=
1702
1685
'. On Windows, absolute paths must be valid file:// URLs' ;
0 commit comments