@@ -68,7 +68,8 @@ const inspectDefaultOptions = Object.seal({
68
68
customInspect : true ,
69
69
showProxy : false ,
70
70
maxArrayLength : 100 ,
71
- breakLength : 60
71
+ breakLength : 60 ,
72
+ compact : true
72
73
} ) ;
73
74
74
75
const propertyIsEnumerable = Object . prototype . propertyIsEnumerable ;
@@ -86,6 +87,10 @@ const strEscapeSequencesReplacer = /[\x00-\x1f\x27\x5c]/g;
86
87
const keyStrRegExp = / ^ [ a - z A - Z _ ] [ a - z A - Z _ 0 - 9 ] * $ / ;
87
88
const numberRegExp = / ^ ( 0 | [ 1 - 9 ] [ 0 - 9 ] * ) $ / ;
88
89
90
+ const readableRegExps = { } ;
91
+
92
+ const MIN_LINE_LENGTH = 16 ;
93
+
89
94
// Escaped special characters. Use empty strings to fill up unused entries.
90
95
const meta = [
91
96
'\\u0000' , '\\u0001' , '\\u0002' , '\\u0003' , '\\u0004' ,
@@ -276,7 +281,8 @@ function inspect(value, opts) {
276
281
showProxy : inspectDefaultOptions . showProxy ,
277
282
maxArrayLength : inspectDefaultOptions . maxArrayLength ,
278
283
breakLength : inspectDefaultOptions . breakLength ,
279
- indentationLvl : 0
284
+ indentationLvl : 0 ,
285
+ compact : inspectDefaultOptions . compact
280
286
} ;
281
287
// Legacy...
282
288
if ( arguments . length > 2 ) {
@@ -374,7 +380,7 @@ function ensureDebugIsInitialized() {
374
380
function formatValue ( ctx , value , recurseTimes , ln ) {
375
381
// Primitive types cannot have properties
376
382
if ( typeof value !== 'object' && typeof value !== 'function' ) {
377
- return formatPrimitive ( ctx . stylize , value ) ;
383
+ return formatPrimitive ( ctx . stylize , value , ctx ) ;
378
384
}
379
385
if ( value === null ) {
380
386
return ctx . stylize ( 'null' , 'null' ) ;
@@ -481,10 +487,10 @@ function formatValue(ctx, value, recurseTimes, ln) {
481
487
} catch ( e ) { /* ignore */ }
482
488
483
489
if ( typeof raw === 'string' ) {
484
- const formatted = formatPrimitive ( stylizeNoColor , raw ) ;
490
+ const formatted = formatPrimitive ( stylizeNoColor , raw , ctx ) ;
485
491
if ( keyLength === raw . length )
486
492
return ctx . stylize ( `[String: ${ formatted } ]` , 'string' ) ;
487
- base = ` [String: ${ formatted } ]` ;
493
+ base = `[String: ${ formatted } ]` ;
488
494
// For boxed Strings, we have to remove the 0-n indexed entries,
489
495
// since they just noisy up the output and are redundant
490
496
// Make boxed primitive Strings look like such
@@ -505,25 +511,25 @@ function formatValue(ctx, value, recurseTimes, ln) {
505
511
const name = `${ constructor . name } ${ value . name ? `: ${ value . name } ` : '' } ` ;
506
512
if ( keyLength === 0 )
507
513
return ctx . stylize ( `[${ name } ]` , 'special' ) ;
508
- base = ` [${ name } ]` ;
514
+ base = `[${ name } ]` ;
509
515
} else if ( isRegExp ( value ) ) {
510
516
// Make RegExps say that they are RegExps
511
517
if ( keyLength === 0 || recurseTimes < 0 )
512
518
return ctx . stylize ( regExpToString . call ( value ) , 'regexp' ) ;
513
- base = ` ${ regExpToString . call ( value ) } ` ;
519
+ base = `${ regExpToString . call ( value ) } ` ;
514
520
} else if ( isDate ( value ) ) {
515
521
if ( keyLength === 0 ) {
516
522
if ( Number . isNaN ( value . getTime ( ) ) )
517
523
return ctx . stylize ( value . toString ( ) , 'date' ) ;
518
524
return ctx . stylize ( dateToISOString . call ( value ) , 'date' ) ;
519
525
}
520
526
// Make dates with properties first say the date
521
- base = ` ${ dateToISOString . call ( value ) } ` ;
527
+ base = `${ dateToISOString . call ( value ) } ` ;
522
528
} else if ( isError ( value ) ) {
523
529
// Make error with message first say the error
524
530
if ( keyLength === 0 )
525
531
return formatError ( value ) ;
526
- base = ` ${ formatError ( value ) } ` ;
532
+ base = `${ formatError ( value ) } ` ;
527
533
} else if ( isAnyArrayBuffer ( value ) ) {
528
534
// Fast path for ArrayBuffer and SharedArrayBuffer.
529
535
// Can't do the same for DataView because it has a non-primitive
@@ -553,13 +559,13 @@ function formatValue(ctx, value, recurseTimes, ln) {
553
559
const formatted = formatPrimitive ( stylizeNoColor , raw ) ;
554
560
if ( keyLength === 0 )
555
561
return ctx . stylize ( `[Number: ${ formatted } ]` , 'number' ) ;
556
- base = ` [Number: ${ formatted } ]` ;
562
+ base = `[Number: ${ formatted } ]` ;
557
563
} else if ( typeof raw === 'boolean' ) {
558
564
// Make boxed primitive Booleans look like such
559
565
const formatted = formatPrimitive ( stylizeNoColor , raw ) ;
560
566
if ( keyLength === 0 )
561
567
return ctx . stylize ( `[Boolean: ${ formatted } ]` , 'boolean' ) ;
562
- base = ` [Boolean: ${ formatted } ]` ;
568
+ base = `[Boolean: ${ formatted } ]` ;
563
569
} else if ( typeof raw === 'symbol' ) {
564
570
const formatted = formatPrimitive ( stylizeNoColor , raw ) ;
565
571
return ctx . stylize ( `[Symbol: ${ formatted } ]` , 'symbol' ) ;
@@ -603,9 +609,42 @@ function formatNumber(fn, value) {
603
609
return fn ( `${ value } ` , 'number' ) ;
604
610
}
605
611
606
- function formatPrimitive ( fn , value ) {
607
- if ( typeof value === 'string' )
612
+ function formatPrimitive ( fn , value , ctx ) {
613
+ if ( typeof value === 'string' ) {
614
+ if ( ctx . compact === false &&
615
+ value . length > MIN_LINE_LENGTH &&
616
+ ctx . indentationLvl + value . length > ctx . breakLength ) {
617
+ // eslint-disable-next-line max-len
618
+ const minLineLength = Math . max ( ctx . breakLength - ctx . indentationLvl , MIN_LINE_LENGTH ) ;
619
+ // eslint-disable-next-line max-len
620
+ const averageLineLength = Math . ceil ( value . length / Math . ceil ( value . length / minLineLength ) ) ;
621
+ const divisor = Math . max ( averageLineLength , MIN_LINE_LENGTH ) ;
622
+ var res = '' ;
623
+ if ( readableRegExps [ divisor ] === undefined ) {
624
+ // Build a new RegExp that naturally breaks text into multiple lines.
625
+ //
626
+ // Rules
627
+ // 1. Greedy match all text up the max line length that ends with a
628
+ // whitespace or the end of the string.
629
+ // 2. If none matches, non-greedy match any text up to a whitespace or
630
+ // the end of the string.
631
+ //
632
+ // eslint-disable-next-line max-len, no-unescaped-regexp-dot
633
+ readableRegExps [ divisor ] = new RegExp ( `(.|\\n){1,${ divisor } }(\\s|$)|(\\n|.)+?(\\s|$)` , 'gm' ) ;
634
+ }
635
+ const indent = ' ' . repeat ( ctx . indentationLvl ) ;
636
+ const matches = value . match ( readableRegExps [ divisor ] ) ;
637
+ if ( matches . length > 1 ) {
638
+ res += `${ fn ( strEscape ( matches [ 0 ] ) , 'string' ) } +\n` ;
639
+ for ( var i = 1 ; i < matches . length - 1 ; i ++ ) {
640
+ res += `${ indent } ${ fn ( strEscape ( matches [ i ] ) , 'string' ) } +\n` ;
641
+ }
642
+ res += `${ indent } ${ fn ( strEscape ( matches [ i ] ) , 'string' ) } ` ;
643
+ return res ;
644
+ }
645
+ }
608
646
return fn ( strEscape ( value ) , 'string' ) ;
647
+ }
609
648
if ( typeof value === 'number' )
610
649
return formatNumber ( fn , value ) ;
611
650
if ( typeof value === 'boolean' )
@@ -806,7 +845,7 @@ function formatProperty(ctx, value, recurseTimes, key, array) {
806
845
const desc = Object . getOwnPropertyDescriptor ( value , key ) ||
807
846
{ value : value [ key ] , enumerable : true } ;
808
847
if ( desc . value !== undefined ) {
809
- const diff = array === 0 ? 3 : 2 ;
848
+ const diff = array !== 0 || ctx . compact === false ? 2 : 3 ;
810
849
ctx . indentationLvl += diff ;
811
850
str = formatValue ( ctx , desc . value , recurseTimes , array === 0 ) ;
812
851
ctx . indentationLvl -= diff ;
@@ -839,25 +878,36 @@ function formatProperty(ctx, value, recurseTimes, key, array) {
839
878
840
879
function reduceToSingleString ( ctx , output , base , braces , addLn ) {
841
880
const breakLength = ctx . breakLength ;
881
+ var i = 0 ;
882
+ if ( ctx . compact === false ) {
883
+ const indentation = ' ' . repeat ( ctx . indentationLvl ) ;
884
+ var res = `${ base ? `${ base } ` : '' } ${ braces [ 0 ] } \n${ indentation } ` ;
885
+ for ( ; i < output . length - 1 ; i ++ ) {
886
+ res += `${ output [ i ] } ,\n${ indentation } ` ;
887
+ }
888
+ res += `${ output [ i ] } \n${ indentation } ${ braces [ 1 ] } ` ;
889
+ return res ;
890
+ }
842
891
if ( output . length * 2 <= breakLength ) {
843
892
var length = 0 ;
844
- for ( var i = 0 ; i < output . length && length <= breakLength ; i ++ ) {
893
+ for ( ; i < output . length && length <= breakLength ; i ++ ) {
845
894
if ( ctx . colors ) {
846
895
length += removeColors ( output [ i ] ) . length + 1 ;
847
896
} else {
848
897
length += output [ i ] . length + 1 ;
849
898
}
850
899
}
851
900
if ( length <= breakLength )
852
- return `${ braces [ 0 ] } ${ base } ${ join ( output , ', ' ) } ${ braces [ 1 ] } ` ;
901
+ return `${ braces [ 0 ] } ${ base ? ` ${ base } ` : '' } ${ join ( output , ', ' ) } ` +
902
+ braces [ 1 ] ;
853
903
}
854
904
// If the opening "brace" is too large, like in the case of "Set {",
855
905
// we need to force the first item to be on the next line or the
856
906
// items will not line up correctly.
857
907
const indentation = ' ' . repeat ( ctx . indentationLvl ) ;
858
908
const extraLn = addLn === true ? `\n${ indentation } ` : '' ;
859
909
const ln = base === '' && braces [ 0 ] . length === 1 ?
860
- ' ' : `${ base } \n${ indentation } ` ;
910
+ ' ' : `${ base ? ` ${ base } ` : base } \n${ indentation } ` ;
861
911
const str = join ( output , `,\n${ indentation } ` ) ;
862
912
return `${ extraLn } ${ braces [ 0 ] } ${ ln } ${ str } ${ braces [ 1 ] } ` ;
863
913
}
0 commit comments