@@ -38,6 +38,15 @@ module.exports = function inspect_(obj, options, depth, seen) {
38
38
throw new TypeError ( 'option "customInspect", if provided, must be `true` or `false`' ) ;
39
39
}
40
40
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
+
41
50
if ( typeof obj === 'undefined' ) {
42
51
return 'undefined' ;
43
52
}
@@ -67,17 +76,28 @@ module.exports = function inspect_(obj, options, depth, seen) {
67
76
return isArray ( obj ) ? '[Array]' : '[Object]' ;
68
77
}
69
78
79
+ var indent = getIndent ( opts , depth ) ;
80
+
70
81
if ( typeof seen === 'undefined' ) {
71
82
seen = [ ] ;
72
83
} else if ( indexOf ( seen , obj ) >= 0 ) {
73
84
return '[Circular]' ;
74
85
}
75
86
76
- function inspect ( value , from ) {
87
+ function inspect ( value , from , noIndent ) {
77
88
if ( from ) {
78
89
seen = seen . slice ( ) ;
79
90
seen . push ( from ) ;
80
91
}
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
+ }
81
101
return inspect_ ( value , opts , depth + 1 , seen ) ;
82
102
}
83
103
@@ -102,7 +122,11 @@ module.exports = function inspect_(obj, options, depth, seen) {
102
122
}
103
123
if ( isArray ( obj ) ) {
104
124
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 ( ', ' ) + ' ]' ;
106
130
}
107
131
if ( isError ( obj ) ) {
108
132
var parts = arrObjKeys ( obj , inspect ) ;
@@ -119,16 +143,16 @@ module.exports = function inspect_(obj, options, depth, seen) {
119
143
if ( isMap ( obj ) ) {
120
144
var mapParts = [ ] ;
121
145
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 ) ) ;
123
147
} ) ;
124
- return collectionOf ( 'Map' , mapSize . call ( obj ) , mapParts ) ;
148
+ return collectionOf ( 'Map' , mapSize . call ( obj ) , mapParts , indent ) ;
125
149
}
126
150
if ( isSet ( obj ) ) {
127
151
var setParts = [ ] ;
128
152
setForEach . call ( obj , function ( value ) {
129
153
setParts . push ( inspect ( value , obj ) ) ;
130
154
} ) ;
131
- return collectionOf ( 'Set' , setSize . call ( obj ) , setParts ) ;
155
+ return collectionOf ( 'Set' , setSize . call ( obj ) , setParts , indent ) ;
132
156
}
133
157
if ( isWeakMap ( obj ) ) {
134
158
return weakCollectionOf ( 'WeakMap' ) ;
@@ -149,9 +173,12 @@ module.exports = function inspect_(obj, options, depth, seen) {
149
173
return markBoxed ( inspect ( String ( obj ) ) ) ;
150
174
}
151
175
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 ( ', ' ) + ' }' ;
155
182
}
156
183
return String ( obj ) ;
157
184
} ;
@@ -299,8 +326,39 @@ function weakCollectionOf(type) {
299
326
return type + ' { ? }' ;
300
327
}
301
328
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 ;
304
362
}
305
363
306
364
function arrObjKeys ( obj , inspect ) {
0 commit comments