|
| 1 | +function Version1deepMerge(a, b) { |
| 2 | + // Handle null/undefined cases |
| 3 | + if (a == null) return b; |
| 4 | + if (b == null) return a; |
| 5 | + |
| 6 | + // If either value is not an object, b takes precedence |
| 7 | + if (typeof a !== 'object' || typeof b !== 'object') { |
| 8 | + return b; |
| 9 | + } |
| 10 | + |
| 11 | + // Handle arrays - concatenate them |
| 12 | + if (Array.isArray(a) && Array.isArray(b)) { |
| 13 | + return [...a, ...b]; |
| 14 | + } |
| 15 | + |
| 16 | + // If one is array and other is object, prefer the object |
| 17 | + if (Array.isArray(a) !== Array.isArray(b)) { |
| 18 | + return Array.isArray(b) ? b : a; |
| 19 | + } |
| 20 | + |
| 21 | + // Merge objects |
| 22 | + const result = { ...a }; |
| 23 | + |
| 24 | + for (const key in b) { |
| 25 | + if (b.hasOwnProperty(key)) { |
| 26 | + if (key in result) { |
| 27 | + // Recursively merge nested objects |
| 28 | + result[key] = deepMerge(result[key], b[key]); |
| 29 | + } else { |
| 30 | + // Add new property from b |
| 31 | + result[key] = b[key]; |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return result; |
| 37 | +} |
| 38 | + |
| 39 | +function deepMerge(...objects) { |
| 40 | + // Filter out null/undefined objects |
| 41 | + const validObjects = objects.filter(obj => obj != null); |
| 42 | + |
| 43 | + if (validObjects.length === 0) return {}; |
| 44 | + if (validObjects.length === 1) return validObjects[0]; |
| 45 | + |
| 46 | + return validObjects.reduce((result, current) => { |
| 47 | + return mergeTwo(result, current); |
| 48 | + }, {}); |
| 49 | +} |
| 50 | + |
| 51 | +function mergeTwo(a, b) { |
| 52 | + // Handle null/undefined cases |
| 53 | + if (a == null) return b; |
| 54 | + if (b == null) return a; |
| 55 | + |
| 56 | + // If either value is not an object, b takes precedence |
| 57 | + if (typeof a !== 'object' || typeof b !== 'object') { |
| 58 | + return b; |
| 59 | + } |
| 60 | + |
| 61 | + // Handle arrays - concatenate them |
| 62 | + if (Array.isArray(a) && Array.isArray(b)) { |
| 63 | + return [...a, ...b]; |
| 64 | + } |
| 65 | + |
| 66 | + // If one is array and other is object, prefer the object |
| 67 | + if (Array.isArray(a) !== Array.isArray(b)) { |
| 68 | + return Array.isArray(b) ? b : a; |
| 69 | + } |
| 70 | + |
| 71 | + // Merge objects - create new object to avoid mutation |
| 72 | + const result = {}; |
| 73 | + |
| 74 | + // Get all keys from both objects |
| 75 | + const allKeys = new Set([...Object.keys(a), ...Object.keys(b)]); |
| 76 | + |
| 77 | + for (const key of allKeys) { |
| 78 | + const hasA = key in a; |
| 79 | + const hasB = key in b; |
| 80 | + |
| 81 | + if (hasA && hasB) { |
| 82 | + // Both have the key - recursively merge |
| 83 | + result[key] = mergeTwo(a[key], b[key]); |
| 84 | + } else if (hasA) { |
| 85 | + // Only a has the key |
| 86 | + result[key] = a[key]; |
| 87 | + } else { |
| 88 | + // Only b has the key |
| 89 | + result[key] = b[key]; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return result; |
| 94 | +} |
| 95 | + |
1 | 96 | function isSingleValue(item) { |
2 | 97 | return ( |
3 | 98 | item !== null && |
|
0 commit comments