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 object with number as key replaces object with an array of n number length #103

Open
TLPcoder opened this issue Jul 17, 2020 · 1 comment

Comments

@TLPcoder
Copy link

I have a use case where i always have to unflatten a json object and i dont know if that object is already unflattened. If there is a number for an object key unflatten method create and array which replaces the original object and the array length matches the number plus one. With the last element being the element originally stored by the key. This happens only for nest objects not root level.

example:

flat.unflatten({ testing: { 12: 12 }}) => { testing: [,,,,,,,,,,,,12]}

@airtonix
Copy link

airtonix commented Sep 8, 2020

experiencing this too.

https://runkit.com/airtonix/flat-number-key-results-in-array-tests

// Define `window` to fake browser env
global.window = {};
const {describe, it, expect, run} = require("jest-lite");

const flat = require("flat")


describe('unflatten', () => {
  describe('overwrite:true', () => {
      it('number keys dont result in an array', () => {
        const expectedId = 'imagine-a-uuid-here'
        const source = `{ "foo": { "12": { "id": "${expectedId}"  } } }`
        
        const output = flat.unflatten(JSON.parse(source), { 
            delimiter: '__',
            overwrite: true
        })
        expect(Array.isArray(output.foo)).toBeFalsy()
        expect(output).toHaveProperty('foo.12.id', expectedId)
      });
      
      it('alphanumber keys dont result in an array', () => {
        const expectedId = 'imagine-a-uuid-here'
        const source = `{ "foo": { "a12": { "id": "${expectedId}"  } } }`
        
        const output = flat.unflatten(JSON.parse(source), { 
            delimiter: '__',
            overwrite: true
        })
        expect(Array.isArray(output.foo)).toBeFalsy()
        expect(output).toHaveProperty('foo.a12.id', expectedId)
      });
  });
  describe('overwrite:false', () => {
      
      it('number keys dont result in an array', () => {
        const expectedId = 'imagine-a-uuid-here'
        const source = `{ "foo": { "12": { "id": "${expectedId}"  } } }`
        
        const output = flat.unflatten(JSON.parse(source), { 
            delimiter: '__',
            overwrite: false
        })
        expect(Array.isArray(output.foo)).toBeFalsy()
        expect(output).toHaveProperty('foo.12.id', expectedId)
      });
      
  });
});

const results = await run();
results.forEach((result, index) => {
    const [ root, ...testPath ] = result.testPath
    const passes = result.status === 'pass'
    console.log(`${passes ? ':)' : ':<' } ${testPath.join(' > ')}`)
    if(result.status === 'fail'){ 
        result.errors.forEach(error => console.dir(error))
    }
})

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

No branches or pull requests

2 participants