Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / function / restParam.js
1 /** Used as the `TypeError` message for "Functions" methods. */
2 var FUNC_ERROR_TEXT = 'Expected a function';
3
4 /* Native method references for those with the same name as other `lodash` methods. */
5 var nativeMax = Math.max;
6
7 /**
8  * Creates a function that invokes `func` with the `this` binding of the
9  * created function and arguments from `start` and beyond provided as an array.
10  *
11  * **Note:** This method is based on the [rest parameter](https://developer.mozilla.org/Web/JavaScript/Reference/Functions/rest_parameters).
12  *
13  * @static
14  * @memberOf _
15  * @category Function
16  * @param {Function} func The function to apply a rest parameter to.
17  * @param {number} [start=func.length-1] The start position of the rest parameter.
18  * @returns {Function} Returns the new function.
19  * @example
20  *
21  * var say = _.restParam(function(what, names) {
22  *   return what + ' ' + _.initial(names).join(', ') +
23  *     (_.size(names) > 1 ? ', & ' : '') + _.last(names);
24  * });
25  *
26  * say('hello', 'fred', 'barney', 'pebbles');
27  * // => 'hello fred, barney, & pebbles'
28  */
29 function restParam(func, start) {
30   if (typeof func != 'function') {
31     throw new TypeError(FUNC_ERROR_TEXT);
32   }
33   start = nativeMax(start === undefined ? (func.length - 1) : (+start || 0), 0);
34   return function() {
35     var args = arguments,
36         index = -1,
37         length = nativeMax(args.length - start, 0),
38         rest = Array(length);
39
40     while (++index < length) {
41       rest[index] = args[start + index];
42     }
43     switch (start) {
44       case 0: return func.call(this, rest);
45       case 1: return func.call(this, args[0], rest);
46       case 2: return func.call(this, args[0], args[1], rest);
47     }
48     var otherArgs = Array(start + 1);
49     index = -1;
50     while (++index < start) {
51       otherArgs[index] = args[index];
52     }
53     otherArgs[start] = rest;
54     return func.apply(this, otherArgs);
55   };
56 }
57
58 module.exports = restParam;