Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a22ece7

Browse files
acustikozlitinaelja
authored andcommitted
Fix collection size check in merge() (immutable-js#1521)
Records can have a size property (`collection.size`, in this conditional) that is independent of the Collection.size property; using toSeq() ensures that the Collection.size property is checked instead
1 parent 7f4e616 commit a22ece7

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

__tests__/merge.ts

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
merge,
1616
mergeDeep,
1717
mergeDeepWith,
18+
Record,
1819
Set,
1920
} from '../';
2021

@@ -229,4 +230,9 @@ describe('merge', () => {
229230
Map([[a, Map([[b, Map([[c, 10], [d, 2], [e, 20], [f, 30], [g, 40]])]])]])
230231
);
231232
});
233+
234+
it('merges records with a size property set to 0', () => {
235+
const Sizable = Record({ size: 0 });
236+
expect(Sizable().merge({ size: 123 }).size).toBe(123);
237+
});
232238
});

src/methods/merge.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ function mergeIntoKeyedWith(collection, collections, merger) {
2828
if (iters.length === 0) {
2929
return collection;
3030
}
31-
if (collection.size === 0 && !collection.__ownerID && iters.length === 1) {
31+
if (
32+
collection.toSeq().size === 0 &&
33+
!collection.__ownerID &&
34+
iters.length === 1
35+
) {
3236
return collection.constructor(iters[0]);
3337
}
3438
return collection.withMutations(collection => {

0 commit comments

Comments
 (0)