all files / addon/utils/ mergeDeep.js

14.29% Statements 2/14
0% Branches 0/13
0% Functions 0/2
16.67% Lines 2/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23                                          
function isObject(item) {
  return (item && typeof item === 'object' && !Array.isArray(item));
}
 
export default function mergeDeep(target, ...sources) {
  if (!sources.length) return target;
  const source = sources.shift();
 
  if (isObject(target) && isObject(source)) {
    for (const key in source) {
      if (isObject(source[key])) {
        if (!target[key]) Object.assign(target, { [key]: {} });
        mergeDeep(target[key], source[key]);
      } else {
        Object.assign(target, { [key]: source[key] });
      }
    }
  }
 
  return mergeDeep(target, ...sources);
}