You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.5 KiB

4 years ago
  1. var baseRest = require('./_baseRest'),
  2. createWrap = require('./_createWrap'),
  3. getHolder = require('./_getHolder'),
  4. replaceHolders = require('./_replaceHolders');
  5. /** Used to compose bitmasks for function metadata. */
  6. var WRAP_PARTIAL_FLAG = 32;
  7. /**
  8. * Creates a function that invokes `func` with `partials` prepended to the
  9. * arguments it receives. This method is like `_.bind` except it does **not**
  10. * alter the `this` binding.
  11. *
  12. * The `_.partial.placeholder` value, which defaults to `_` in monolithic
  13. * builds, may be used as a placeholder for partially applied arguments.
  14. *
  15. * **Note:** This method doesn't set the "length" property of partially
  16. * applied functions.
  17. *
  18. * @static
  19. * @memberOf _
  20. * @since 0.2.0
  21. * @category Function
  22. * @param {Function} func The function to partially apply arguments to.
  23. * @param {...*} [partials] The arguments to be partially applied.
  24. * @returns {Function} Returns the new partially applied function.
  25. * @example
  26. *
  27. * function greet(greeting, name) {
  28. * return greeting + ' ' + name;
  29. * }
  30. *
  31. * var sayHelloTo = _.partial(greet, 'hello');
  32. * sayHelloTo('fred');
  33. * // => 'hello fred'
  34. *
  35. * // Partially applied with placeholders.
  36. * var greetFred = _.partial(greet, _, 'fred');
  37. * greetFred('hi');
  38. * // => 'hi fred'
  39. */
  40. var partial = baseRest(function(func, partials) {
  41. var holders = replaceHolders(partials, getHolder(partial));
  42. return createWrap(func, WRAP_PARTIAL_FLAG, undefined, partials, holders);
  43. });
  44. // Assign default placeholders.
  45. partial.placeholder = {};
  46. module.exports = partial;