_baseMerge.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import Stack from './_Stack.js';
  2. import assignMergeValue from './_assignMergeValue.js';
  3. import baseFor from './_baseFor.js';
  4. import baseMergeDeep from './_baseMergeDeep.js';
  5. import isObject from './isObject.js';
  6. import keysIn from './keysIn.js';
  7. import safeGet from './_safeGet.js';
  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. stack || (stack = new Stack);
  25. if (isObject(srcValue)) {
  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. export default baseMerge;