es-toolkit/docs/reference/function/ary.md
2024-08-11 10:54:13 +09:00

927 B

ary

Creates a function that invokes func, with up to n arguments, ignoring any additional arguments.

Signature

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, optional): The arity cap, defaulting to the number of parameters of func. Negative numbers will be treated as 0, and decimals will be rounded down.

Returns

((...args: any[]) => ReturnType<F>): Returns the new capped function.

Examples

import { ary } from 'es-toolkit/function';

function fn(a, b, c) {
  console.log(arguments);
}

ary(fn, 2)(1, 2, 3); // [Arguments] { '0': 1, '1': 2 }
ary(fn); // [Arguments] { '0': 1, '1': 2, '2': 3 }
ary(fn, -1); // [Arguments] {}
ary(fn, 1.5); // [Arguments] { '0': 1 }
ary(fn, 2, {}); // [Arguments] { '0': 1, '1': 2, '2': 3 }