| 1 | /**
|
|---|
| 2 | * Creates a new function that spreads elements of an array argument into individual arguments
|
|---|
| 3 | * for the original function. The array argument is positioned based on the `argsIndex` parameter.
|
|---|
| 4 | *
|
|---|
| 5 | * @template F - A function type with any number of parameters and any return type.
|
|---|
| 6 | * @param {F} func - The function to be transformed. It can be any function with any number of arguments.
|
|---|
| 7 | * @param {number} [argsIndex=0] - The index where the array argument is positioned among the other arguments.
|
|---|
| 8 | * If `argsIndex` is negative or `NaN`, it defaults to `0`. If it's a fractional number, it is rounded to the nearest integer.
|
|---|
| 9 | * @returns {(...args: any[]) => ReturnType<F>} - A new function that takes multiple arguments, including an array of arguments at the specified `argsIndex`,
|
|---|
| 10 | * and returns the result of calling the original function with those arguments.
|
|---|
| 11 | *
|
|---|
| 12 | * @example
|
|---|
| 13 | * function add(a, b) {
|
|---|
| 14 | * return a + b;
|
|---|
| 15 | * }
|
|---|
| 16 | *
|
|---|
| 17 | * const spreadAdd = spread(add);
|
|---|
| 18 | * console.log(spreadAdd([1, 2])); // Output: 3
|
|---|
| 19 | *
|
|---|
| 20 | * @example
|
|---|
| 21 | * // Example function to spread arguments over
|
|---|
| 22 | * function add(a, b) {
|
|---|
| 23 | * return a + b;
|
|---|
| 24 | * }
|
|---|
| 25 | *
|
|---|
| 26 | * // Create a new function that uses `spread` to combine arguments
|
|---|
| 27 | * const spreadAdd = spread(add, 1);
|
|---|
| 28 | *
|
|---|
| 29 | * // Calling `spreadAdd` with an array as the second argument
|
|---|
| 30 | * console.log(spreadAdd(1, [2])); // Output: 3
|
|---|
| 31 | *
|
|---|
| 32 | * @example
|
|---|
| 33 | * // Function with default arguments
|
|---|
| 34 | * function greet(name, greeting = 'Hello') {
|
|---|
| 35 | * return `${greeting}, ${name}!`;
|
|---|
| 36 | * }
|
|---|
| 37 | *
|
|---|
| 38 | * // Create a new function that uses `spread` to position the argument array at index 0
|
|---|
| 39 | * const spreadGreet = spread(greet, 0);
|
|---|
| 40 | *
|
|---|
| 41 | * // Calling `spreadGreet` with an array of arguments
|
|---|
| 42 | * console.log(spreadGreet(['Alice'])); // Output: Hello, Alice!
|
|---|
| 43 | * console.log(spreadGreet(['Bob', 'Hi'])); // Output: Hi, Bob!
|
|---|
| 44 | */
|
|---|
| 45 | declare function spread<R>(func: (...args: any[]) => R, argsIndex?: number): (...args: any[]) => R;
|
|---|
| 46 |
|
|---|
| 47 | export { spread };
|
|---|