UNPKG

@onesy/utils

Version:
38 lines (32 loc) 1.24 kB
import is from './is'; import copy from './copy'; const optionsDefault = { copy: false, merge: { array: false } }; const merge = function (target, source) { let options_ = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; const options = { ...optionsDefault, ...options_ }; if (options.merge.array && is('array', target) && is('array', source)) { const length = Math.max(target.length, source.length); for (let i = 0; i < length; i++) { if (target[i] === undefined) target[i] = source[i]; if (is('object', target[i]) && is('object', source[i]) || is('array', target[i]) && is('array', source[i])) target[i] = merge(target[i], source[i], options); } } if (is('object', target) && is('object', source)) { Object.keys(source).forEach(key => { // We only care about direct target object properties // not about inherited properties from a prototype chain if (target.hasOwnProperty(key)) { if (is('object', target[key]) && is('object', source[key])) target[key] = merge(target[key], source[key], options); } else target[key] = options.copy ? copy(source[key]) : source[key]; }); } return target; }; export default merge;