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

Uniform flatten #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

marseille
Copy link

This addition adds uniform flattening when you specify uniformFlatten:true and a maxDepth.

I was finding in my project that I needed to flatten an object but that it needed to be consistently flattened across the board.

@timoxley
Copy link
Contributor

timoxley commented Jul 2, 2015

@marseille I don't understand what you mean by "uniform". Can you add some docs?

@marseille
Copy link
Author

Sure, I can elaborate a bit more. I did have an example or two in the readme.md and the test.js that explain a bit, maybe they will help.

When I used the maxDepth option on an object, I did not see it apply the same depth to each key of an object. Instead I saw it count the number of objects it would flatten. If I specified an object with three keys, and first key having a depth of 3 - only the first key would be flattened. The rest of the keys would be untouched, if we were to use maxDepth 3.

That does not work well for my use case, I'd like every key in the object flattened to the same depth, if specified.

Let's say you have an object -

var hello_hash = {
     hello: {
          world: {again: 'good morning'}
      },
      salut: {
          monde: {encore: {bonne: 'matin'}}
      },
      konnichiwa: { sekai: { mou: { ohayou: 'gozaimasu' } } }
}

flatten(hello_hash, {maxDepth: 3})

/*
 hello_hash => 
  { 
    hello.world.again': 'good morning',
    salut: { 
        monde: { encore: {bonne:'matin' } },
    }
   konnichiwa: { sekai: { mou: {ohayou: 'gozaimasu' } } }
 }
*/

We can see here that the first key 'hello' was flattened, but none of the other keys were flattened. It looks like it flattened 3 objects, as per maxDepth, and then it was done working on the object.

Another object demonstrating uniformFlatten -

var hello_hash = {
     hello: {
          world: {again: 'good morning'}
      },
      salut: {
          monde: {encore: {bonne: 'matin'}}
      },
      konnichiwa: { sekai: { mou: { ohayou: 'gozaimasu' } } }
}
flatten(hello_hash, {maxDepth: 3, uniformFlatten:true})

/*
 hello_hash => 
  { 'hello.world.again': 'good morning',
    'salut.monde.encore': { bonne: 'matin' },
    'konnichiwa.sekai.mou': { ohayou: 'gozaimasu' } }
*/

Now we can see that every key in the object has been flattened to the same depth. We specified maxDepth: 3, as a result, every key has flattened 3 levels deep. Uniformly flattened, if you will.

This works very well for me and I can see it being useful in the future as well.

It's possible the name might be confusing, does that clarify what I meant by uniform?

@marseille
Copy link
Author

May I have an update on the status of this pull request?

Is there additional information I could provide to help clarify any concerns?

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.

2 participants