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

Underscore could use a _.propertyResult #2286

Open
machineghost opened this issue Aug 25, 2015 · 3 comments · May be fixed by #2531
Open

Underscore could use a _.propertyResult #2286

machineghost opened this issue Aug 25, 2015 · 3 comments · May be fixed by #2531
Labels
contrib probably belongs in Underscore-contrib enhancement

Comments

@machineghost
Copy link

The new _.property is really neat, and I love using it, but there's one problem: it only returns static values.

For instance, let's say I want to find out if any object in an array contains has an isNew property; I can do the following:

var objects = [{isNew: true}, {isNew: false}];

_(objects).any(_('isNew').property()); // === true

That's a lot better than before property, when I had to do:

_(objects).any(function(x) { return x.isNew; });

But what if instead of random objects I have a bunch of Backbone.Model objects. Those don't have an isNew property, they have an isNew method. So:

_(objects).any(_('isNew').property());

won't work; instead I'm back to the old style:

_(objects).any(function(x) { return _(x).result('isNew'); });

or (if I combine partial and result):

_(objects).any(function(_(_.result).partial(_, 'isNew'));

It would be really great if there was a method that was just like _.property, except that if it would return a function, it returns the result of executing that function instead (like _.result). In other words, if I could do ...

_(objects).any(_('isNew').propertyResult());

Since it's a fairly common practice to have is* methods on objects, and also common practice to want to use Underscore methods like any or filter with those methods, I really think this would be a useful addition to the library.

@jridgewell
Copy link
Collaborator

Lodash added _.method and _.methodOf for this.

monkpow pushed a commit to monkpow/underscore that referenced this issue May 5, 2016
monkpow pushed a commit to monkpow/underscore that referenced this issue May 5, 2016
monkpow pushed a commit to monkpow/underscore that referenced this issue May 5, 2016
monkpow pushed a commit to monkpow/underscore that referenced this issue May 5, 2016
@monkpow monkpow linked a pull request May 5, 2016 that will close this issue
monkpow pushed a commit to monkpow/underscore that referenced this issue May 5, 2016
…ctions on an Object. Closes jashkenas#2286: Underscore could use a _.propertyResult.
monkpow pushed a commit to monkpow/underscore that referenced this issue May 6, 2016
…ctions on an Object. Closes jashkenas#2286: Underscore could use a _.propertyResult.
@carpben
Copy link
Contributor

carpben commented Jul 5, 2020

I wonder if this feature is still relvant, and is worthy of development in Javascript's current eco-system.

Since this suggestion was originally published, arrow function became a popular and wide spread feature in the Javascript ecosystem.

At the time @machineghost had to write:

_(objects).any(function(x) { return x.isNew; });

But today we can simply write:

_(objects).any( (x)=>x.isNew );

@machineghost wanted to be able to write:

_(objects).any(_('isNew').propertyResult());

But today we can write:

_(objects).any( (x)=>x.isNew() );

Personally I find the arrow function version much more readable.

@jgonggrijp
Copy link
Collaborator

@carpben I think you are right that with modern syntax, _.propertyResult wouldn't be a game changer. At least not as an iteratee; there may be other convincing use cases that we haven't considered. But as far as I know, nobody has reported one yet.

So I agree that it doesn't seem right to add this to Underscore at this time, but I would still welcome it as a contribution to Underscore-contrib. Contrib could be regarded as a proofing ground for suggested new features for Underscore. If Contrib offers _.propertyResult, it might turn out that people find a real use for it, in which case we can reconsider adding it to Underscore.

I should also mention that we currently still support some environments that don't have arrow functions and that not everyone may want or be able to use a transpiler. That's not a reason to add the function by itself, but still something to keep in mind.

@jgonggrijp jgonggrijp added the contrib probably belongs in Underscore-contrib label Aug 29, 2020
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

Successfully merging a pull request may close this issue.

4 participants