MatthiasMargot/redux-communication

View on GitHub
src/overwrite-deep/overwrite-deep.js

Summary

Maintainability
A
0 mins
Test Coverage
/*
 * (object, object) => object
 *
 * recursively replaces a given object's values with a given
 * overwrite object's values, while leaving unspecified properties
 * intact
 * */

function overwriteDeep (initial, overwrite) {
  return Object.keys(overwrite).reduce(
    (reduction, key) => ({
      ...reduction,
      [ key ]: overwrite[ key ] instanceof Object
        ? overwriteDeep(initial[ key ], overwrite[ key ])
        : overwrite[ key ],
    }),
    { ...initial },
  )
}

export default overwriteDeep