UNPKG

1.1 kBJavaScriptView Raw
1var apply = require('./_apply');
2
3/* Built-in method references for those with the same name as other `lodash` methods. */
4var nativeMax = Math.max;
5
6/**
7 * A specialized version of `baseRest` which transforms the rest array.
8 *
9 * @private
10 * @param {Function} func The function to apply a rest parameter to.
11 * @param {number} [start=func.length-1] The start position of the rest parameter.
12 * @param {Function} transform The rest array transform.
13 * @returns {Function} Returns the new function.
14 */
15function overRest(func, start, transform) {
16 start = nativeMax(start === undefined ? (func.length - 1) : start, 0);
17 return function() {
18 var args = arguments,
19 index = -1,
20 length = nativeMax(args.length - start, 0),
21 array = Array(length);
22
23 while (++index < length) {
24 array[index] = args[start + index];
25 }
26 index = -1;
27 var otherArgs = Array(start + 1);
28 while (++index < start) {
29 otherArgs[index] = args[index];
30 }
31 otherArgs[start] = transform(array);
32 return apply(func, this, otherArgs);
33 };
34}
35
36module.exports = overRest;