OpenC3/cosmos

View on GitHub
openc3-cosmos-init/plugins/packages/openc3-cosmos-ace-diff/src/helpers/merge.js

Summary

Maintainability
A
0 mins
Test Coverage
/**
 * Simple is object check.
 * @param item
 * @returns {boolean}
 */
function isObject(item) {
  return (
    item && typeof item === 'object' && !Array.isArray(item) && item !== null
  )
}

/**
 * Deep merge two objects.
 * @param target
 * @param source
 */
export default function merge(target, source) {
  if (isObject(target) && isObject(source)) {
    Object.keys(source).forEach((key) => {
      if (isObject(source[key])) {
        if (!target[key] || !isObject(target[key])) {
          target[key] = source[key]
        }
        merge(target[key], source[key])
      } else {
        Object.assign(target, { [key]: source[key] })
      }
    })
  }
  return target
}