@@ -38,6 +38,15 @@ module.exports = function inspect_(obj, options, depth, seen) {
3838 throw new TypeError ( 'option "customInspect", if provided, must be `true` or `false`' ) ;
3939 }
4040
41+ if (
42+ has ( opts , 'indent' )
43+ && opts . indent !== null
44+ && opts . indent !== '\t'
45+ && ! ( parseInt ( opts . indent , 10 ) === opts . indent && opts . indent > 0 )
46+ ) {
47+ throw new TypeError ( 'options "indent" must be "\\t", an integer > 0, or `null`' ) ;
48+ }
49+
4150 if ( typeof obj === 'undefined' ) {
4251 return 'undefined' ;
4352 }
@@ -67,17 +76,28 @@ module.exports = function inspect_(obj, options, depth, seen) {
6776 return isArray ( obj ) ? '[Array]' : '[Object]' ;
6877 }
6978
79+ var indent = getIndent ( opts , depth ) ;
80+
7081 if ( typeof seen === 'undefined' ) {
7182 seen = [ ] ;
7283 } else if ( indexOf ( seen , obj ) >= 0 ) {
7384 return '[Circular]' ;
7485 }
7586
76- function inspect ( value , from ) {
87+ function inspect ( value , from , noIndent ) {
7788 if ( from ) {
7889 seen = seen . slice ( ) ;
7990 seen . push ( from ) ;
8091 }
92+ if ( noIndent ) {
93+ var newOpts = {
94+ depth : opts . depth
95+ } ;
96+ if ( has ( opts , 'quoteStyle' ) ) {
97+ newOpts . quoteStyle = opts . quoteStyle ;
98+ }
99+ return inspect_ ( value , newOpts , depth + 1 , seen ) ;
100+ }
81101 return inspect_ ( value , opts , depth + 1 , seen ) ;
82102 }
83103
@@ -102,7 +122,11 @@ module.exports = function inspect_(obj, options, depth, seen) {
102122 }
103123 if ( isArray ( obj ) ) {
104124 if ( obj . length === 0 ) { return '[]' ; }
105- return '[ ' + arrObjKeys ( obj , inspect ) . join ( ', ' ) + ' ]' ;
125+ var xs = arrObjKeys ( obj , inspect ) ;
126+ if ( indent && ! singleLineValues ( xs ) ) {
127+ return '[' + indentedJoin ( xs , indent ) + ']' ;
128+ }
129+ return '[ ' + xs . join ( ', ' ) + ' ]' ;
106130 }
107131 if ( isError ( obj ) ) {
108132 var parts = arrObjKeys ( obj , inspect ) ;
@@ -119,16 +143,16 @@ module.exports = function inspect_(obj, options, depth, seen) {
119143 if ( isMap ( obj ) ) {
120144 var mapParts = [ ] ;
121145 mapForEach . call ( obj , function ( value , key ) {
122- mapParts . push ( inspect ( key , obj ) + ' => ' + inspect ( value , obj ) ) ;
146+ mapParts . push ( inspect ( key , obj , true ) + ' => ' + inspect ( value , obj ) ) ;
123147 } ) ;
124- return collectionOf ( 'Map' , mapSize . call ( obj ) , mapParts ) ;
148+ return collectionOf ( 'Map' , mapSize . call ( obj ) , mapParts , indent ) ;
125149 }
126150 if ( isSet ( obj ) ) {
127151 var setParts = [ ] ;
128152 setForEach . call ( obj , function ( value ) {
129153 setParts . push ( inspect ( value , obj ) ) ;
130154 } ) ;
131- return collectionOf ( 'Set' , setSize . call ( obj ) , setParts ) ;
155+ return collectionOf ( 'Set' , setSize . call ( obj ) , setParts , indent ) ;
132156 }
133157 if ( isWeakMap ( obj ) ) {
134158 return weakCollectionOf ( 'WeakMap' ) ;
@@ -149,9 +173,12 @@ module.exports = function inspect_(obj, options, depth, seen) {
149173 return markBoxed ( inspect ( String ( obj ) ) ) ;
150174 }
151175 if ( ! isDate ( obj ) && ! isRegExp ( obj ) ) {
152- var xs = arrObjKeys ( obj , inspect ) ;
153- if ( xs . length === 0 ) { return '{}' ; }
154- return '{ ' + xs . join ( ', ' ) + ' }' ;
176+ var ys = arrObjKeys ( obj , inspect ) ;
177+ if ( ys . length === 0 ) { return '{}' ; }
178+ if ( indent ) {
179+ return '{' + indentedJoin ( ys , indent ) + '}' ;
180+ }
181+ return '{ ' + ys . join ( ', ' ) + ' }' ;
155182 }
156183 return String ( obj ) ;
157184} ;
@@ -299,8 +326,39 @@ function weakCollectionOf(type) {
299326 return type + ' { ? }' ;
300327}
301328
302- function collectionOf ( type , size , entries ) {
303- return type + ' (' + size + ') {' + entries . join ( ', ' ) + '}' ;
329+ function collectionOf ( type , size , entries , indent ) {
330+ var joinedEntries = indent ? indentedJoin ( entries , indent ) : entries . join ( ', ' ) ;
331+ return type + ' (' + size + ') {' + joinedEntries + '}' ;
332+ }
333+
334+ function singleLineValues ( xs ) {
335+ for ( var i = 0 ; i < xs . length ; i ++ ) {
336+ if ( indexOf ( xs [ i ] , '\n' ) >= 0 ) {
337+ return false ;
338+ }
339+ }
340+ return true ;
341+ }
342+
343+ function getIndent ( opts , depth ) {
344+ var baseIndent ;
345+ if ( opts . indent === '\t' ) {
346+ baseIndent = '\t' ;
347+ } else if ( typeof opts . indent === 'number' && opts . indent > 0 ) {
348+ baseIndent = Array ( opts . indent + 1 ) . join ( ' ' ) ;
349+ } else {
350+ return null ;
351+ }
352+ return {
353+ base : baseIndent ,
354+ prev : Array ( depth + 1 ) . join ( baseIndent )
355+ } ;
356+ }
357+
358+ function indentedJoin ( xs , indent ) {
359+ if ( xs . length === 0 ) { return '' ; }
360+ var lineJoiner = '\n' + indent . prev + indent . base ;
361+ return lineJoiner + xs . join ( ',' + lineJoiner ) + '\n' + indent . prev ;
304362}
305363
306364function arrObjKeys ( obj , inspect ) {
0 commit comments