ljharb / qs

A querystring parser with nesting support
BSD 3-Clause "New" or "Revised" License
8.47k stars 731 forks source link

[New] `stringify`/`parse`: add `encodeDotKeys`/`decodeDotKeys` respectively to allow . in object keys #488

Closed aks- closed 5 months ago

aks- commented 6 months ago

Previously the dot in the keys would be encoded but the round-tripping was broken. Here is the previous behavior

const opts = { allowDots: true };
qs.parse(qs.stringify({ "name.obj": { "first": "John", "last": "Doe" } }, opts), opts)
 // => { name: { obj: { first: 'John', last: 'Doe' } } }

This PR fixes the above behavior and fixes #249:

 qs.parse(qs.stringify({ "name.obj": { "first": "John", "last": "Doe" } }, { allowDots: true, encodeDotKeys: true }), { allowDots: true, decodeDotKeys: true })
{ 'name.obj': { first: 'John', last: 'Doe' } }
aks- commented 6 months ago

@ljharb I wonder if we need allowDots to be true for encodeDotKeys and decodeDotKeys work. Also I can not think of any other name that is similar for parse and stringify method. Any suggestions?

codecov[bot] commented 6 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 99.81%. Comparing base (5f0449f) to head (867fc7b).

:exclamation: Current head 867fc7b differs from pull request most recent head 30004b2. Consider uploading reports for the commit 30004b2 to get more accurate results

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #488 +/- ## ======================================= Coverage 99.81% 99.81% ======================================= Files 9 9 Lines 1600 1645 +45 Branches 187 190 +3 ======================================= + Hits 1597 1642 +45 Misses 3 3 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

aks- commented 6 months ago

@ljharb I realised that allowDots = false & encodeDotInKeys does not break the round-tripping but it does not encode the dot in key. I just pushed a commit which adjusts the tests and always encodes the key if encodeDotInKeys is set to true except for the invalid case where the error would be thrown. Can you please take another look?