mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
43 lines
945 B
JavaScript
43 lines
945 B
JavaScript
import apply from './.internal/apply.js';
|
|
|
|
/**
|
|
* Creates a function that invokes `func` with its arguments transformed.
|
|
*
|
|
* @since 4.0.0
|
|
* @category Function
|
|
* @param {Function} func The function to wrap.
|
|
* @param {Function[]} [transforms=[identity]]
|
|
* The argument transforms.
|
|
* @returns {Function} Returns the new function.
|
|
* @example
|
|
*
|
|
* function doubled(n) {
|
|
* return n * 2;
|
|
* }
|
|
*
|
|
* function square(n) {
|
|
* return n * n;
|
|
* }
|
|
*
|
|
* const func = overArgs((x, y) => [x, y], [square, doubled]);
|
|
*
|
|
* func(9, 3);
|
|
* // => [81, 6]
|
|
*
|
|
* func(10, 5);
|
|
* // => [100, 10]
|
|
*/
|
|
function overArgs(func, transforms) {
|
|
const funcsLength = transforms.length;
|
|
return function(...args) {
|
|
let index = -1;
|
|
const length = Math.min(args.length, funcsLength);
|
|
while (++index < length) {
|
|
args[index] = transforms[index].call(this, args[index]);
|
|
}
|
|
return apply(func, this, args);
|
|
};
|
|
}
|
|
|
|
export default overArgs;
|