Create a function that accepts a single argument
Wrap a function so it ignores every argument after the first.
function unary(fn) { return value => fn(value); }
Usage
['1', '2', '3'].map(Number.parseInt); // → [1, NaN, NaN]
['1', '2', '3'].map(unary(Number.parseInt)); // → [1, 2, 3]
Know the limits
- map passes (value, index, array), so parseInt received the index as its radix, as the first line shows. unary drops everything after the value.
The function above is the code the usage lines run. It does not validate its arguments; add checks where it receives untrusted input.
Source & maintenance
Based on Create a function that accepts a single argument from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.
Documented as a reference with executed usage lines. Original MDX retained as inert source evidence.
Maintained by Oveyon. Original source commit: 14b481cf60df. This edition’s file: contents/create-a-function-that-accepts-a-single-argument.mdx in akamind/1loc, commit 0a77f131b22d. Read the license.
Keep exploring
- Logical xor operator — True when exactly one of two values is truthy.
- Identity function — Return the argument unchanged.
- Box handler — Chain transformations on a value, then unwrap the result.