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] parse/stringify: add allowEmptyArrays option to allow [] in object values #487

Merged
merged 1 commit into from
Jan 28, 2024

Conversation

aks-
Copy link
Contributor

@aks- aks- commented Jan 26, 2024

This PR adds allowEmptyArrays option which let's you keep empty arrays in the object values and address this issue

Without allowEmptyArrays option

qs.stringify({ foo: [], bar: baz }) // 'bar=baz'
qs.parse(qs.stringify({ foo: [], bar: baz })) // { bar: baz }

With allowEmptyArrays options set to true

qs.stringify({ foo: [], bar: baz }, { allowEmptyArrays: true }) // 'foo[]&bar=baz'
qs.parse(qs.stringify({ foo: [], bar: baz })) // { foo: [], bar: baz }

lib/parse.js Outdated Show resolved Hide resolved
lib/parse.js Outdated Show resolved Hide resolved
lib/stringify.js Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
test/parse.js Show resolved Hide resolved
test/stringify.js Outdated Show resolved Hide resolved
@aks- aks- force-pushed the add-allowEmptyArrays branch 2 times, most recently from a7f71fd to 62397f0 Compare January 26, 2024 22:04
lib/parse.js Outdated Show resolved Hide resolved
lib/parse.js Outdated Show resolved Hide resolved
lib/stringify.js Outdated Show resolved Hide resolved
lib/stringify.js Outdated Show resolved Hide resolved
lib/stringify.js Outdated Show resolved Hide resolved
@aks-
Copy link
Contributor Author

aks- commented Jan 27, 2024

@ljharb qs.stringify({ foo: [], bar: 'baz' }, { allowEmptyArrays: true }) produces foo[]&bar=baz. But I am wondering if you need { allowEmptyArrays: true } when parsing because one can argue if the string has foo[] that means we want to be able to parse it to original object that was handed to qs.stringify. What do you think about dropping allowEmptyArrays option from the qs.parse?

Current behaviour is qs.parse('foo[]&bar=baz') -> { foo: [ '' ], bar: 'baz' }. After the above change it would produce { foo: [], bar: 'baz' }. I would guess that would be a breaking change where ''(empty string) is dropped?

@ljharb
Copy link
Owner

ljharb commented Jan 27, 2024

Yes, that would be a breaking change, and it’s important that things round trip - so i think we do need the option in both.

test/stringify.js Outdated Show resolved Hide resolved
@aks- aks- force-pushed the add-allowEmptyArrays branch 2 times, most recently from 281de52 to f3b3bc5 Compare January 27, 2024 20:39
test/parse.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.

LGTM; i can make the remaining changes as part of landing, but feel free to do it yourself too :-)

lib/parse.js Outdated Show resolved Hide resolved
lib/parse.js Outdated Show resolved Hide resolved
lib/stringify.js Outdated Show resolved Hide resolved
lib/stringify.js Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
test/stringify.js Outdated Show resolved Hide resolved
@ljharb ljharb changed the title Add allow empty arrays [New] parse/stringify: add allowEmptyArrays option to allow [] in object values Jan 28, 2024
Copy link

codecov bot commented Jan 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (04f422f) 99.80% compared to head (f22b2bc) 99.81%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #487   +/-   ##
=======================================
  Coverage   99.80%   99.81%           
=======================================
  Files           9        9           
  Lines        1543     1583   +40     
  Branches      177      183    +6     
=======================================
+ Hits         1540     1580   +40     
  Misses          3        3           

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

@ljharb ljharb merged commit f22b2bc into ljharb:main Jan 28, 2024
320 of 321 checks passed
@konnng-dev
Copy link

@ljharb any ETA to release a new version containing these changes?

@ljharb
Copy link
Owner

ljharb commented Feb 1, 2024

@konnng-dev there's a few open PRs i want to land first

@mattsputnikdigital
Copy link

Needs adding to the typescript definitions

Object literal may only specify known properties, and 'allowEmptyArrays' does not exist in type 'IStringifyOptions'.

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.

None yet

4 participants