all files / util/ merge.js

16.67% Statements 3/18
0% Branches 0/12
0% Functions 0/3
16.67% Lines 3/18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42                                                                             
// TODO: rename this module to avoid confusion with lodash-es/merge
import { merge as _merge, mergeWith as _mergeWith } from 'lodash-es'
import isArray from './isArray'
 
/**
  Same as lodash/merge except that it provides options how to
  treat arrays.
 
  The default implementation overwrites elements.
   get concatenated rather than overwritten.
*/
export default function merge(a, b, options) {
  options = options || {}
  var _with = null
  if (options.array === 'replace') {
    _with = _replaceArrays
  } else if (options.array === 'concat') {
    _with = _concatArrays
  }
  if (_with) {
    return _mergeWith(a, b, _with)
  } else {
    return _merge(a, b)
  }
}
 
function _concatArrays(objValue, srcValue) {
  if (isArray(objValue)) {
    return objValue.concat(srcValue)
  } else {
    return null
  }
}
 
function _replaceArrays(objValue, srcValue) {
  if (isArray(objValue)) {
    return srcValue
  } else {
    return null
  }
}