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

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

Merged
merged 2 commits into from
Feb 28, 2024

Conversation

aks-
Copy link
Contributor

@aks- aks- commented Jan 28, 2024

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-
Copy link
Contributor Author

aks- commented Jan 28, 2024

@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?

Copy link

codecov bot commented Jan 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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

❗ 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
@@           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           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Owner

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add test cases that use encodeValuesOnly with this option.

lib/parse.js Outdated Show resolved Hide resolved
@aks- aks- requested a review from ljharb January 28, 2024 20:23
@ljharb ljharb force-pushed the make-allowdots-roundtripping branch from 14a0bcd to 0b83865 Compare January 28, 2024 22:52
README.md Outdated Show resolved Hide resolved
test/parse.js Outdated Show resolved Hide resolved
test/stringify.js Outdated Show resolved Hide resolved
Copy link
Owner

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This looks good; I can clean up the remaining comments.

lib/parse.js Outdated Show resolved Hide resolved
lib/stringify.js Outdated Show resolved Hide resolved
@aks-
Copy link
Contributor Author

aks- commented Feb 2, 2024

@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?

@aks- aks- requested a review from ljharb February 2, 2024 08:51
@ljharb ljharb force-pushed the make-allowdots-roundtripping branch from 867fc7b to d514cc9 Compare February 28, 2024 04:15
@ljharb ljharb force-pushed the make-allowdots-roundtripping branch from d514cc9 to 882bee2 Compare February 28, 2024 04:22
@ljharb ljharb force-pushed the make-allowdots-roundtripping branch 2 times, most recently from 7231cb8 to 30004b2 Compare February 28, 2024 05:49
@ljharb ljharb merged commit 30004b2 into ljharb:main Feb 28, 2024
320 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

If { allowDots: true } is passed, dots in property names should be encoded
2 participants