Skip to content

Commit

Permalink
[Bugfix beta] don’t leak -0 as a key
Browse files Browse the repository at this point in the history
* some tests for NaN support
  • Loading branch information
stefanpenner committed Oct 2, 2014
1 parent 63df972 commit ee9c6ab
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/ember-metal/lib/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,11 @@ Map.prototype = {
var values = this.values;
var guid = guidFor(key);

keys.add(key, guid);
// ensure we don't store -0
var k = key === -0 ? 0 : key;

keys.add(k, guid);

values[guid] = value;

this.size = keys.size;
Expand Down
43 changes: 43 additions & 0 deletions packages/ember-metal/tests/map_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,49 @@ function testMap(nameAndFunc) {
});
equal(iterations, 0);
});

test("-0", function() {
equal(map.has(-0), false);
equal(map.has(0), false);

map.set(-0, 'zero');

equal(map.has(-0), true);
equal(map.has(0), true);

equal(map.get(0), 'zero');
equal(map.get(-0), 'zero');

map.forEach(function(value, key) {
equal(1/key, Infinity, 'spec says key should be positive zero');
});
});

test("NaN", function() {
equal(map.has(NaN), false);

map.set(NaN, 'not-a-number');

equal(map.has(NaN), true);

equal(map.get(NaN), 'not-a-number');

});

test("NaN Boxed", function() {
//jshint -W053
var boxed = new Number(NaN);
equal(map.has(boxed), false);

map.set(boxed, 'not-a-number');

equal(map.has(boxed), true);
equal(map.has(NaN), false);

equal(map.get(NaN), undefined);
equal(map.get(boxed), 'not-a-number');
});

}

for (var i = 0; i < varieties.length; i++) {
Expand Down

0 comments on commit ee9c6ab

Please sign in to comment.