Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Add WeakMap.has and conform WeakMap.delete to spec
Browse files Browse the repository at this point in the history
  • Loading branch information
markfinger authored and arv committed Aug 1, 2014
1 parent fc3d02d commit 6662df5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
17 changes: 14 additions & 3 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
<!-- Tests -->
<script>
suite('WeakMap', function() {

var assert = chai.assert;

test('WeakMap has get and set functions', function() {
test('WeakMap has get, set, delete, and has functions', function() {
assert.isDefined(WeakMap.prototype.get);
assert.isDefined(WeakMap.prototype.set);
assert.isDefined(WeakMap.prototype.delete);
assert.isDefined(WeakMap.prototype.has);
});

test('WeakMap\'s get and set perform as expected', function() {
test('WeakMap\'s methods perform as expected', function() {
var wm = new WeakMap();

var o1 = {};
Expand All @@ -54,6 +55,16 @@
assert.equal(wm.get({}), undefined);
// `wm.get(o3)` should return undefined, because that is the set value
assert.equal(wm.get(o3), undefined);

assert.equal(wm.has(o1), true);
assert.equal(wm.has({}), false);

wm.delete(o1);
assert.equal(wm.get(o1), undefined);
assert.equal(wm.has(o1), false);
// Ensure that delete returns true/false indicating if the value was removed
assert.equal(wm.delete(o2), true);
assert.equal(wm.delete({}), false);
});
});
</script>
Expand Down
11 changes: 10 additions & 1 deletion weakmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ if (typeof WeakMap === 'undefined') {
entry[1] : undefined;
},
delete: function(key) {
this.set(key, undefined);
var entry = key[this.name];
if (!entry) return false;
var hasValue = entry[0] === key;
entry[0] = entry[1] = undefined;
return hasValue;
},
has: function(key) {
var entry = key[this.name];
if (!entry) return false;
return entry[0] === key;
}
};

Expand Down

0 comments on commit 6662df5

Please sign in to comment.