UNPKG

1.5 kBJavaScriptView Raw
1import apply from './_apply';
2import arrayMap from './_arrayMap';
3import baseFlatten from './_baseFlatten';
4import baseIteratee from './_baseIteratee';
5import isFlattenableIteratee from './_isFlattenableIteratee';
6import rest from './rest';
7
8/* Built-in method references for those with the same name as other `lodash` methods. */
9var nativeMin = Math.min;
10
11/**
12 * Creates a function that invokes `func` with arguments transformed by
13 * corresponding `transforms`.
14 *
15 * @static
16 * @since 4.0.0
17 * @memberOf _
18 * @category Function
19 * @param {Function} func The function to wrap.
20 * @param {...(Function|Function[])} [transforms] The functions to transform.
21 * arguments, specified individually or in arrays.
22 * @returns {Function} Returns the new function.
23 * @example
24 *
25 * function doubled(n) {
26 * return n * 2;
27 * }
28 *
29 * function square(n) {
30 * return n * n;
31 * }
32 *
33 * var func = _.overArgs(function(x, y) {
34 * return [x, y];
35 * }, square, doubled);
36 *
37 * func(9, 3);
38 * // => [81, 6]
39 *
40 * func(10, 5);
41 * // => [100, 10]
42 */
43var overArgs = rest(function(func, transforms) {
44 transforms = arrayMap(baseFlatten(transforms, 1, isFlattenableIteratee), baseIteratee);
45 var funcsLength = transforms.length;
46 return rest(function(args) {
47 var index = -1,
48 length = nativeMin(args.length, funcsLength);
49
50 while (++index < length) {
51 args[index] = transforms[index].call(this, args[index]);
52 }
53 return apply(func, this, args);
54 });
55});
56
57export default overArgs;