@@ -32,7 +32,9 @@ const { isBuffer } = require('buffer').Buffer;
32
32
33
33
const {
34
34
previewMapIterator,
35
- previewSetIterator
35
+ previewSetIterator,
36
+ previewWeakMap,
37
+ previewWeakSet
36
38
} = require ( 'internal/v8' ) ;
37
39
38
40
const {
@@ -54,6 +56,8 @@ const {
54
56
isPromise,
55
57
isSet,
56
58
isSetIterator,
59
+ isWeakMap,
60
+ isWeakSet,
57
61
isRegExp,
58
62
isDate,
59
63
isTypedArray
@@ -291,6 +295,8 @@ function inspect(value, opts) {
291
295
colors : inspectDefaultOptions . colors ,
292
296
customInspect : inspectDefaultOptions . customInspect ,
293
297
showProxy : inspectDefaultOptions . showProxy ,
298
+ // TODO(BridgeAR): Deprecate `maxArrayLength` and replace it with
299
+ // `maxEntries`.
294
300
maxArrayLength : inspectDefaultOptions . maxArrayLength ,
295
301
breakLength : inspectDefaultOptions . breakLength ,
296
302
indentationLvl : 0 ,
@@ -328,6 +334,8 @@ Object.defineProperty(inspect, 'defaultOptions', {
328
334
if ( options === null || typeof options !== 'object' ) {
329
335
throw new ERR_INVALID_ARG_TYPE ( 'options' , 'Object' ) ;
330
336
}
337
+ // TODO(BridgeAR): Add input validation and make sure `defaultOptions` are
338
+ // not configurable.
331
339
return _extend ( inspectDefaultOptions , options ) ;
332
340
}
333
341
} ) ;
@@ -465,6 +473,7 @@ function formatValue(ctx, value, recurseTimes, ln) {
465
473
let braces ;
466
474
let noIterator = true ;
467
475
let raw ;
476
+ let extra ;
468
477
469
478
// Iterators and the rest are split to reduce checks
470
479
if ( value [ Symbol . iterator ] ) {
@@ -562,6 +571,20 @@ function formatValue(ctx, value, recurseTimes, ln) {
562
571
} else if ( isPromise ( value ) ) {
563
572
braces [ 0 ] = `${ prefix } {` ;
564
573
formatter = formatPromise ;
574
+ } else if ( isWeakSet ( value ) ) {
575
+ braces [ 0 ] = `${ prefix } {` ;
576
+ if ( ctx . showHidden ) {
577
+ formatter = formatWeakSet ;
578
+ } else {
579
+ extra = '[items unknown]' ;
580
+ }
581
+ } else if ( isWeakMap ( value ) ) {
582
+ braces [ 0 ] = `${ prefix } {` ;
583
+ if ( ctx . showHidden ) {
584
+ formatter = formatWeakMap ;
585
+ } else {
586
+ extra = '[items unknown]' ;
587
+ }
565
588
} else {
566
589
// Check boxed primitives other than string with valueOf()
567
590
// NOTE: `Date` has to be checked first!
@@ -616,6 +639,9 @@ function formatValue(ctx, value, recurseTimes, ln) {
616
639
ctx . seen . push ( value ) ;
617
640
const output = formatter ( ctx , value , recurseTimes , keys ) ;
618
641
642
+ if ( extra !== undefined )
643
+ output . unshift ( extra ) ;
644
+
619
645
for ( var i = 0 ; i < symbols . length ; i ++ ) {
620
646
output . push ( formatProperty ( ctx , value , recurseTimes , symbols [ i ] , 0 ) ) ;
621
647
}
@@ -839,6 +865,46 @@ function formatMap(ctx, value, recurseTimes, keys) {
839
865
return output ;
840
866
}
841
867
868
+ function formatWeakSet ( ctx , value , recurseTimes , keys ) {
869
+ const maxArrayLength = Math . max ( ctx . maxArrayLength , 0 ) ;
870
+ const entries = previewWeakSet ( value , maxArrayLength + 1 ) ;
871
+ const maxLength = Math . min ( maxArrayLength , entries . length ) ;
872
+ let output = new Array ( maxLength ) ;
873
+ for ( var i = 0 ; i < maxLength ; ++ i )
874
+ output [ i ] = formatValue ( ctx , entries [ i ] , recurseTimes ) ;
875
+ // Sort all entries to have a halfway reliable output (if more entries than
876
+ // retrieved ones exist, we can not reliably return the same output).
877
+ output = output . sort ( ) ;
878
+ if ( entries . length > maxArrayLength )
879
+ output . push ( '... more items' ) ;
880
+ for ( i = 0 ; i < keys . length ; i ++ )
881
+ output . push ( formatProperty ( ctx , value , recurseTimes , keys [ i ] , 0 ) ) ;
882
+ return output ;
883
+ }
884
+
885
+ function formatWeakMap ( ctx , value , recurseTimes , keys ) {
886
+ const maxArrayLength = Math . max ( ctx . maxArrayLength , 0 ) ;
887
+ const entries = previewWeakMap ( value , maxArrayLength + 1 ) ;
888
+ // Entries exist as [key1, val1, key2, val2, ...]
889
+ const remainder = entries . length / 2 > maxArrayLength ;
890
+ const len = entries . length / 2 - ( remainder ? 1 : 0 ) ;
891
+ const maxLength = Math . min ( maxArrayLength , len ) ;
892
+ let output = new Array ( maxLength ) ;
893
+ for ( var i = 0 ; i < len ; i ++ ) {
894
+ const pos = i * 2 ;
895
+ output [ i ] = `${ formatValue ( ctx , entries [ pos ] , recurseTimes ) } => ` +
896
+ formatValue ( ctx , entries [ pos + 1 ] , recurseTimes ) ;
897
+ }
898
+ // Sort all entries to have a halfway reliable output (if more entries than
899
+ // retrieved ones exist, we can not reliably return the same output).
900
+ output = output . sort ( ) ;
901
+ if ( remainder > 0 )
902
+ output . push ( '... more items' ) ;
903
+ for ( i = 0 ; i < keys . length ; i ++ )
904
+ output . push ( formatProperty ( ctx , value , recurseTimes , keys [ i ] , 0 ) ) ;
905
+ return output ;
906
+ }
907
+
842
908
function formatCollectionIterator ( preview , ctx , value , recurseTimes , keys ) {
843
909
const output = [ ] ;
844
910
for ( const entry of preview ( value ) ) {
0 commit comments