(collection, sources, merger)
| 29 | } |
| 30 | |
| 31 | export 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 | |
| 77 | function deepMergerWith(merger) { |
| 78 | function deepMerger(oldValue, newValue, key) { |
no test coverage detected