Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add _.oneArg (and maybe _.twoArgs and _.limitArgs) #2871

Open
sktguha opened this issue Aug 7, 2020 · 2 comments
Open

add _.oneArg (and maybe _.twoArgs and _.limitArgs) #2871

sktguha opened this issue Aug 7, 2020 · 2 comments
Labels
contrib probably belongs in Underscore-contrib enhancement

Comments

@sktguha
Copy link

sktguha commented Aug 7, 2020

I suggest adding a new function called oneArg to underscore. It wraps a function and allows it to be called with one argument only and ignores rest of the arguments.
Implementation:
const oneArg = fn => arg => fn(arg);

usage:
Before oneArg:
['34','11','19','199','201'].map(parseInt); // returns [34, NaN, 1, 1, 33]

After oneArg:
const safeParseInt = oneArg(parseInt); ['34','11','19','199','201'].map(safeParseInt) // returns [34, 11, 19, 199, 201]

@jgonggrijp
Copy link
Collaborator

Thanks for the suggestion, @sktguha! It is an interesting idea. I'm cross-posting this to our companion project, Underscore-contrib, because I think it should be added there first before we consider making it part of the core library.

In the meanwhile, your particular use case can also be handled using _.partial:

import _, { partial } from 'underscore';

const safeParseInt = partial(parseInt, _, undefined);

You would be right to point out, though, that partial cannot handle variadic functions while oneArg can, so there is definitely an added value here.

@sktguha
Copy link
Author

sktguha commented Aug 9, 2020

Thanks for the reply . Interestingly it is available as unary in lodash. https://bit.dev/lodash/lodash/unary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contrib probably belongs in Underscore-contrib enhancement
Projects
None yet
Development

No branches or pull requests

2 participants