-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Allow a specified depth for flatten #2460
Allow a specified depth for flatten #2460
Conversation
Per a new TC39 proposal http://bterlson.github.io/proposal-flatMap/#sec-Array.prototype.flatten they're suggesting a depth argument. This was fairly trivial to implement without breaking the current functionality. Thoughts?
ae70bb9
to
72bd84f
Compare
Awesome. 👍 |
👍 |
_.flatten = function(array, shallow) { | ||
return flatten(array, shallow, false); | ||
_.flatten = function(array, depth) { | ||
if (!depth) depth = Infinity; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should a depth
of 0
be seen as Infinity
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so you think we need a if (depth === 0) return array;
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should return a shallow clone/slice which should shake out naturally if 0
is allowed to be used as a depth (no need for a if
or special branch)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so then if (!depth && depth !== 0) return
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@paulfalgout To preserve as much back compat ya I think that would do.
ea360d8
to
7153603
Compare
So looking into both 0 and negative values, it seems much cleaner to handle this in |
Oh cool!
Yeah. |
} else if (depth <= 0) { | ||
return _.clone(array); | ||
} | ||
return flatten(array, depth, false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to ToNumber depth
here so we're not passing true
to flatten
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we're internally calling flatten
with booleans, so I don't know if we want to enforce that at the moment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we updat ethe internal calls to use a number
LGTM 👍 |
_.flatten = function(array, shallow) { | ||
return flatten(array, shallow, false); | ||
_.flatten = function(array, depth) { | ||
if (!depth && depth !== 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This conditional confuses the hell out of me. Maybe:
if (depth === false) {
depth = Infinity;
} else if (depth === true) {
depth = 1;
} else if (depth < 0) {
depth = 0;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the internal flatten
doesn't handle depth 0 at all and will conflict with whatever "strict" is doing.
depth
as with shallow
is optional so the default value is Infinity
, but to keep with the current API false
should also be assumed as Infinity
so I suppose it could be:
if(depth === false || _.isUndefined(depth)) {
depth = Infinity;
} else if (depth <= 0) {
return _.clone(array);
}
We can certainly add the else if (depth === true) {
but flatten
is called internally with true
elsewhere.
This was initially easy, but got complicated with edge cases and without refactoring internals a bit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@paulfalgout I think you already did the right thing. I think this is not even a breaking change. I'm in favor of merging this, but it has gotten a bit out of sync and I just opened #2849 which will make this even more out of sync. So let's postpone merging this just a little bit more.
Really good job!
@paulfalgout I merged #2849 three weeks ago and I nearly forgot to get back to you. Sorry about that. So
|
I'm now looking into merging this by executing the above steps myself. On closer inspection, I noticed that many internal calls to |
@jgonggrijp thanks for hopping on this.. I've been a little out of it as of late. |
Welcome. |
Per a new TC39 proposal http://bterlson.github.io/proposal-flatMap/#sec-Array.prototype.flatten they're suggesting a depth argument.
This was fairly trivial to implement without breaking the current functionality.
Thoughts?