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.
function add(a, b) {
return a + b;
}
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;
}
// 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}!
;
}
// 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!
func: F
- The function to be transformed. It can be any function with any number of arguments.
argsIndex: number = 0
- The index where the array argument is positioned among the other arguments.
If
argsIndex
is negative orNaN
, it defaults to0
. If it's a fractional number, it is rounded to the nearest integer.
(...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.