All files / loader deep-merge.js

100% Statements 11/11
92.86% Branches 13/14
100% Functions 3/3
100% Lines 11/11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23        1x 12x 24x 12x   12x 2x   10x 2x   8x 8x         1x  
/* eslint-disable prefer-object-spread/prefer-object-spread */
/**
 * @param {Object[]} objects array of plain objects
 */
const deepMerge = (...objects) =>
  objects.reduce((acc, object) => {
    if (!acc || typeof object !== 'object') {
      return object;
    }
    if (!Array.isArray(acc) && Array.isArray(object)) {
      return object;
    }
    if (Array.isArray(acc) && Array.isArray(object)) {
      return [...acc, ...object];
    }
    return Object.keys(object).reduce(
      (acc2, key) => Object.assign({}, acc2, { [key]: deepMerge(acc2[key], object[key]) }),
      acc || {}
    );
  }, {});
 
module.exports = deepMerge;