Skip to content

Conversation

bolgovr
Copy link

@bolgovr bolgovr commented Jun 7, 2012

I don't sure it is bug or a feature.

@jdalton
Copy link
Member

jdalton commented Jun 7, 2012

It's a feature and more consistent as other "Array" category methods like Underscore's _first, _.last, _.initial, and _.zipObject don't have fail-safes for not passing a required argument. The fail-safes are implemented in the "Collections" category methods though I'm not sure if they are really needed either. Underscore is tied to its _.each (a "Collections" method) when it needs to iterate and so has the side effect of its behavior bleeding into other non collections methods.

@jdalton
Copy link
Member

jdalton commented Jun 7, 2012

Did you run into an issue in your code where you expected an empty array?

@bolgovr
Copy link
Author

bolgovr commented Jun 7, 2012

yes, after drop-in replacement I found that this code fails because match returns array or null.

var result = _.flatten(string.match(/regexp/))[0] //result or undefined, but not throw Error;

I understand that this is not best idea to use _.flatten in this way, but anyway. In this commits I missed that underscore also flattens objects:

_.flatten({'a': 1}); //[1]

I can do it in this PR if you want.

@bolgovr
Copy link
Author

bolgovr commented Jun 7, 2012

underscore flatten(for in loop):

_.flatten({'a': [1, 2, [3]], 'b': [1,[4],10]}); // [1,2,3,1,4,10] 

lodash flatten (for loop):

_.flatten({'a': [1, 2, [3]], 'b': [1,[4],10]}); // [] 

@jdalton
Copy link
Member

jdalton commented Jun 7, 2012

In this commits I missed that underscore also flattens objects:

_.flatten({'a': 1}); //[1]
I can do it in this PR if you want.

Naw, no need. The flattening of objects is undocumented/untested and an unintended side effect of Underscore's _.each overuse/dependency.

@jdalton
Copy link
Member

jdalton commented Jun 7, 2012

yes, after drop-in replacement I found that this code fails because match returns array or null.

var result = _.flatten(string.match(/regexp/))[0] //result or undefined, but not throw Error;

The string.match(/regexp/) case is a great example of when a fail safe would be nice, because it's annoying to always do (string.match(/regexp/) || 0). However, I think you juuuust happened to not run into issues as I pointed out other "Arrays" methods would have thrown errors.

I will close this for now, but will consider adding a fail safe to all "Arrays" methods.

@jdalton
Copy link
Member

jdalton commented Jun 7, 2012

Resolved by 0a2e5fd2.

jdalton added a commit that referenced this pull request Aug 29, 2013
jdalton added a commit that referenced this pull request Sep 25, 2014
…closes #23, #24]

Former-commit-id: 66d09d3c8f3c045daf310c46581afa085daa57de
@lock
Copy link

lock bot commented Dec 27, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging this pull request may close these issues.

2 participants