Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / function / modArgs.js
1 var arrayEvery = require('../internal/arrayEvery'),
2     baseFlatten = require('../internal/baseFlatten'),
3     baseIsFunction = require('../internal/baseIsFunction'),
4     restParam = require('./restParam');
5
6 /** Used as the `TypeError` message for "Functions" methods. */
7 var FUNC_ERROR_TEXT = 'Expected a function';
8
9 /* Native method references for those with the same name as other `lodash` methods. */
10 var nativeMin = Math.min;
11
12 /**
13  * Creates a function that runs each argument through a corresponding
14  * transform function.
15  *
16  * @static
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 as individual functions or arrays of functions.
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 modded = _.modArgs(function(x, y) {
34  *   return [x, y];
35  * }, square, doubled);
36  *
37  * modded(1, 2);
38  * // => [1, 4]
39  *
40  * modded(5, 10);
41  * // => [25, 20]
42  */
43 var modArgs = restParam(function(func, transforms) {
44   transforms = baseFlatten(transforms);
45   if (typeof func != 'function' || !arrayEvery(transforms, baseIsFunction)) {
46     throw new TypeError(FUNC_ERROR_TEXT);
47   }
48   var length = transforms.length;
49   return restParam(function(args) {
50     var index = nativeMin(args.length, length);
51     while (index--) {
52       args[index] = transforms[index](args[index]);
53     }
54     return func.apply(this, args);
55   });
56 });
57
58 module.exports = modArgs;