curry.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. var createWrap = require('./_createWrap');
  2. /** Used to compose bitmasks for function metadata. */
  3. var WRAP_CURRY_FLAG = 8;
  4. /**
  5. * Creates a function that accepts arguments of `func` and either invokes
  6. * `func` returning its result, if at least `arity` number of arguments have
  7. * been provided, or returns a function that accepts the remaining `func`
  8. * arguments, and so on. The arity of `func` may be specified if `func.length`
  9. * is not sufficient.
  10. *
  11. * The `_.curry.placeholder` value, which defaults to `_` in monolithic builds,
  12. * may be used as a placeholder for provided arguments.
  13. *
  14. * **Note:** This method doesn't set the "length" property of curried functions.
  15. *
  16. * @static
  17. * @memberOf _
  18. * @since 2.0.0
  19. * @category Function
  20. * @param {Function} func The function to curry.
  21. * @param {number} [arity=func.length] The arity of `func`.
  22. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  23. * @returns {Function} Returns the new curried function.
  24. * @example
  25. *
  26. * var abc = function(a, b, c) {
  27. * return [a, b, c];
  28. * };
  29. *
  30. * var curried = _.curry(abc);
  31. *
  32. * curried(1)(2)(3);
  33. * // => [1, 2, 3]
  34. *
  35. * curried(1, 2)(3);
  36. * // => [1, 2, 3]
  37. *
  38. * curried(1, 2, 3);
  39. * // => [1, 2, 3]
  40. *
  41. * // Curried with placeholders.
  42. * curried(1)(_, 3)(2);
  43. * // => [1, 2, 3]
  44. */
  45. function curry(func, arity, guard) {
  46. arity = guard ? undefined : arity;
  47. var result = createWrap(func, WRAP_CURRY_FLAG, undefined, undefined, undefined, undefined, undefined, arity);
  48. result.placeholder = curry.placeholder;
  49. return result;
  50. }
  51. // Assign default placeholders.
  52. curry.placeholder = {};
  53. module.exports = curry;