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

added "collect" alias for "map" #433

Merged
merged 1 commit into from Jan 13, 2012
Merged

Conversation

petejkim
Copy link
Contributor

because other functions also similarly have ruby-like aliases

@jashkenas
Copy link
Owner

Thanks. I'd be happy to merge this pull request, but please amend it first to not include any auto-generated code (the docco, and the -min version).

@petejkim
Copy link
Contributor Author

Hello Jake, I have amended the commit to exclude the auto generated code.

jashkenas added a commit that referenced this pull request Jan 13, 2012
added "collect" alias for "map"
@jashkenas jashkenas merged commit 9c88b72 into jashkenas:master Jan 13, 2012
@shesek
Copy link
Contributor

shesek commented Jan 13, 2012

Instead of copying tests, isn't it better to simply equals(_.collect, _.map, 'aliased as "collect"');?

@petejkim
Copy link
Contributor Author

maybe, but other aliases were tested that way.

    answers = [];
    _.forEach([1, 2, 3], function(num){ answers.push(num); });
    equals(answers.join(', '), '1, 2, 3', 'aliased as "forEach"');

@jashkenas
Copy link
Owner

@shesek: If you want to send a pull req making that change for all aliases ... that would be fine.

@justinperkins
Copy link

FWIW, it appears that this alias is not chainable.

// works fine
_.select([1,2,3,3,3], function(i){ return i == 3}).map(function(i){ return i+1})

// does not work, #collect not defined
_.select([1,2,3,3,3], function(i){ return i == 3}).collect(function(i){ return i+1})

@jashkenas
Copy link
Owner

FWIW, it appears that this alias is not chainable.

Nope, it is. You're just not doing a chain in your example -- you're calling the native map function on a native array. A chain would look like this:

_.chain([1,2,3,3,3]).select(function(i){ return i == 3}).collect(function(i){ return i+1}).value()

@justinperkins
Copy link

Ahhh, I didn't realize I had to explicitly set my chaining up like that. Thank you.

I'm using this in the context of a Backbone.js collection, so I have been using the underscore collection methods straight on my collection instance. This chaining syntax requires a bit of a tweak but no big deal.

// what I was trying to do
myCollection.select(...).collect(...)

// what I really need to do
_.chain(myCollection.models).select(...).collect(...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants