Skip to content

unary

Summary

Takes a function fn of any arity and returns version of it that takes exactly one parameter.

Examples

It easy to forget that parseInt can take a number and a radix:

1
2
3
4
['1', '2', '3'].map(parseInt);
//=> [1, NaN, NaN]
// equivalent to [parseInt('1', 0), parseInt('2', 1), parseInt('3', 2)]
// array indices................^.................^.................^

We can avoid this with unary:

1
2
3
['1', '2', '3'].map(unary(parseInt));
//=> [1, 2, 3]
// equivalent to [parseInt('1'), parseInt('2'), parseInt('3')]

Parameters

Name Type Description
fn function

Return

function