1 /** Used as the `TypeError` message for "Functions" methods. */
2 var FUNC_ERROR_TEXT = 'Expected a function';
4 /* Native method references for those with the same name as other `lodash` methods. */
5 var nativeMax = Math.max;
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.
11 * **Note:** This method is based on the [rest parameter](https://developer.mozilla.org/Web/JavaScript/Reference/Functions/rest_parameters).
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.
21 * var say = _.restParam(function(what, names) {
22 * return what + ' ' + _.initial(names).join(', ') +
23 * (_.size(names) > 1 ? ', & ' : '') + _.last(names);
26 * say('hello', 'fred', 'barney', 'pebbles');
27 * // => 'hello fred, barney, & pebbles'
29 function restParam(func, start) {
30 if (typeof func != 'function') {
31 throw new TypeError(FUNC_ERROR_TEXT);
33 start = nativeMax(start === undefined ? (func.length - 1) : (+start || 0), 0);
37 length = nativeMax(args.length - start, 0),
40 while (++index < length) {
41 rest[index] = args[start + index];
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);
48 var otherArgs = Array(start + 1);
50 while (++index < start) {
51 otherArgs[index] = args[index];
53 otherArgs[start] = rest;
54 return func.apply(this, otherArgs);
58 module.exports = restParam;