@@ -159,6 +159,7 @@ function inspect(value, opts) {
159
159
budget : { } ,
160
160
indentationLvl : 0 ,
161
161
seen : [ ] ,
162
+ currentDepth : 0 ,
162
163
stylize : stylizeNoColor ,
163
164
showHidden : inspectDefaultOptions . showHidden ,
164
165
depth : inspectDefaultOptions . depth ,
@@ -760,6 +761,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
760
761
recurseTimes += 1 ;
761
762
762
763
ctx . seen . push ( value ) ;
764
+ ctx . currentDepth = recurseTimes ;
763
765
let output ;
764
766
const indentationLvl = ctx . indentationLvl ;
765
767
try {
@@ -783,7 +785,10 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
783
785
}
784
786
}
785
787
786
- const res = reduceToSingleString ( ctx , output , base , braces ) ;
788
+ const combine = typeof ctx . compact === 'number' &&
789
+ ctx . currentDepth - recurseTimes < ctx . compact ;
790
+
791
+ const res = reduceToSingleString ( ctx , output , base , braces , combine ) ;
787
792
const budget = ctx . budget [ ctx . indentationLvl ] || 0 ;
788
793
const newLength = budget + res . length ;
789
794
ctx . budget [ ctx . indentationLvl ] = newLength ;
@@ -824,7 +829,7 @@ function formatBigInt(fn, value) {
824
829
825
830
function formatPrimitive ( fn , value , ctx ) {
826
831
if ( typeof value === 'string' ) {
827
- if ( ctx . compact === false &&
832
+ if ( ctx . compact !== true &&
828
833
ctx . indentationLvl + value . length > ctx . breakLength &&
829
834
value . length > kMinLineLength ) {
830
835
const rawMaxLineLength = ctx . breakLength - ctx . indentationLvl ;
@@ -1134,7 +1139,7 @@ function formatProperty(ctx, value, recurseTimes, key, type) {
1134
1139
const desc = Object . getOwnPropertyDescriptor ( value , key ) ||
1135
1140
{ value : value [ key ] , enumerable : true } ;
1136
1141
if ( desc . value !== undefined ) {
1137
- const diff = ( type !== kObjectType || ctx . compact === false ) ? 2 : 3 ;
1142
+ const diff = ( type !== kObjectType || ctx . compact !== true ) ? 2 : 3 ;
1138
1143
ctx . indentationLvl += diff ;
1139
1144
str = formatValue ( ctx , desc . value , recurseTimes ) ;
1140
1145
if ( diff === 3 ) {
@@ -1191,16 +1196,27 @@ function formatProperty(ctx, value, recurseTimes, key, type) {
1191
1196
return `${ name } :${ extra } ${ str } ` ;
1192
1197
}
1193
1198
1194
- function reduceToSingleString ( ctx , output , base , braces ) {
1199
+ function reduceToSingleString ( ctx , output , base , braces , combine = false ) {
1195
1200
const breakLength = ctx . breakLength ;
1196
1201
let i = 0 ;
1197
- if ( ctx . compact === false ) {
1198
- const indentation = ' ' . repeat ( ctx . indentationLvl ) ;
1199
- let res = `${ base ? `${ base } ` : '' } ${ braces [ 0 ] } \n${ indentation } ` ;
1202
+ if ( ctx . compact !== true ) {
1203
+ if ( combine ) {
1204
+ const totalLength = output . reduce ( ( sum , cur ) => sum + cur . length , 0 ) ;
1205
+ if ( totalLength + output . length * 2 < breakLength ) {
1206
+ let res = `${ base ? `${ base } ` : '' } ${ braces [ 0 ] } ` ;
1207
+ for ( ; i < output . length - 1 ; i ++ ) {
1208
+ res += `${ output [ i ] } , ` ;
1209
+ }
1210
+ res += `${ output [ i ] } ${ braces [ 1 ] } ` ;
1211
+ return res ;
1212
+ }
1213
+ }
1214
+ const indentation = `\n${ ' ' . repeat ( ctx . indentationLvl ) } ` ;
1215
+ let res = `${ base ? `${ base } ` : '' } ${ braces [ 0 ] } ${ indentation } ` ;
1200
1216
for ( ; i < output . length - 1 ; i ++ ) {
1201
- res += `${ output [ i ] } ,\n ${ indentation } ` ;
1217
+ res += `${ output [ i ] } ,${ indentation } ` ;
1202
1218
}
1203
- res += `${ output [ i ] } \n ${ indentation } ${ braces [ 1 ] } ` ;
1219
+ res += `${ output [ i ] } ${ indentation } ${ braces [ 1 ] } ` ;
1204
1220
return res ;
1205
1221
}
1206
1222
if ( output . length * 2 <= breakLength ) {
0 commit comments