Skip to content

Commit 3ddb3e4

Browse files
committed
[New] add support for WeakMap and WeakSet
1 parent 36d47b9 commit 3ddb3e4

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

.eslintrc

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55
"complexity": 0,
66
"func-style": [2, 'declaration'],
77
"indent": [2, 4],
8-
"max-lines-per-function": [2, 120],
8+
"max-lines-per-function": [2, 130],
99
"max-params": [2, 4],
10-
"max-statements": [2, 80],
10+
"max-statements": [2, 90],
1111
"max-statements-per-line": [2, { "max": 2 }],
1212
"no-magic-numbers": 0,
1313
"no-param-reassign": 1,
1414
"strict": 0, // TODO
1515
},
1616
"globals": {
1717
"BigInt": false,
18+
"WeakSet": false,
19+
"WeakMap": false,
1820
},
1921
"overrides": [
2022
{

.nycrc

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"instrumentation": false,
55
"sourceMap": false,
66
"reporter": ["html", "text-summary"],
7-
"lines": 94,
8-
"statements": 94,
7+
"lines": 93,
8+
"statements": 93,
99
"functions": 96,
10-
"branches": 90,
10+
"branches": 89,
1111
"exclude": [
1212
"coverage",
1313
"example",

index.js

+46
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ var hasSet = typeof Set === 'function' && Set.prototype;
66
var setSizeDescriptor = Object.getOwnPropertyDescriptor && hasSet ? Object.getOwnPropertyDescriptor(Set.prototype, 'size') : null;
77
var setSize = hasSet && setSizeDescriptor && typeof setSizeDescriptor.get === 'function' ? setSizeDescriptor.get : null;
88
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;
913
var booleanValueOf = Boolean.prototype.valueOf;
1014
var objectToString = Object.prototype.toString;
1115
var match = String.prototype.match;
@@ -113,6 +117,12 @@ module.exports = function inspect_(obj, options, depth, seen) {
113117
});
114118
return collectionOf('Set', setSize.call(obj), setParts);
115119
}
120+
if (isWeakMap(obj)) {
121+
return weakCollectionOf('WeakMap');
122+
}
123+
if (isWeakSet(obj)) {
124+
return weakCollectionOf('WeakSet');
125+
}
116126
if (isNumber(obj)) {
117127
return markBoxed(inspect(Number(obj)));
118128
}
@@ -192,6 +202,22 @@ function isMap(x) {
192202
return false;
193203
}
194204

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+
195221
function isSet(x) {
196222
if (!setSize || !x || typeof x !== 'object') {
197223
return false;
@@ -208,6 +234,22 @@ function isSet(x) {
208234
return false;
209235
}
210236

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+
211253
function isElement(x) {
212254
if (!x || typeof x !== 'object') { return false; }
213255
if (typeof HTMLElement !== 'undefined' && x instanceof HTMLElement) {
@@ -235,6 +277,10 @@ function markBoxed(str) {
235277
return 'Object(' + str + ')';
236278
}
237279

280+
function weakCollectionOf(type) {
281+
return type + ' { ? }';
282+
}
283+
238284
function collectionOf(type, size, entries) {
239285
return type + ' (' + size + ') {' + entries.join(', ') + '}';
240286
}

test/values.js

+20
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ test('Map', { skip: typeof Map !== 'function' }, function (t) {
8787
t.end();
8888
});
8989

90+
test('WeakMap', { skip: typeof WeakMap !== 'function' }, function (t) {
91+
var map = new WeakMap();
92+
map.set({ a: 1 }, ['b']);
93+
var expectedString = 'WeakMap { ? }';
94+
t.equal(inspect(map), expectedString, 'new WeakMap([[{ a: 1 }, ["b"]]]) should not show size or contents');
95+
t.equal(inspect(new WeakMap()), 'WeakMap { ? }', 'empty WeakMap should not show as empty');
96+
97+
t.end();
98+
});
99+
90100
test('Set', { skip: typeof Set !== 'function' }, function (t) {
91101
var set = new Set();
92102
set.add({ a: 1 });
@@ -103,6 +113,16 @@ test('Set', { skip: typeof Set !== 'function' }, function (t) {
103113
t.end();
104114
});
105115

116+
test('WeakSet', { skip: typeof WeakSet !== 'function' }, function (t) {
117+
var map = new WeakSet();
118+
map.add({ a: 1 });
119+
var expectedString = 'WeakSet { ? }';
120+
t.equal(inspect(map), expectedString, 'new WeakSet([{ a: 1 }]) should not show size or contents');
121+
t.equal(inspect(new WeakSet()), 'WeakSet { ? }', 'empty WeakSet should not show as empty');
122+
123+
t.end();
124+
});
125+
106126
test('Strings', function (t) {
107127
var str = 'abc';
108128

0 commit comments

Comments
 (0)