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

qs.stringify does not respect the addQueryPrefix param when the result of stringify is an empty string #418

Open
ehab-cl opened this issue Aug 2, 2021 · 3 comments

Comments

@ehab-cl
Copy link

ehab-cl commented Aug 2, 2021

Hey, thank you for the amazing library.

The following will return an empty string which i think is unexpected
qs.stringify({}, { addQueryPrefix: true })
i would expect the previous to return "?"

The use case is for many navigating methods including the native replaceState in window.history they discard an empty string while "?" means to empty the url query params.

I have noticed that this change was introduced in this commit 229b0e9#diff-0b54a0fe4e28dd7c969fecd6711539ca19dcd5cc1e19a175193d805468d863caL208, previously it would work as expected.

@ljharb
Copy link
Owner

ljharb commented Aug 2, 2021

Yes, imo it would be incorrect to produce a ? when there's nothing in the string; see #213. You can do qs.stringify({}, { addQueryPrefix: true }) || '?' pretty easily if that's something you want.

@ehab-cl
Copy link
Author

ehab-cl commented Aug 11, 2021

@ljharb Thanks for the reply. Yea what you suggested is what i already did in my code. But i still think it is worth adding in documentation that addQueryPrefix does not work when stringify produces empty string.

Still i think the way the option is called does not give any indicator that query prefix won't be added when the result is an empty string , and one can do something like the following if not interested in getting just a question mark without anything.

// lodash isEmpty and assuming that params has no undefined values 
isEmpty(params) ? "" : qs.stringify(params, { addQueryPrefix: true })

@ljharb
Copy link
Owner

ljharb commented Aug 11, 2021

I'm fine adding it to the docs.

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