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

Enable flattening to a specified depth (continued) #2868

Merged
merged 7 commits into from
Jul 28, 2020

Conversation

jgonggrijp
Copy link
Collaborator

This is a continuation of #2460 by @paulfalgout. The former boolean shallow parameter of both the internal and the external flatten is replaced by a depth parameter which can be either boolean or numeric. Boolean values still have the same meaning. Numeric 1 has the same meaning as true while 0 or less results in a shallow copy without any flattening. The default is still infinite depth.

@jashkenas The original PR was approved by @akre54 and @michaelficarra in 2016. If this is sufficiently reassuring for you, you can stop reading here.

@paulfalgout I moved your checks against nonnumeric and nonpositive depth values back from the public to the internal flatten. At the time, you were concerned that this would be inconsistent with strict === true, but there is actually no conflict. When depth === 1, every nested element of every array is copied whether the nested element is an array or not, even when strict === true. Extrapolating this to depth === 0, "every array" is just the top-level array, so every element of it should be copied, regardless of whether it is an array and regardless of the value of strict.

Merging this should automatically also merge #2460.

paulfalgout and others added 7 commits March 6, 2016 03:02
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?
This is mostly to ensure that the internal flatten is still
well-behaved, since it being called internally with boolean arguments,
and there is no check in place to enforce that false is interpreted as
infinite depth.
Manual testing revealed that flattening with depth=false behaved as a
shallow flatten instead of a deep one.
@coveralls
Copy link

Coverage Status

Coverage increased (+0.02%) to 96.28% when pulling 7d37875 on jgonggrijp:feature/flatten-depth into 6ee1d2e on jashkenas:master.

@jgonggrijp
Copy link
Collaborator Author

Thanks for the review @jashkenas!

@jgonggrijp jgonggrijp merged commit f85ee46 into jashkenas:master Jul 28, 2020
@jgonggrijp jgonggrijp deleted the feature/flatten-depth branch July 28, 2020 19:40
jgonggrijp added a commit to jgonggrijp/underscore that referenced this pull request Aug 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants