assignInWith.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. var copyObject = require('./_copyObject'),
  2. createAssigner = require('./_createAssigner'),
  3. keysIn = require('./keysIn');
  4. /**
  5. * This method is like `_.assignIn` except that it accepts `customizer`
  6. * which is invoked to produce the assigned values. If `customizer` returns
  7. * `undefined`, assignment is handled by the method instead. The `customizer`
  8. * is invoked with five arguments: (objValue, srcValue, key, object, source).
  9. *
  10. * **Note:** This method mutates `object`.
  11. *
  12. * @static
  13. * @memberOf _
  14. * @since 4.0.0
  15. * @alias extendWith
  16. * @category Object
  17. * @param {Object} object The destination object.
  18. * @param {...Object} sources The source objects.
  19. * @param {Function} [customizer] The function to customize assigned values.
  20. * @returns {Object} Returns `object`.
  21. * @see _.assignWith
  22. * @example
  23. *
  24. * function customizer(objValue, srcValue) {
  25. * return _.isUndefined(objValue) ? srcValue : objValue;
  26. * }
  27. *
  28. * var defaults = _.partialRight(_.assignInWith, customizer);
  29. *
  30. * defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 });
  31. * // => { 'a': 1, 'b': 2 }
  32. */
  33. var assignInWith = createAssigner(function(object, source, srcIndex, customizer) {
  34. copyObject(source, keysIn(source), object, customizer);
  35. });
  36. module.exports = assignInWith;