UNPKG

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