Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80676 views
1
/**
2
* lodash 3.6.1 (Custom Build) <https://lodash.com/>
3
* Build: `lodash modern modularize exports="npm" -o ./`
4
* Copyright 2012-2015 The Dojo Foundation <http://dojofoundation.org/>
5
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
6
* Copyright 2009-2015 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
7
* Available under MIT license <https://lodash.com/license>
8
*/
9
10
/** Used as the `TypeError` message for "Functions" methods. */
11
var FUNC_ERROR_TEXT = 'Expected a function';
12
13
/* Native method references for those with the same name as other `lodash` methods. */
14
var nativeMax = Math.max;
15
16
/**
17
* Creates a function that invokes `func` with the `this` binding of the
18
* created function and arguments from `start` and beyond provided as an array.
19
*
20
* **Note:** This method is based on the [rest parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters).
21
*
22
* @static
23
* @memberOf _
24
* @category Function
25
* @param {Function} func The function to apply a rest parameter to.
26
* @param {number} [start=func.length-1] The start position of the rest parameter.
27
* @returns {Function} Returns the new function.
28
* @example
29
*
30
* var say = _.restParam(function(what, names) {
31
* return what + ' ' + _.initial(names).join(', ') +
32
* (_.size(names) > 1 ? ', & ' : '') + _.last(names);
33
* });
34
*
35
* say('hello', 'fred', 'barney', 'pebbles');
36
* // => 'hello fred, barney, & pebbles'
37
*/
38
function restParam(func, start) {
39
if (typeof func != 'function') {
40
throw new TypeError(FUNC_ERROR_TEXT);
41
}
42
start = nativeMax(start === undefined ? (func.length - 1) : (+start || 0), 0);
43
return function() {
44
var args = arguments,
45
index = -1,
46
length = nativeMax(args.length - start, 0),
47
rest = Array(length);
48
49
while (++index < length) {
50
rest[index] = args[start + index];
51
}
52
switch (start) {
53
case 0: return func.call(this, rest);
54
case 1: return func.call(this, args[0], rest);
55
case 2: return func.call(this, args[0], args[1], rest);
56
}
57
var otherArgs = Array(start + 1);
58
index = -1;
59
while (++index < start) {
60
otherArgs[index] = args[index];
61
}
62
otherArgs[start] = rest;
63
return func.apply(this, otherArgs);
64
};
65
}
66
67
module.exports = restParam;
68
69