8000 Updated `Record` to return itself on no-op `set` call · githubsheng/immutable-js@d2537c7 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit d2537c7

Browse files
committed
Updated Record to return itself on no-op set call
Updated record tests Fixes immutable-js#558 and immutable-js#536
1 parent 850b3f7 commit d2537c7

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

__tests__/Record.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,24 @@ describe('Record', () => {
9494
expect(t.get('c')).toBeUndefined();
9595
})
9696

97+
it('returns itself when setting identical values', () => {
98+
var MyType = Record({a:1, b:2});
99+
var t1 = new MyType;
100+
var t2 = new MyType({a: 1});
101+
var t3 = t1.set('a', 1);
102+
var t4 = t2.set('a', 1);
103+
expect(t3).toBe(t1);
104+
expect(t4).toBe(t2);
105+
})
106+
107+
it('returns new record when setting new values', () => {
108+
var MyType = Record({a:1, b:2});
109+
var t1 = new MyType;
110+
var t2 = new MyType({a: 1});
111+
var t3 = t1.set('a', 3);
112+
var t4 = t2.set('a', 3);
113+
expect(t3).not.toBe(t1);
114+
expect(t4).not.toBe(t2);
115+
})
116+
97117
});

dist/immutable.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3682,6 +3682,12 @@
36823682
if (!this.has(k)) {
36833683
throw new Error('Cannot set unknown key "' + k + '" on ' + recordName(this));
36843684
}
3685+
if (this._map && !this._map.has(k)) {
3686+
var defaultVal = this._defaultValues[k];
3687+
if (v === defaultVal) {
3688+
return this;
3689+
}
3690+
}
36853691
var newMap = this._map && this._map.set(k, v);
36863692
if (this.__ownerID || newMap === this._map) {
36873693
return this;

0 commit comments

Comments
 (0)
0