@@ -513,13 +513,6 @@ function getPrefix(constructor, tag, fallback) {
513
513
return '' ;
514
514
}
515
515
516
- function addExtraKeys ( source , target , keys ) {
517
- for ( const key of keys ) {
518
- target [ key ] = source [ key ] ;
519
- }
520
- return target ;
521
- }
522
-
523
516
function findTypedConstructor ( value ) {
524
517
for ( const [ check , clazz ] of [
525
518
[ isUint8Array , Uint8Array ] ,
@@ -535,14 +528,36 @@ function findTypedConstructor(value) {
535
528
[ isBigUint64Array , BigUint64Array ]
536
529
] ) {
537
530
if ( check ( value ) ) {
538
- return new clazz ( value ) ;
531
+ return clazz ;
539
532
}
540
533
}
541
- return value ;
542
534
}
543
535
544
536
const getBoxedValue = formatPrimitive . bind ( null , stylizeNoColor ) ;
545
537
538
+ function noPrototypeIterator ( ctx , value , recurseTimes ) {
539
+ let newVal ;
540
+ // TODO: Create a Subclass in case there's no prototype and show
541
+ // `null-prototype`.
542
+ if ( isSet ( value ) ) {
543
+ const clazz = Object . getPrototypeOf ( value ) || Set ;
544
+ newVal = new clazz ( setValues ( value ) ) ;
545
+ } else if ( isMap ( value ) ) {
546
+ const clazz = Object . getPrototypeOf ( value ) || Map ;
547
+ newVal = new clazz ( mapEntries ( value ) ) ;
548
+ } else if ( Array . isArray ( value ) ) {
549
+ const clazz = Object . getPrototypeOf ( value ) || Array ;
550
+ newVal = new clazz ( value . length || 0 ) ;
551
+ } else if ( isTypedArray ( value ) ) {
552
+ const clazz = findTypedConstructor ( value ) || Uint8Array ;
553
+ newVal = new clazz ( value ) ;
554
+ }
555
+ if ( newVal ) {
556
+ Object . defineProperties ( newVal , Object . getOwnPropertyDescriptors ( value ) ) ;
557
+ return formatValue ( ctx , newVal , recurseTimes ) ;
558
+ }
559
+ }
560
+
546
561
// Note: using `formatValue` directly requires the indentation level to be
547
562
// corrected by setting `ctx.indentationLvL += diff` and then to decrease the
548
563
// value afterwards again.
@@ -798,39 +813,25 @@ function formatValue(ctx, value, recurseTimes) {
798
813
braces = [ '{' , '}' ] ;
799
814
// The input prototype got manipulated. Special handle these.
800
815
// We have to rebuild the information so we are able to display everything.
801
- } else if ( isSet ( value ) ) {
802
- const newVal = addExtraKeys ( value , new Set ( setValues ( value ) ) , keys ) ;
803
- return formatValue ( ctx , newVal , recurseTimes ) ;
804
- } else if ( isMap ( value ) ) {
805
- const newVal = addExtraKeys ( value , new Map ( mapEntries ( value ) ) , keys ) ;
806
- return formatValue ( ctx , newVal , recurseTimes ) ;
807
- } else if ( Array . isArray ( value ) ) {
808
- // The prefix is not always possible to fully reconstruct.
809
- const prefix = getPrefix ( constructor , tag ) ;
810
- braces = [ `${ prefix === 'Array ' ? '' : prefix } [` , ']' ] ;
811
- formatter = formatArray ;
812
- const newValue = [ ] ;
813
- newValue . length = value . length ;
814
- value = addExtraKeys ( value , newValue , keys ) ;
815
- } else if ( isTypedArray ( value ) ) {
816
- const newValue = findTypedConstructor ( value ) ;
817
- value = addExtraKeys ( value , newValue , keys . slice ( newValue . length ) ) ;
818
- // The prefix is not always possible to fully reconstruct.
819
- braces = [ `${ getPrefix ( getConstructorName ( value ) , tag ) } [` , ']' ] ;
820
- formatter = formatTypedArray ;
821
- } else if ( isMapIterator ( value ) ) {
822
- braces = [ `[${ tag || 'Map Iterator' } ] {` , '}' ] ;
823
- formatter = formatMapIterator ;
824
- } else if ( isSetIterator ( value ) ) {
825
- braces = [ `[${ tag || 'Set Iterator' } ] {` , '}' ] ;
826
- formatter = formatSetIterator ;
827
- // Handle other regular objects again.
828
- } else if ( keyLength === 0 ) {
829
- if ( isExternal ( value ) )
830
- return ctx . stylize ( '[External]' , 'special' ) ;
831
- return `${ getPrefix ( constructor , tag ) } {}` ;
832
816
} else {
833
- braces [ 0 ] = `${ getPrefix ( constructor , tag ) } {` ;
817
+ const specialIterator = noPrototypeIterator ( ctx , value , recurseTimes ) ;
818
+ if ( specialIterator ) {
819
+ return specialIterator ;
820
+ }
821
+ if ( isMapIterator ( value ) ) {
822
+ braces = [ `[${ tag || 'Map Iterator' } ] {` , '}' ] ;
823
+ formatter = formatMapIterator ;
824
+ } else if ( isSetIterator ( value ) ) {
825
+ braces = [ `[${ tag || 'Set Iterator' } ] {` , '}' ] ;
826
+ formatter = formatSetIterator ;
827
+ // Handle other regular objects again.
828
+ } else if ( keyLength === 0 ) {
829
+ if ( isExternal ( value ) )
830
+ return ctx . stylize ( '[External]' , 'special' ) ;
831
+ return `${ getPrefix ( constructor , tag ) } {}` ;
832
+ } else {
833
+ braces [ 0 ] = `${ getPrefix ( constructor , tag ) } {` ;
834
+ }
834
835
}
835
836
}
836
837
0 commit comments