Skip to content

Commit

Permalink
Map#set should always return "this"
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Nov 15, 2014
1 parent 4709fae commit ed0a261
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,7 @@
// fast O(1) path
if (typeof this._storage[fkey] !== 'undefined') {
this._storage[fkey].value = value;
return;
return this;
} else {
entry = this._storage[fkey] = new MapEntry(key, value);
i = head.prev;
Expand All @@ -1735,7 +1735,7 @@
while ((i = i.next) !== head) {
if (ES.SameValueZero(i.key, key)) {
i.value = value;
return;
return this;
}
}
entry = entry || new MapEntry(key, value);
Expand Down
34 changes: 19 additions & 15 deletions test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Collections', function () {
testMapping = function (key, value) {
expect(map.has(key)).to.equal(false);
expect(map.get(key)).to.equal(undefined);
map.set(key, value);
expect(map.set(key, value)).to.equal(map);
expect(map.get(key)).to.equal(value);
expect(map.has(key)).to.equal(true);
};
Expand Down Expand Up @@ -105,7 +105,7 @@ describe('Collections', function () {
testMapping(+0, value1);
expect(map.has(-0)).to.equal(true);
expect(map.get(-0)).to.equal(value1);
map.set(-0, value2);
expect(map.set(-0, value2)).to.equal(map);
expect(map.get(-0)).to.equal(value2);
expect(map.get(+0)).to.equal(value2);
});
Expand Down Expand Up @@ -162,7 +162,7 @@ describe('Collections', function () {
testMapping(key, 'to-be-present');
expect(map.has(key)).to.equal(true);
expect(map.has({})).to.equal(false);
map.set(key, void 0);
expect(map.set(key, void 0)).to.equal(map);
expect(map.get(key)).to.equal(undefined);
expect(map.has(key)).to.equal(true);
expect(map.has({})).to.equal(false);
Expand All @@ -172,7 +172,7 @@ describe('Collections', function () {
expect(map.has(NaN)).to.equal(false);
expect(map.has(NaN + 1)).to.equal(false);
expect(map.has(23)).to.equal(false);
map.set(NaN, 'value');
expect(map.set(NaN, 'value')).to.equal(map);
expect(map.has(NaN)).to.equal(true);
expect(map.has(NaN + 1)).to.equal(true);
expect(map.has(23)).to.equal(false);
Expand Down Expand Up @@ -208,9 +208,9 @@ describe('Collections', function () {
});

it('should have keys, values and size props', function () {
map.set('a', 1);
map.set('b', 2);
map.set('c', 3);
expect(map.set('a', 1)).to.equal(map);
expect(map.set('b', 2)).to.equal(map);
expect(map.set('c', 3)).to.equal(map);
expect(typeof map.keys).to.equal('function');
expect(typeof map.values).to.equal('function');
expect(map.size).to.equal(3);
Expand All @@ -219,9 +219,9 @@ describe('Collections', function () {
});

it('should have an iterator that works with Array.from', function () {
map.set('a', 1);
map.set('b', NaN);
map.set('c', false);
expect(map.set('a', 1)).to.equal(map);
expect(map.set('b', NaN)).to.equal(map);
expect(map.set('c', false)).to.equal(map);
expect(Array.from(map)).to.eql([['a',1], ['b',NaN], ['c',false]]);
expect(Array.from(map.keys())).to.eql(['a', 'b', 'c']);
expect(Array.from(map.values())).to.eql([1, NaN, false]);
Expand All @@ -233,9 +233,9 @@ describe('Collections', function () {

beforeEach(function () {
map = new Map();
map.set('a', 1);
map.set('b', 2);
map.set('c', 3);
expect(map.set('a', 1)).to.equal(map);
expect(map.set('b', 2)).to.equal(map);
expect(map.set('c', 3)).to.equal(map);
});

afterEach(function () {
Expand All @@ -260,7 +260,7 @@ describe('Collections', function () {
var map = new Map();
var expectedKeys = [{}, null, undefined, '', NaN, 0];
expectedKeys.forEach(function (key) {
map.set(key, true);
expect(map.set(key, true)).to.equal(map);
});
var foundKeys = [];
map.forEach(function (value, key, entireMap) {
Expand All @@ -285,7 +285,7 @@ describe('Collections', function () {
var hasModifiedA = false;
map.forEach(function (value, key) {
if (!hasModifiedA && key === 'a') {
map.set('a', 4);
expect(map.set('a', 4)).to.equal(map);
hasModifiedA = true;
} else {
expect(key).not.to.equal('a');
Expand All @@ -295,6 +295,10 @@ describe('Collections', function () {

it('returns the map from #set() for chaining', function () {
expect(map.set({}, {})).to.equal(map);
expect(map.set(42, {})).to.equal(map);
expect(map.set(0, {})).to.equal(map);
expect(map.set(NaN, {})).to.equal(map);
expect(map.set(-0, {})).to.equal(map);
});

it('visits keys added in the iterator', function () {
Expand Down

0 comments on commit ed0a261

Please sign in to comment.