henrytseng / dataobject-parser

A nodejs module designed to parse an Object hash transposing between dot-notation and a structured heirarchy of Objects/Arrays.
MIT License
31 stars 18 forks source link

Handling Arrays #12

Open KaitaniLabs opened 4 years ago

KaitaniLabs commented 4 years ago

Great package!

I have an issue with handling arrays. I'm using untranspose to convert into a mongodb modifier.

Input data looks like this:

{
   tracking: true,
   blocked: true,
   nsfw: false,
   categories: { 
       subscribed: [ 'xag2vJYrDJdsyZfah', 'abpxCb6pN3aR4piMw' ],
       blocked: [] 
   },
   sources: { 
      subscribed: [ '4QSh8AqECuPauHFor' ], 
      blocked: [] 
   }
}

Output data looks like this:

{
    'tracking': true,
    'blocked': true,
    'nsfw': false,
    'categories.subscribed[0]': 'xag2vJYrDJdsyZfah',
    'sources.subscribed[0]': '4QSh8AqECuPauHFor'
}

I was expecting/would prefer this:

{
    'tracking': true,
    'blocked': true,
    'nsfw': false,
    'categories.subscribed': ['xag2vJYrDJdsyZfah', 'abpxCb6pN3aR4piMw'],
    'sources.subscribed': ['4QSh8AqECuPauHFor']
}

Could we include an optional flag to not break down arrays? Also, it seems a little buggy in its current format because it only included the first array element not the second.

henrytseng commented 2 years ago

Hmmm, if it's not returning both item then that would be an issue.

As for it returning arrays - it's actually designed to flatten the object - including the nested arrays. We won't be able to change that functionality but I could look at adding some options to get some customized formatting possibly.

henrytseng commented 2 years ago

Can you shed some light on this one? I added a test but didn't see any missing items. Let me know what platform you saw that one on.

https://github.com/henrytseng/dataobject-parser/pull/15