@@ -6,6 +6,10 @@ var hasSet = typeof Set === 'function' && Set.prototype;
6
6
var setSizeDescriptor = Object . getOwnPropertyDescriptor && hasSet ? Object . getOwnPropertyDescriptor ( Set . prototype , 'size' ) : null ;
7
7
var setSize = hasSet && setSizeDescriptor && typeof setSizeDescriptor . get === 'function' ? setSizeDescriptor . get : null ;
8
8
var setForEach = hasSet && Set . prototype . forEach ;
9
+ var hasWeakMap = typeof WeakMap === 'function' && WeakMap . prototype ;
10
+ var weakMapHas = hasWeakMap ? WeakMap . prototype . has : null ;
11
+ var hasWeakSet = typeof WeakSet === 'function' && WeakSet . prototype ;
12
+ var weakSetHas = hasWeakSet ? WeakSet . prototype . has : null ;
9
13
var booleanValueOf = Boolean . prototype . valueOf ;
10
14
var objectToString = Object . prototype . toString ;
11
15
var match = String . prototype . match ;
@@ -113,6 +117,12 @@ module.exports = function inspect_(obj, options, depth, seen) {
113
117
} ) ;
114
118
return collectionOf ( 'Set' , setSize . call ( obj ) , setParts ) ;
115
119
}
120
+ if ( isWeakMap ( obj ) ) {
121
+ return weakCollectionOf ( 'WeakMap' ) ;
122
+ }
123
+ if ( isWeakSet ( obj ) ) {
124
+ return weakCollectionOf ( 'WeakSet' ) ;
125
+ }
116
126
if ( isNumber ( obj ) ) {
117
127
return markBoxed ( inspect ( Number ( obj ) ) ) ;
118
128
}
@@ -192,6 +202,22 @@ function isMap(x) {
192
202
return false ;
193
203
}
194
204
205
+ function isWeakMap ( x ) {
206
+ if ( ! weakMapHas || ! x || typeof x !== 'object' ) {
207
+ return false ;
208
+ }
209
+ try {
210
+ weakMapHas . call ( x , weakMapHas ) ;
211
+ try {
212
+ weakSetHas . call ( x , weakSetHas ) ;
213
+ } catch ( s ) {
214
+ return true ;
215
+ }
216
+ return x instanceof WeakMap ; // core-js workaround, pre-v2.5.0
217
+ } catch ( e ) { }
218
+ return false ;
219
+ }
220
+
195
221
function isSet ( x ) {
196
222
if ( ! setSize || ! x || typeof x !== 'object' ) {
197
223
return false ;
@@ -208,6 +234,22 @@ function isSet(x) {
208
234
return false ;
209
235
}
210
236
237
+ function isWeakSet ( x ) {
238
+ if ( ! weakSetHas || ! x || typeof x !== 'object' ) {
239
+ return false ;
240
+ }
241
+ try {
242
+ weakSetHas . call ( x , weakSetHas ) ;
243
+ try {
244
+ weakMapHas . call ( x , weakMapHas ) ;
245
+ } catch ( s ) {
246
+ return true ;
247
+ }
248
+ return x instanceof WeakSet ; // core-js workaround, pre-v2.5.0
249
+ } catch ( e ) { }
250
+ return false ;
251
+ }
252
+
211
253
function isElement ( x ) {
212
254
if ( ! x || typeof x !== 'object' ) { return false ; }
213
255
if ( typeof HTMLElement !== 'undefined' && x instanceof HTMLElement ) {
@@ -235,6 +277,10 @@ function markBoxed(str) {
235
277
return 'Object(' + str + ')' ;
236
278
}
237
279
280
+ function weakCollectionOf ( type ) {
281
+ return type + ' { ? }' ;
282
+ }
283
+
238
284
function collectionOf ( type , size , entries ) {
239
285
return type + ' (' + size + ') {' + entries . join ( ', ' ) + '}' ;
240
286
}
0 commit comments