The lodash method _.rest exported as a standalone module for creating functions with rest parameters.
npx @tessl/cli install tessl/npm-lodash--rest@4.0.0lodash.rest provides the lodash method _.rest as a standalone module. This utility creates a function that invokes the original function with arguments from a specified start position and beyond provided as a rest parameter array. It implements the ES6 rest parameters pattern, allowing you to collect trailing function arguments into an array.
npm install lodash.restvar rest = require('lodash.rest');ES6/Modern Node.js:
const rest = require('lodash.rest');const rest = require('lodash.rest');
// Create a function that collects trailing arguments
const say = rest(function(what, names) {
return what + ' ' + names.slice(0, -1).join(', ') +
(names.length > 1 ? ', & ' : '') + names[names.length - 1];
});
console.log(say('hello', 'fred', 'barney', 'pebbles'));
// => 'hello fred, barney, & pebbles'
// Using with a custom start position
const greet = rest(function(greeting, separator, names) {
return greeting + separator + names.join(', ');
}, 2);
console.log(greet('Hello', ': ', 'Alice', 'Bob', 'Charlie'));
// => 'Hello: Alice, Bob, Charlie'Creates a function that invokes the original function with arguments from start position and beyond provided as an array.
/**
* Creates a function that invokes `func` with the `this` binding of the
* created function and arguments from `start` and beyond provided as
* an array.
*
* @param {Function} func - The function to apply a rest parameter to
* @param {number} [start=func.length-1] - The start position of the rest parameter
* @returns {Function} Returns the new function
* @throws {TypeError} Throws if `func` is not a function
* @since 4.0.0
* @category Function
*/
function rest(func, start);Parameters:
func (Function): The function to apply a rest parameter to. Must be a valid function.start (number, optional): The start position of the rest parameter. Defaults to func.length - 1. Must be a non-negative integer.Returns:
Throws:
TypeError: When func is not a function, throws "Expected a function"Usage Examples:
const rest = require('lodash.rest');
// Basic usage - collect last N arguments
const variadic = rest(function(a, b, others) {
console.log('a:', a);
console.log('b:', b);
console.log('others:', others);
});
variadic('first', 'second', 'third', 'fourth');
// a: first
// b: second
// others: ['third', 'fourth']
// Custom start position
const customStart = rest(function(prefix, args) {
return prefix + ': ' + args.join(', ');
}, 1);
console.log(customStart('Items', 'apple', 'banana', 'cherry'));
// => 'Items: apple, banana, cherry'
// Edge case - start position 0 collects all arguments
const collectAll = rest(function(allArgs) {
return allArgs.length;
}, 0);
console.log(collectAll('a', 'b', 'c')); // => 3this binding when the returned function is called