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