MCPcopy
hub / github.com/immutable-js/immutable-js / mergeWithSources

Function mergeWithSources

src/functional/merge.js:31–75  ·  view source on GitHub ↗
(collection, sources, merger)

Source from the content-addressed store, hash-verified

29}
30
31export function mergeWithSources(collection, sources, merger) {
32 if (!isDataStructure(collection)) {
33 throw new TypeError(
34 'Cannot merge into non-data-structure value: ' + collection
35 );
36 }
37 if (isImmutable(collection)) {
38 return typeof merger === 'function' && collection.mergeWith
39 ? collection.mergeWith(merger, ...sources)
40 : collection.merge
41 ? collection.merge(...sources)
42 : collection.concat(...sources);
43 }
44 const isArray = Array.isArray(collection);
45 let merged = collection;
46 const Collection = isArray ? IndexedCollection : KeyedCollection;
47 const mergeItem = isArray
48 ? (value) => {
49 // Copy on write
50 if (merged === collection) {
51 merged = shallowCopy(merged);
52 }
53 merged.push(value);
54 }
55 : (value, key) => {
56 if (isProtoKey(key)) {
57 return;
58 }
59
60 const hasVal = hasOwnProperty.call(merged, key);
61 const nextVal =
62 hasVal && merger ? merger(merged[key], value, key) : value;
63 if (!hasVal || nextVal !== merged[key]) {
64 // Copy on write
65 if (merged === collection) {
66 merged = shallowCopy(merged);
67 }
68 merged[key] = nextVal;
69 }
70 };
71 for (let i = 0; i < sources.length; i++) {
72 Collection(sources[i]).forEach(mergeItem);
73 }
74 return merged;
75}
76
77function deepMergerWith(merger) {
78 function deepMerger(oldValue, newValue, key) {

Callers 5

mergeInFunction · 0.90
mergeFunction · 0.85
mergeWithFunction · 0.85
mergeDeepWithSourcesFunction · 0.85
deepMergerFunction · 0.85

Calls 10

isImmutableFunction · 0.90
isProtoKeyFunction · 0.90
isDataStructureFunction · 0.85
shallowCopyFunction · 0.85
forEachMethod · 0.80
mergeWithMethod · 0.65
mergeMethod · 0.65
concatMethod · 0.65
pushMethod · 0.65
CollectionClass · 0.50

Tested by

no test coverage detected