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

Guarantee isomorphism of flatten and unflatten commands #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sheerun
Copy link

@sheerun sheerun commented Sep 15, 2015

If no changes are needed when flattening / unflattening,
the same object is returned, saving memory and allowing
for strict equality checks on results of flatten/unflatten.

This commit also adds "shallow" option for unflatten command
that ignores nested "messy" object. It is especially useful
when used with "maxDepth" option of flatten function.

For example, now following is true:

var object1 = { "foo": { "bar": { "baz": "buz" } } }
var object2 = flatten(object1, { maxDepth: 2 })
var object3 = unflatten(object2, { shallow: true })

assert(object1.foo.bar === object2['foo.bar'])
assert(object1.foo.bar === object3.foo.bar)

If no changes are needed when flattening / unflattening,
the same object is returned, saving memory and allowing
for strict equality checks on results of flatten/unflatten.

This commit also adds "shallow" option for unflatten command
that ignores nested "messy" object. It is especially useful
when used with "maxDepth" option of flatten function.

For example, now following is true:

```
var object1 = { "foo": { "bar": { "baz": "buz" } } }
var object2 = flatten(object1, { maxDepth: 2 })
var object3 = unflatten(object2, { shallow: true })

assert(object1.foo.bar === object2['foo.bar'])
assert(object1.foo.bar === object3.foo.bar)
```
@timoxley
Copy link
Contributor

@sheerun For API symmetry, what do you think about shallow being maxDepth, and working at the specified depth rather than just level 2?

i.e.

var opts = { maxDepth: 2 }
var object2 = unflatten(flatten(object1, opts), opts)

// or
var opts = { maxDepth: 4 }
var object4 = unflatten(flatten(object3, opts), opts)

@sheerun
Copy link
Author

sheerun commented Jan 31, 2016

The same maxDepth won't work for isomorphic operation.

The isomorphic case is: unflatten(flatten(object1, { maxDepth: 2 }), { maxDepth: 1 })

That is for unflatten only maxDepth: 1 makes any sense, because flattening produces 1 layer from N layers (when unflattening we only want to unflatten this only layer). Am I clear?

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