-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
merge.js
48 lines (45 loc) · 1.3 KB
/
merge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { KeyedCollection } from '../Collection';
import { NOT_SET } from '../TrieUtils';
import { update } from '../functional/update';
export function merge(...iters) {
return mergeIntoKeyedWith(this, iters);
}
export function mergeWith(merger, ...iters) {
if (typeof merger !== 'function') {
throw new TypeError('Invalid merger function: ' + merger);
}
return mergeIntoKeyedWith(this, iters, merger);
}
function mergeIntoKeyedWith(collection, collections, merger) {
const iters = [];
for (let ii = 0; ii < collections.length; ii++) {
const collection = KeyedCollection(collections[ii]);
if (collection.size !== 0) {
iters.push(collection);
}
}
if (iters.length === 0) {
return collection;
}
if (
collection.toSeq().size === 0 &&
!collection.__ownerID &&
iters.length === 1
) {
return collection.constructor(iters[0]);
}
return collection.withMutations(collection => {
const mergeIntoCollection = merger
? (value, key) => {
update(collection, key, NOT_SET, oldVal =>
oldVal === NOT_SET ? value : merger(oldVal, value, key)
);
}
: (value, key) => {
collection.set(key, value);
};
for (let ii = 0; ii < iters.length; ii++) {
iters[ii].forEach(mergeIntoCollection);
}
});
}