lodash.restparam
This package provides a modular build of Lodash's `_.restParam` function, designed to create a new function that collects arguments from a specified position into an array. While `lodash.restparam` is at version 3.6.1, the broader Lodash ecosystem has progressed to version 4.x. In Lodash v4.0.0, the `_.restParam` function was renamed to `_.rest`. Furthermore, all individual, per-method `lodash.*` packages, including `lodash.restparam`, are officially discouraged by the Lodash team and are slated for removal in Lodash v5. Modern JavaScript (ES6+) introduced native rest parameters (`...args`), which largely supersede the functionality provided by `_.restParam`. The package is unmaintained and should generally be avoided in favor of native features or direct imports from the main `lodash` package.
Common errors
-
TypeError: _.restParam is not a function
cause You are likely trying to use `_.restParam` with a version of Lodash v4.0.0 or higher where the function was renamed to `_.rest`.fixUpdate your code to use `_.rest` instead of `_.restParam`, or use native JavaScript rest parameters (`...args`). If you are directly importing `lodash.restparam`, ensure your project does not implicitly rely on a newer version of Lodash. -
Bundle size unexpectedly large despite only using a few Lodash functions.
cause Modular `lodash.*` packages like `lodash.restparam` bundle their internal dependencies. If multiple such packages are used, or if the main `lodash` package is also present, it can lead to duplicated code in your final bundle, circumventing potential tree-shaking benefits.fixReplace `lodash.restparam` with native JavaScript rest parameters (`...args`). For other Lodash functions, prefer importing specific methods directly from the main `lodash` package (e.g., `import { someMethod } from 'lodash';`) and leverage modern bundlers for tree-shaking, or use `babel-plugin-lodash`.
Warnings
- breaking The `_.restParam` function was renamed to `_.rest` in Lodash v4.0.0. This `lodash.restparam` package is locked at version 3.x, meaning it will not provide the updated function name or any features from Lodash v4+.
- deprecated All individual, per-method `lodash.*` packages (like `lodash.restparam`) are officially discouraged by the Lodash team and are scheduled for removal in Lodash v5. They often lead to larger bundle sizes compared to importing specific functions from the main `lodash` package.
- gotcha Native JavaScript introduced rest parameters (`...args`) in ES6 (ES2015), providing equivalent functionality to `_.restParam` and `_.rest`. Using this `lodash.restparam` package often introduces unnecessary dependency and bundle size.
- gotcha As an unmaintained, older modular package, `lodash.restparam` will not receive security updates. The main `lodash` package has had security advisories (e.g., prototype pollution fixes in v4.18.0) which would not be patched in this specific module.
Install
-
npm install lodash.restparam -
yarn add lodash.restparam -
pnpm add lodash.restparam
Imports
- restParam
import { restParam } from 'lodash.restparam';import restParam from 'lodash.restparam';
- restParam (CommonJS)
const restParam = require('lodash.restparam'); - Rest parameter (modern Lodash)
import { restParam } from 'lodash';import { rest } from 'lodash';
Quickstart
import restParam from 'lodash.restparam';
const greetWithManyNames = restParam(function(greeting, ...names) {
return `${greeting} ${names.join(', ')}!`;
}, 1); // Collect all arguments from index 1 (the second argument) onwards
console.log(greetWithManyNames('Hello', 'Alice', 'Bob', 'Charlie'));
// Expected output: "Hello Alice, Bob, Charlie!"
// Demonstrating the equivalent with native rest parameters, preferred in modern JS:
function modernGreet(greeting, ...names) {
return `${greeting} ${names.join(', ')}!`;
}
console.log(modernGreet('Hi', 'David', 'Eve'));
// Expected output: "Hi David, Eve!"