unary
Creates a function that calls fn with only its first argument, discarding
any others.
Prevents the classic footgun where a callback expecting extra positional
arguments is passed directly to Array.prototype.map:
['1', '2', '3'].map(parseInt) silently passes the array index as
parseInt’s radix argument, producing [1, NaN, NaN].
Available since v3.0.0
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
fn | F | The function to restrict to one argument |
Returns
Section titled “Returns”function — A new function that calls fn with only its first argument
Examples
Section titled “Examples”Fix the classic .map(parseInt) bug
Section titled “Fix the classic .map(parseInt) bug”Array.prototype.map passes (value, index, array) — parseInt reads index as its radix argument.
Restrict any multi-argument function to one argument
Section titled “Restrict any multi-argument function to one argument”Useful whenever a callback slot passes more arguments than the function should see.
