-
Notifications
You must be signed in to change notification settings - Fork 84
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
coalesce with falsy #52
Comments
in the upcoming version you'll be able to augment objectPath.extend(function(base, options){
return {
coalesce: function(obj, paths, defaultValue, falsy){
falsy = falsy !== undefined ? falsy : false;
if (falsy === true){
var value, i, len = paths.length;
for(i = 0; i < len; i++) {
if ((value = base.get(obj, paths[i], void 0, options.ownPropertiesOnly))){
return value;
}
}
return defaultValue;
}
return base.coalesce(obj, paths, defaultValue, options.ownPropertiesOnly);
}
};
});
// new usage
objectPath.coalesce(obj, ['path','path.2','path.3'], null, true); as you can see, you don't need to rewrite the coalesce code by hand, only augment it's capabilities :) |
@pocesar, hm, your snippet does not represent my idea, since it relies on base Your snippet can be effectively reduced to: |
indeed, I've rewritten the snippet. if you want to test the 1.0.0 to see if solves your problem, check the 1.0.0 branch |
@pocesar, thanks, no testing required, I've looked through your updated version and it's a solution. I think such thing must be in core, since it's a very low-level code and it has complexity comparable to complexity of |
it makes sense to have a fourth parameter to change the behavior of the function (like |
@pocesar, of course it can save compability. This can be done with boolean parameter or predicate for |
@pocesar, I have an idea of such design:
I'm not sure if If user do not pass |
that would definitely be a good plugin to have :) |
Hi. I'm finding myself using
coalesce
for achieving clean and idiomatic code. But sometimes my data providers return me objects, where some keys I pick present, but falsy (undefined
,null
, empty string etc). In the mean timecoalesce
relies on key existing/non-existing.Is it possible to implement in this module another variant of
coalesce
which will check not key presence, but its value falsiness? So, it will work like good oldobject.key || object.key2 || ...
but with benefits of deep paths support.I've looked through opened & closed issues, but found nothing similar. If it is a dulicate, please, forgive me. I think other users of
object-path
could face this issue as well, and it's not pleasant to return to||
-stuff when you're already using such powerful utility like this.The text was updated successfully, but these errors were encountered: