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 _.withDefault combinator #166

Open
joshuacc opened this issue Jul 31, 2014 · 7 comments
Open

Add _.withDefault combinator #166

joshuacc opened this issue Jul 31, 2014 · 7 comments
Assignees
Labels
after modules This should be postponed until after modularization (temporary label, see #220) enhancement

Comments

@joshuacc
Copy link
Contributor

The _.withDefault function would accept a function as its argument, and returns a new function that transparently replaces null/undefined return values with a default value.

Example:

function getArrLength (arr) {
  if (_.isArray(arr)) return arr.length;
}

getArrLengthOrZero = _.withDefault(getArrLength, 0);

getArrLengthOrZero({});
// => 0

May need a better name for the function, though.

@fogus
Copy link
Member

fogus commented Jul 31, 2014

Hi @joshuacc,

Take a look a fnull at https://github.com/documentcloud/underscore-contrib/blob/master/underscore.function.combinators.js#L192 and see if that gives you what you need.

@joshuacc joshuacc self-assigned this Jul 31, 2014
@fogus
Copy link
Member

fogus commented Jul 31, 2014

Ahhh! Wait, I see that your proposal works on the return. Maybe we could talk about how this operates with or augments fnull instead?

@joshuacc
Copy link
Contributor Author

@fogus I saw that, but if I'm reading right it operates on the opposite end of things. My understanding is that _.fnull fills in non-existy arguments.

What I'm looking for in _.withDefault is filling in non-existy return values.

@fogus
Copy link
Member

fogus commented Jul 31, 2014

Right. I saw that right after hitting send. :(

@joshuacc
Copy link
Contributor Author

Hehe. Replying at the same time. 😁

One common use case that I could see for _.withDefault is when using _.map to pluck properties that may or may not be defined.

var arr = [
    { prop: "val" },
    { prop: "val" },
    {}
];

_.map(arr, function (val) { return val; });
// => ["val", "val", undefined]

_.map(arr, _.withDefault(function (val) { return val; }, "default"));
// => ["val", "val", "default"]

I think this fits as a nice complement to _.fnull operating on the other end of things.

@jgonggrijp
Copy link
Contributor

I like this idea a lot. The names withDefault and fnull don't reveal the duality between them, though. Maybe one of them can get an alias to emphasize this duality?

@jgonggrijp jgonggrijp added the after modules This should be postponed until after modularization (temporary label, see #220) label Aug 7, 2020
@joshuacc
Copy link
Contributor Author

joshuacc commented Aug 7, 2020

Maybe fnull could get an alias like withDefaultArgs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
after modules This should be postponed until after modularization (temporary label, see #220) enhancement
Projects
None yet
Development

No branches or pull requests

3 participants