Files
lodash/overArgs.js
John-David Dalton 35cdf6513b Bump to v4.13.0.
2016-05-22 19:35:24 -07:00

63 lines
1.7 KiB
JavaScript

import apply from './_apply.js';
import arrayMap from './_arrayMap.js';
import baseFlatten from './_baseFlatten.js';
import baseIteratee from './_baseIteratee.js';
import baseUnary from './_baseUnary.js';
import isArray from './isArray.js';
import isFlattenableIteratee from './_isFlattenableIteratee.js';
import rest from './rest.js';
/* Built-in method references for those with the same name as other `lodash` methods. */
var nativeMin = Math.min;
/**
* Creates a function that invokes `func` with arguments transformed by
* corresponding `transforms`.
*
* @static
* @since 4.0.0
* @memberOf _
* @category Function
* @param {Function} func The function to wrap.
* @param {...(Array|Array[]|Function|Function[]|Object|Object[]|string|string[])}
* [transforms[_.identity]] The functions to transform.
* @returns {Function} Returns the new function.
* @example
*
* function doubled(n) {
* return n * 2;
* }
*
* function square(n) {
* return n * n;
* }
*
* var func = _.overArgs(function(x, y) {
* return [x, y];
* }, [square, doubled]);
*
* func(9, 3);
* // => [81, 6]
*
* func(10, 5);
* // => [100, 10]
*/
var overArgs = rest(function(func, transforms) {
transforms = (transforms.length == 1 && isArray(transforms[0]))
? arrayMap(transforms[0], baseUnary(baseIteratee))
: arrayMap(baseFlatten(transforms, 1, isFlattenableIteratee), baseUnary(baseIteratee));
var funcsLength = transforms.length;
return rest(function(args) {
var index = -1,
length = nativeMin(args.length, funcsLength);
while (++index < length) {
args[index] = transforms[index].call(this, args[index]);
}
return apply(func, this, args);
});
});
export default overArgs;