Skip to main content
This release is 5 versions behind 1.33.0 — the latest version of @es-toolkit/es-toolkit. Jump to latest

@es-toolkit/es-toolkit@1.29.0-dev.958+1d1d0ee6
Built and signed on GitHub Actions

A modern JavaScript utility library that's 2-3 times faster and up to 97% smaller—a major upgrade to lodash.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
3 months ago (1.29.0-dev.958+1d1d0ee6)
function spread
spread<F extends (...args: any[]) => any>(
func: F,
argsIndex?: number,
): (...args: any[]) => ReturnType<F>

Creates a new function that spreads elements of an array argument into individual arguments for the original function. The array argument is positioned based on the argsIndex parameter.

Examples

function add(a, b) { return a + b; }

const spreadAdd = spread(add); console.log(spreadAdd([1, 2])); // Output: 3

// Example function to spread arguments over function add(a, b) { return a + b; }

// Create a new function that uses spread to combine arguments const spreadAdd = spread(add, 1);

// Calling spreadAdd with an array as the second argument console.log(spreadAdd(1, [2])); // Output: 3

// Function with default arguments function greet(name, greeting = 'Hello') { return ${greeting}, ${name}!; }

// Create a new function that uses spread to position the argument array at index 0 const spreadGreet = spread(greet, 0);

// Calling spreadGreet with an array of arguments console.log(spreadGreet(['Alice'])); // Output: Hello, Alice! console.log(spreadGreet(['Bob', 'Hi'])); // Output: Hi, Bob!

Type Parameters

F extends (...args: any[]) => any
  • A function type with any number of parameters and any return type.

Parameters

func: F
  • The function to be transformed. It can be any function with any number of arguments.
optional
argsIndex: number = 0
  • The index where the array argument is positioned among the other arguments. If argsIndex is negative or NaN, it defaults to 0. If it's a fractional number, it is rounded to the nearest integer.

Return Type

(...args: any[]) => ReturnType<F>
  • A new function that takes multiple arguments, including an array of arguments at the specified argsIndex, and returns the result of calling the original function with those arguments.

Add Package

deno add jsr:@es-toolkit/es-toolkit

Import symbol

import { spread } from "@es-toolkit/es-toolkit/compat";

---- OR ----

Import directly with a jsr specifier

import { spread } from "jsr:@es-toolkit/es-toolkit/compat";

Add Package

npx jsr add @es-toolkit/es-toolkit

Import symbol

import { spread } from "@es-toolkit/es-toolkit/compat";

Add Package

yarn dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { spread } from "@es-toolkit/es-toolkit/compat";

Add Package

pnpm dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { spread } from "@es-toolkit/es-toolkit/compat";

Add Package

bunx jsr add @es-toolkit/es-toolkit

Import symbol

import { spread } from "@es-toolkit/es-toolkit/compat";