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

Feature request: option for deciding how to parse query without argument #223

Open
timhwang21 opened this issue Aug 18, 2017 · 6 comments
Open

Comments

@timhwang21
Copy link

Hi,

I was wondering if it would be possible to support passing an option for deciding how to parse queries without arguments? Currently url?foo can be parsed as either the empty string or as null. I am wondering if it's possible to have the parser treat a query without arguments as true.

Some background: I ran into a case in our codebase which does this, it seemed odd to me but apparently there's nothing in the standards that forbid queries without arguments. Are there more specific standards (e.g. the node implementation) that this library is trying to adhere to?

Thanks!

@ljharb
Copy link
Owner

ljharb commented Aug 22, 2017

I'm not sure what you mean by "without arguments" - in url?foo, foo is the query string; qs.parse('foo&bar=') indeed returns { bar: "", foo: "" }, and qs.stringify({ foo: '', bar: true })) returns 'foo=&bar=true'.

You can certainly do this, however:

qs.stringify({ foo: '', bar: null }, { strictNullHandling: true }); // 'foo&bar='
qs.parse('foo&bar=', { strictNullHandling: true }); // { bar: '', foo: null }

Does that cover your use case?

@timhwang21
Copy link
Author

"Query string field without a value" would probably be more accurate, sorry.

I was mainly wondering if something like this were possible -- basically, supporting valueless fields being parsed as having a given value:

qs.parse('foo&bar=', { parseEmptyValueAs: true }); // { bar: '', foo: true }
// possibly also useful
qs.parse('foo&bar=', { parseEmptyValueAs: '123' }); // { bar: '', foo: '123' }

Thanks!

@ljharb
Copy link
Owner

ljharb commented Aug 22, 2017

That's interesting. Effectively, it'd be an option for both parse and stringify that defined what primitive "an absent value" was - so qs.stringify({ foo: 123, bar: 456 }, { emptyValue: 123 }) would give 'foo&bar=456'.

I think that's reasonable, since it's pretty cumbersome to do that otherwise.

@timhwang21
Copy link
Author

Sounds good, do you think it would make sense to (eventually) combine with strictNullHandling? Instead of true that option can just take whatever value you want the absent value to take. Otherwise the user may get unexpected results with { strictNullHandling: true, emptyValue: 123 } -- it'd be user error for sure, but can be reduced by consolidating the settings.

@ljharb
Copy link
Owner

ljharb commented Aug 23, 2017

yeah, i'd think that the options would be incompatible - ie, strictNullHandling: true would throw when emptyValue is supplied, and emptyValue: null would be identical to strictNullHandling: true.

timhwang21 added a commit to timhwang21/qs that referenced this issue Sep 16, 2017
Addresses ljharb#223.

Behavior:
* When parsing and encountering empty values, replaces them with
opt.emptyValue (default to '')
* When stringifying and encountering value === opt.emptyValue, outputs a key
without a value
@timhwang21
Copy link
Author

Hi @ljharb I've pushed up a WIP patchset, ran into a small issue and was hoping to get your feedback. Thanks!

timhwang21 added a commit to timhwang21/qs that referenced this issue Nov 24, 2018
Addresses ljharb#223.

Behavior:
* When parsing and encountering empty values, replaces them with
opt.emptyValue (default to '')
* When stringifying and encountering value === opt.emptyValue, outputs a key
without a value
timhwang21 added a commit to timhwang21/qs that referenced this issue Nov 25, 2018
Addresses ljharb#223.

Behavior:
* When parsing and encountering empty values, replaces them with
opt.emptyValue (default to '')
* When stringifying and encountering value === opt.emptyValue, outputs a key
without a value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants