nthArg
util
Creates a function that gets the argument at index n.
Installation
Import
import { nthArg } from '@tulx/utils';Source Code
Implementation
/**
* Creates a function that gets the argument at index n.
*
* @param n - The index of the argument to return.
* @returns Returns the new function.
*
* @example
* ```ts
* const func = nthArg(1);
* func('a', 'b', 'c', 'd'); // 'b'
* ```
*/
export function nthArg(n: number = 0): (...args: unknown[]) => unknown {
return function (...args: unknown[]): unknown {
const index = n >= 0 ? n : args.length + n;
return args[index];
};
}
Example
import { nthArg } from '@tulx/utils';
const func = nthArg(1);
func('a', 'b', 'c', 'd'); // 'b'Related Functions
attempt
Attempts to invoke func, returning either the result or the caught error object.
bindAll
Binds methods of an object to the object itself, overwriting the existing method.
cond
Creates a function that iterates over pairs and invokes the corresponding function of the first predicate to return truthy.
conforms
Creates a function that invokes the predicate properties of source with the corresponding property values of a given object.
constant
Creates a function that returns value.
defaultTo
Checks value to determine whether a default value should be returned in its place.