es-toolkit/docs/reference/function/ary.md

34 lines
708 B
Markdown
Raw Normal View History

# ary
2024-08-25 15:57:59 +03:00
Creates a function that invokes func, with up to `n` arguments, ignoring any additional arguments.
## Signature
```typescript
2024-08-11 04:54:13 +03:00
function ary<F extends (...args: any[]) => any>(func: F, n: number): (...args: any[]) => ReturnType<F>;
```
### Parameters
- `func` (`F`): The function to cap arguments for.
- `n` (`number`): The arity cap.
### Returns
(`(...args: any[]) => ReturnType<F>`): Returns the new capped function.
## Examples
```typescript
import { ary } from 'es-toolkit/function';
function fn(a: number, b: number, c: number) {
return Array.from(arguments);
}
2024-08-31 08:45:20 +03:00
ary(fn, 0)(1, 2, 3); // []
ary(fn, 1)(1, 2, 3); // [1]
ary(fn, 2)(1, 2, 3); // [1, 2]
ary(fn, 3)(1, 2, 3); // [1, 2, 3]
```