time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

overArgs.js (1620B)


      1 var apply = require('./_apply'),
      2     arrayMap = require('./_arrayMap'),
      3     baseFlatten = require('./_baseFlatten'),
      4     baseIteratee = require('./_baseIteratee'),
      5     baseRest = require('./_baseRest'),
      6     baseUnary = require('./_baseUnary'),
      7     castRest = require('./_castRest'),
      8     isArray = require('./isArray');
      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 module.exports = overArgs;