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 support for variable-depth flatten #1961

Merged
merged 1 commit into from
Feb 10, 2016
Merged

Conversation

msmorgan
Copy link
Contributor

@msmorgan msmorgan commented Feb 8, 2016

This commit adds an optional maxDepth argument to _.flatten, allowing a nested array to be recursively flattened to a variable degree.

Please double-check I made the correct change in fp/_mapping.js, because it was hard to tell what was going on in that file.

@msmorgan
Copy link
Contributor Author

msmorgan commented Feb 8, 2016

I forgot to save a file so I'm closing and re-submitting this.

@msmorgan msmorgan closed this Feb 8, 2016
@jfmengels
Copy link
Contributor

You can force-push your branch instead of closing this one.

@msmorgan
Copy link
Contributor Author

msmorgan commented Feb 8, 2016

Done. Didn't know you could do that.

@jdalton
Copy link
Member

jdalton commented Feb 8, 2016

Could you remove the dist/ and doc/ files.

@jfmengels
Copy link
Contributor

I'm pretty sure you should not commit the dist/ and doc/ folders.
As for the mapping, looks good, but it depends on the behavior that you want. If you put it in the aryMethod[1] category, then you won't be able to use the maxDepth option out of the box. If you put it in the aryMethod[2], then you will be forced to add a maxDepth value every time.

I'm guessing the common case is that people want it to be flattened only by one level, and would use flattenDeep otherwise. I'd argue it would actually make more sense to have the maxDepth option in flattenDeep function (then put that one in the 2-args mapping, and flatten in the 1 or keep it uncapped).

result || (result = []);

if (maxDepth < 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The min depth should be 1. A flatten that doesn't flatten isn't a flatten.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally made it callable with maxDepth == 0 for flexibility purposes. It defaults to 1. I'll change it if you think it's necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya it shouldn't go below 1.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't look too closely, but is the case needed for recursion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not, since it won't be called recursively once maxDepth reaches 1.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@megawac

didn't look too closely, but is the case needed for recursion?

Naw just base functionality flatten should flatten at least 1 level.
I've expected a depth level request for a while :P

@msmorgan msmorgan force-pushed the master branch 2 times, most recently from 9f359c8 to 9975b89 Compare February 8, 2016 23:45
@msmorgan
Copy link
Contributor Author

msmorgan commented Feb 8, 2016

If the consensus is that I should move this functionality to flattenDeep (with a default maxDepth value of Infinity?), I can change it tomorrow.

@jdalton
Copy link
Member

jdalton commented Feb 9, 2016

@msmorgan

If the consensus is that I should move this functionality to flattenDeep (with a default maxDepth value of Infinity?), I can change it tomorrow.

I think that a separate method is the way to go. I like _.flatten because it's simple and doesn't have any extra params that muck up iteration. In v4 I've tried to split functionality into separate methods like _.flatten and _.flattenDeep.

I like the modification to baseFlatten but, if this is added, I imagine it would be exposed to the user as_.flattenDepth or something.

Have you signed our cla?

@msmorgan
Copy link
Contributor Author

msmorgan commented Feb 9, 2016

I just signed it (submitted a CLA on 2016-02-09).

If this will be a separate method, what do you think of the name _.flattenBy?

@jdalton
Copy link
Member

jdalton commented Feb 9, 2016

@msmorgan

If this will be a separate method, what do you think of the name _.flattenBy?

No the "By" postfix has another meaning. I think _.flattenDepth is fine.

@msmorgan
Copy link
Contributor Author

msmorgan commented Feb 9, 2016

@jdalton

Okay, _.flattenDepth it is.

What should it do if a depth of 0 is passed? I think it should simply copy the array. Wanting to recursively flatten 0 times might not come up that often, but if it does for someone, that behavior seems correct/consistent.

However, I concede that baseFlatten should probably default to 1 if non-numeric data is passed, rather than 0 as I have it now.

@jdalton
Copy link
Member

jdalton commented Feb 9, 2016

What should it do if a depth of 0 is passed?

Okaaaay, it's gross but consistent to do a simple arrayCopy.
I checked and other methods that default to n of 1 still allow 0 and you get what you get.

@StreetStrider
Copy link
Contributor

flattenTo?
flattenUnder?
flattenAt (at level)?

@jdalton
Copy link
Member

jdalton commented Feb 9, 2016

I like flattenTo but we already have a pattern of taking a param and making it part of the name

  • xyzDeep
  • xyzSize
  • xyzN

So turning in to _.flattenDepth is fine

@msmorgan msmorgan force-pushed the master branch 3 times, most recently from ed13a72 to f3aa305 Compare February 9, 2016 20:25
@msmorgan
Copy link
Contributor Author

msmorgan commented Feb 9, 2016

I've moved the new functionality into a separate _.flattenDepth function.

Please verify that I am defaulting the depth argument to baseFlatten correctly.

@@ -27,6 +27,7 @@

/** Used as a reference to the global object. */
var root = (typeof global == 'object' && global) || this;
debugger;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rogue debugger

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops! Should have noticed that.

At one point, I had a bug where baseFlatten was defaulting depth to 0 instead of 1 (because toInteger(undefined) behaved differently than I assumed), and the test suite wouldn't even load.

@msmorgan
Copy link
Contributor Author

msmorgan commented Feb 9, 2016

New commit fixes those three things.

function baseFlatten(array, isDeep, isStrict, result) {
function baseFlatten(array, depth, isStrict, result) {
depth = depth === undefined ? 1 : depth;
if (depth < 1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move the depth < 1 and copyArray() bit to flattenDepth.

jdalton added a commit that referenced this pull request Feb 10, 2016
Added support for variable-depth flatten.
@jdalton jdalton merged commit caca8ef into lodash:master Feb 10, 2016
@jdalton
Copy link
Member

jdalton commented Feb 10, 2016

Thank you @msmorgan!

@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.

5 participants