11/**
22 * @file util.js
3+ * Node.js-style util.inspect implementation, largely based on
4+ * https://github.com/nodejs/node/blob/v8.17.0/lib/util.js.
5+ *
36 * @author Tom Tang <[email protected] > 47 * @date June 2023
58 */
@@ -532,7 +535,8 @@ function formatValue(ctx, value, recurseTimes, ln) {
532535 // Make error with message first say the error
533536 if ( keyLength === 0 )
534537 return formatError ( ctx , value ) ;
535- base = ` ${ formatError ( ctx , value ) } ` ;
538+ base = ` ${ formatError ( ctx , value ) } \n` ;
539+ braces . length = 0 ;
536540 } else if ( isAnyArrayBuffer ( value ) ) {
537541 // Fast path for ArrayBuffer and SharedArrayBuffer.
538542 // Can't do the same for DataView because it has a non-primitive
@@ -647,10 +651,11 @@ function formatError(ctx, error)
647651 . split ( '\n' )
648652 . filter ( a => a . length > 0 )
649653 . map ( a => ` ${ a } ` ) ;
650- return ( `${ error . name } : ${ error . message } \n`
651- + stackEls [ 0 ] + '\n'
652- + style ( stackEls . slice ( 1 ) . join ( '\n' ) )
653- ) ;
654+ const retstr =
655+ `${ error . name } : ${ error . message } \n`
656+ + stackEls [ 0 ] + '\n'
657+ + style ( stackEls . slice ( 1 ) . join ( '\n' ) ) ;
658+ return retstr ;
654659}
655660
656661function formatObject ( ctx , value , recurseTimes , keys ) {
@@ -871,7 +876,12 @@ function reduceToSingleString(ctx, output, base, braces, addLn) {
871876 }
872877 }
873878 if ( length <= breakLength )
874- return `${ braces [ 0 ] } ${ base } ${ join ( output , ', ' ) } ${ braces [ 1 ] } ` ;
879+ {
880+ if ( braces . length )
881+ return `${ braces [ 0 ] } ${ base } ${ join ( output , ', ' ) } ${ braces [ 1 ] } ` ;
882+ else
883+ return `${ base } ${ join ( output , ', ' ) } ` ;
884+ }
875885 }
876886 // If the opening "brace" is too large, like in the case of "Set {",
877887 // we need to force the first item to be on the next line or the
@@ -881,6 +891,7 @@ function reduceToSingleString(ctx, output, base, braces, addLn) {
881891 const ln = base === '' && braces [ 0 ] . length === 1 ?
882892 ' ' : `${ base } \n${ indentation } ` ;
883893 const str = join ( output , `,\n${ indentation } ` ) ;
894+
884895 return `${ extraLn } ${ braces [ 0 ] } ${ ln } ${ str } ${ braces [ 1 ] } ` ;
885896}
886897
0 commit comments