Skip to content

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