| 1 |
/** |
| 2 |
* Simple object check. |
| 3 |
* |
| 4 |
* @param item |
| 5 |
* @return {boolean} |
| 6 |
*/ |
| 7 |
function isObject(item) { |
| 8 |
return item && typeof item === 'object' && !Array.isArray(item); |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Deep merge two objects. |
| 13 |
* |
| 14 |
* @link https://stackoverflow.com/a/34749873 |
| 15 |
* |
| 16 |
* @param {...any} sources |
| 17 |
* @param target |
| 18 |
* @param ...sources |
| 19 |
*/ |
| 20 |
export default function mergeDeep(target, ...sources) { |
| 21 |
if (!sources.length) { |
| 22 |
return target; |
| 23 |
} |
| 24 |
const source = sources.shift(); |
| 25 |
|
| 26 |
if (isObject(target) && isObject(source)) { |
| 27 |
Object.keys(source).forEach((key) => { |
| 28 |
if (isObject(source[key])) { |
| 29 |
if (!target[key]) { |
| 30 |
Object.assign(target, { [key]: {} }); |
| 31 |
} |
| 32 |
mergeDeep(target[key], source[key]); |
| 33 |
} else { |
| 34 |
Object.assign(target, { [key]: source[key] }); |
| 35 |
} |
| 36 |
}); |
| 37 |
} |
| 38 |
|
| 39 |
return mergeDeep(target, ...sources); |
| 40 |
} |
| 41 |
|