merge.js 841 B

12345678910111213141516171819202122232425262728293031323334353637
  1. var isArray = require('./isArray')
  2. var isPlainObject = require('./isPlainObject')
  3. var each = require('./each')
  4. function handleMerge (target, source) {
  5. if ((isPlainObject(target) && isPlainObject(source)) || (isArray(target) && isArray(source))) {
  6. each(source, function (obj, key) {
  7. target[key] = handleMerge(target[key], obj)
  8. })
  9. return target
  10. }
  11. return source
  12. }
  13. /**
  14. * 将一个或多个源对象合并到目标对象中
  15. *
  16. * @param {Object} target 目标对象
  17. * @param {...Object}
  18. * @return {Boolean}
  19. */
  20. var merge = function (target) {
  21. if (!target) {
  22. target = {}
  23. }
  24. var args = arguments
  25. var len = args.length
  26. for (var source, index = 1; index < len; index++) {
  27. source = args[index]
  28. if (source) {
  29. handleMerge(target, source)
  30. }
  31. }
  32. return target
  33. }
  34. module.exports = merge