_baseMerge.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. var Stack = require('./_Stack'),
  2. assignMergeValue = require('./_assignMergeValue'),
  3. baseFor = require('./_baseFor'),
  4. baseMergeDeep = require('./_baseMergeDeep'),
  5. isObject = require('./isObject'),
  6. keysIn = require('./keysIn'),
  7. safeGet = require('./_safeGet');
  8. /**
  9. * The base implementation of `_.merge` without support for multiple sources.
  10. *
  11. * @private
  12. * @param {Object} object The destination object.
  13. * @param {Object} source The source object.
  14. * @param {number} srcIndex The index of `source`.
  15. * @param {Function} [customizer] The function to customize merged values.
  16. * @param {Object} [stack] Tracks traversed source values and their merged
  17. * counterparts.
  18. */
  19. function baseMerge(object, source, srcIndex, customizer, stack) {
  20. if (object === source) {
  21. return;
  22. }
  23. baseFor(source, function(srcValue, key) {
  24. if (isObject(srcValue)) {
  25. stack || (stack = new Stack);
  26. baseMergeDeep(object, source, key, srcIndex, baseMerge, customizer, stack);
  27. }
  28. else {
  29. var newValue = customizer
  30. ? customizer(safeGet(object, key), srcValue, (key + ''), object, source, stack)
  31. : undefined;
  32. if (newValue === undefined) {
  33. newValue = srcValue;
  34. }
  35. assignMergeValue(object, key, newValue);
  36. }
  37. }, keysIn);
  38. }
  39. module.exports = baseMerge;