or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--rest

The lodash method _.rest exported as a standalone module for creating functions with rest parameters.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.rest@4.0.x

To install, run

npx @tessl/cli install tessl/npm-lodash--rest@4.0.0

index.mddocs/

lodash.rest

lodash.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.

Package Information

  • Package Name: lodash.rest
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.rest

Core Imports

var rest = require('lodash.rest');

ES6/Modern Node.js:

const rest = require('lodash.rest');

Basic Usage

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'

Capabilities

Rest Function

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:

  • (Function): Returns a new function that collects arguments from the start position onwards into an array passed as the parameter at the start position.

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')); // => 3

Implementation Notes

  • Based on the ES6 rest parameters specification
  • Handles edge cases like undefined start positions and invalid functions
  • Optimized for performance with internal helper functions
  • Compatible with CommonJS module systems
  • Part of the modular lodash ecosystem for selective imports
  • Maintains proper this binding when the returned function is called
  • Start position is coerced to a non-negative integer internally