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

Unflatten dot-notation keys nested in an array #82

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

Conversation

alexkwolfe
Copy link

There are two things in this PR:

  • Readme correction. The unflatten() function does not actually support the safe option.
  • The unflatten() function previously did not support unflattening keys nested in an array. It does now.
// before: array is totally ignored
unflatten({ this: [ { 'lorem.ipsum': 'bar' } ] })
// => { this: [ { 'lorem.ipsum': 'bar' } ] }

// after: objects in array are unflattened
unflatten({ this: [ { 'lorem.ipsum': 'bar' } ] })
// => { this: [ { lorem: { ipsum: 'bar' } } ] }

@alexkwolfe
Copy link
Author

Any feedback on this?

@robblovell
Copy link

robblovell commented Apr 27, 2020

This change looks relevant and valuable. Can we get a reviewer to review and merge? The immediate problem that stands out is that this pull request needs to be updated to the latest release. It appears the latest version (5.0.0) has the same problem.

@robblovell
Copy link

robblovell commented Apr 27, 2020

Wouldn't you need a test for unflatten in safe mode like this:

suite('Arrays and safe', function () {

  test('flattend and unflatten array safely', function () {
    const jsonObject = [1, 2, 3, 'hello'];
    const jsonPath = { 0: 1, 1: 2, 2: 3, 3: 'hello' }
    assert.deepStrictEqual(unflatten(jsonPath, { safe: true }), jsonObject)
  })
  test('flattend and unflatten array safe = false', function () {
    const jsonObject = {
      0: 1,
      1: 2,
      2: 3,
      3: 'hello'
    };
    const jsonPath = { 0: 1, 1: 2, 2: 3, 3: 'hello' }
    assert.deepStrictEqual(unflatten(jsonPath, { safe: false }), jsonObject)
  })
  test('flattend and unflatten array safely, case 2', function () {
    const jsonObject = { this: [ { lorem: { ipsum: 'bar' } } ] }
    const jsonPath = { 'this.0.lorem.ipsum': 'bar' }
    assert.deepStrictEqual(unflatten(jsonPath, { safe: true }), jsonObject)
  })
  test('flattend and unflatten array safe false, case 2', function () {
    const jsonObject = { this: [ { lorem: { ipsum: 'bar' } } ] }
    const jsonPath = { 'this.0.lorem.ipsum': 'bar' }
    assert.deepStrictEqual(unflatten(jsonPath, { safe: false }), jsonObject)
  })
})

@alexkwolfe
Copy link
Author

These two tests together verify that safe is not a supported option in unflatten(), correct?

  test('flattend and unflatten array safely, case 2', function () {
    const jsonObject = { this: [ { lorem: { ipsum: 'bar' } } ] }
    const jsonPath = { 'this.0.lorem.ipsum': 'bar' }
    assert.deepStrictEqual(unflatten(jsonPath, { safe: true }), jsonObject)
  })
  test('flattend and unflatten array safe false, case 2', function () {
    const jsonObject = { this: [ { lorem: { ipsum: 'bar' } } ] }
    const jsonPath = { 'this.0.lorem.ipsum': 'bar' }
    assert.deepStrictEqual(unflatten(jsonPath, { safe: false }), jsonObject)
  })

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