createAssigner.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. var bindCallback = require('./bindCallback'),
  2. isIterateeCall = require('./isIterateeCall'),
  3. restParam = require('../function/restParam');
  4. /**
  5. * Creates a `_.assign`, `_.defaults`, or `_.merge` function.
  6. *
  7. * @private
  8. * @param {Function} assigner The function to assign values.
  9. * @returns {Function} Returns the new assigner function.
  10. */
  11. function createAssigner(assigner) {
  12. return restParam(function(object, sources) {
  13. var index = -1,
  14. length = object == null ? 0 : sources.length,
  15. customizer = length > 2 ? sources[length - 2] : undefined,
  16. guard = length > 2 ? sources[2] : undefined,
  17. thisArg = length > 1 ? sources[length - 1] : undefined;
  18. if (typeof customizer == 'function') {
  19. customizer = bindCallback(customizer, thisArg, 5);
  20. length -= 2;
  21. } else {
  22. customizer = typeof thisArg == 'function' ? thisArg : undefined;
  23. length -= (customizer ? 1 : 0);
  24. }
  25. if (guard && isIterateeCall(sources[0], sources[1], guard)) {
  26. customizer = length < 3 ? undefined : customizer;
  27. length = 1;
  28. }
  29. while (++index < length) {
  30. var source = sources[index];
  31. if (source) {
  32. assigner(object, source, customizer);
  33. }
  34. }
  35. return object;
  36. });
  37. }
  38. module.exports = createAssigner;