@@ -6,6 +6,7 @@ const internalUtil = require('internal/util');
66const binding = process . binding ( 'util' ) ;
77
88const isError = internalUtil . isError ;
9+ const kDefaultMaxLength = 100 ;
910
1011var Debug ;
1112
@@ -141,6 +142,8 @@ function inspect(obj, opts) {
141142 if ( ctx . customInspect === undefined ) ctx . customInspect = true ;
142143 if ( ctx . showProxy === undefined ) ctx . showProxy = false ;
143144 if ( ctx . colors ) ctx . stylize = stylizeWithColor ;
145+ if ( ctx . maxArrayLength === undefined ) ctx . maxArrayLength = kDefaultMaxLength ;
146+ if ( ctx . maxArrayLength === null ) ctx . maxArrayLength = Infinity ;
144147 return formatValue ( ctx , obj , ctx . depth ) ;
145148}
146149exports . inspect = inspect ;
@@ -579,14 +582,19 @@ function formatObject(ctx, value, recurseTimes, visibleKeys, keys) {
579582
580583function formatArray ( ctx , value , recurseTimes , visibleKeys , keys ) {
581584 var output = [ ] ;
582- for ( var i = 0 , l = value . length ; i < l ; ++ i ) {
585+ const maxLength = Math . min ( Math . max ( 0 , ctx . maxArrayLength ) , value . length ) ;
586+ const remaining = value . length - maxLength ;
587+ for ( var i = 0 ; i < maxLength ; ++ i ) {
583588 if ( hasOwnProperty ( value , String ( i ) ) ) {
584589 output . push ( formatProperty ( ctx , value , recurseTimes , visibleKeys ,
585590 String ( i ) , true ) ) ;
586591 } else {
587592 output . push ( '' ) ;
588593 }
589594 }
595+ if ( remaining > 0 ) {
596+ output . push ( `... ${ remaining } more item${ remaining > 1 ? 's' : '' } ` ) ;
597+ }
590598 keys . forEach ( function ( key ) {
591599 if ( typeof key === 'symbol' || ! key . match ( / ^ \d + $ / ) ) {
592600 output . push ( formatProperty ( ctx , value , recurseTimes , visibleKeys ,
@@ -598,9 +606,14 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
598606
599607
600608function formatTypedArray ( ctx , value , recurseTimes , visibleKeys , keys ) {
601- var output = new Array ( value . length ) ;
602- for ( var i = 0 , l = value . length ; i < l ; ++ i )
609+ const maxLength = Math . min ( Math . max ( 0 , ctx . maxArrayLength ) , value . length ) ;
610+ const remaining = value . length - maxLength ;
611+ var output = new Array ( maxLength ) ;
612+ for ( var i = 0 ; i < maxLength ; ++ i )
603613 output [ i ] = formatNumber ( ctx , value [ i ] ) ;
614+ if ( remaining > 0 ) {
615+ output . push ( `... ${ remaining } more item${ remaining > 1 ? 's' : '' } ` ) ;
616+ }
604617 for ( const key of keys ) {
605618 if ( typeof key === 'symbol' || ! key . match ( / ^ \d + $ / ) ) {
606619 output . push (
0 commit comments