Skip to content

Commit ec2ba80

Browse files
authored
Merge pull request #2103 from immutable-js/merge-empty-record
2 parents 29431bd + d51af77 commit ec2ba80

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

__tests__/merge.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,21 @@ describe('merge', () => {
329329
const b = Map({ a: Map([[0, Map({ y: 2 })]]) });
330330
expect(mergeDeep(a, b)).toEqual({ a: Map([[0, Map({ y: 2 })]]) });
331331
});
332+
333+
it('works with an empty Record', () => {
334+
class MyRecord extends Record({ a: 1 }) {}
335+
336+
const myRecord = new MyRecord();
337+
expect(merge(myRecord, { a: 4 })).toEqual(
338+
new MyRecord({
339+
a: 4,
340+
})
341+
);
342+
343+
class MyEmptyRecord extends Record({}) {}
344+
345+
const myEmptyRecord = new MyEmptyRecord();
346+
// merging with an empty record should return the same empty record instance
347+
expect(merge(myEmptyRecord, { a: 4 })).toBe(myEmptyRecord);
348+
});
332349
});

src/methods/merge.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { KeyedCollection } from '../Collection';
22
import { NOT_SET } from '../TrieUtils';
33
import { update } from '../functional/update';
4+
import { isRecord } from '../predicates/isRecord';
45

56
export function merge(...iters) {
67
return mergeIntoKeyedWith(this, iters);
@@ -29,7 +30,9 @@ function mergeIntoKeyedWith(collection, collections, merger) {
2930
!collection.__ownerID &&
3031
iters.length === 1
3132
) {
32-
return collection.constructor(iters[0]);
33+
return isRecord(collection)
34+
? collection // Record is empty and will not be updated: return the same instance
35+
: collection.constructor(iters[0]);
3336
}
3437
return collection.withMutations((collection) => {
3538
const mergeIntoCollection = merger

0 commit comments

Comments
 (0)