Skip to content

Commit

Permalink
Merge pull request #795 from davidbonnet/record-noop-set
Browse files Browse the repository at this point in the history
Updated `Record` to return itself on no-op `set` call
  • Loading branch information
leebyron committed Feb 23, 2016
2 parents 850b3f7 + d2537c7 commit 7360dfd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
20 changes: 20 additions & 0 deletions __tests__/Record.ts
Expand Up @@ -94,4 +94,24 @@ describe('Record', () => {
expect(t.get('c')).toBeUndefined();
})

it('returns itself when setting identical values', () => {
var MyType = Record({a:1, b:2});
var t1 = new MyType;
var t2 = new MyType({a: 1});
var t3 = t1.set('a', 1);
var t4 = t2.set('a', 1);
expect(t3).toBe(t1);
expect(t4).toBe(t2);
})

it('returns new record when setting new values', () => {
var MyType = Record({a:1, b:2});
var t1 = new MyType;
var t2 = new MyType({a: 1});
var t3 = t1.set('a', 3);
var t4 = t2.set('a', 3);
expect(t3).not.toBe(t1);
expect(t4).not.toBe(t2);
})

});
6 changes: 6 additions & 0 deletions dist/immutable.js
Expand Up @@ -3682,6 +3682,12 @@
if (!this.has(k)) {
throw new Error('Cannot set unknown key "' + k + '" on ' + recordName(this));
}
if (this._map && !this._map.has(k)) {
var defaultVal = this._defaultValues[k];
if (v === defaultVal) {
return this;
}
}
var newMap = this._map && this._map.set(k, v);
if (this.__ownerID || newMap === this._map) {
return this;
Expand Down

0 comments on commit 7360dfd

Please sign in to comment.