UNPKG

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