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 > Option to skip empty string at "qs.stringify()" #372

Closed
zxy-c opened this issue Jul 2, 2020 · 11 comments
Closed

Feature > Option to skip empty string at "qs.stringify()" #372

zxy-c opened this issue Jul 2, 2020 · 11 comments

Comments

@zxy-c
Copy link

zxy-c commented Jul 2, 2020

const params = {
  a: "",
  b: "value"
}
const result = qs.stringify(params,{
  skipNulls:true,
  skipEmptyString:true
});
result === "b=value"
@ljharb
Copy link
Owner

ljharb commented Jul 8, 2020

See #226 and #307.

@adampatterson
Copy link

I am looking for this option as well, the vanity in me would like to see nice clean URLs 😆

@meotimdihia
Copy link

please add this option.

@ljharb
Copy link
Owner

ljharb commented Mar 2, 2023

@phuocnelet it never disappeared because it never existed.

@phuocnelet
Copy link

@ljharb My bad, I was confused with other library... My bad again but it could be a great feature actually !

@ghost
Copy link

ghost commented Aug 4, 2023

why not add one flag to clear keys with empty-string?
image

@ljharb
Copy link
Owner

ljharb commented Aug 4, 2023

@motograter certainly we could add it - but we're not going to add features just because another library has it. What's the use case?

Either way, i think #372 (comment) still summarizes the best approach here, so I'm going to close this in favor of those.

@ljharb ljharb closed this as completed Aug 4, 2023
@ghost
Copy link

ghost commented Aug 5, 2023

@ljharb
hi man thx 4 answer,
example this is not first issue to feature clearing keys with empty string, so people asking about it

second
ususaly empty string this init value, so why send empty filter ?

and this strange logic , why empty array or object correct convert to "" , but when is string we parsed to "prop="

Empty strings and null values will omit the value, but the equals sign (=) remains in place:

assert.equal(qs.stringify({ a: '' }), 'a=');
Key with no values (such as an empty object or array) will return nothing:

assert.equal(qs.stringify({ a: [] }), '');
assert.equal(qs.stringify({ a: {} }), '');
assert.equal(qs.stringify({ a: [{}] }), '');
assert.equal(qs.stringify({ a: { b: []} }), '');
assert.equal(qs.stringify({ a: { b: {}} }), '');

and i have another issue another lib
image

@ljharb
Copy link
Owner

ljharb commented Aug 5, 2023

@motograter i'm not entirely clear what your comment is trying to get across.

I do understand that if you want someone to be able to search for the empty string, the interface is tricky unless you force them to always write the quotes (or whatever delimiter). I'm not sure how this applies to qs, though, since via a custom encoder or decoder you can make whatever decisions you like there.

@ghost
Copy link

ghost commented Aug 6, 2023

@ljharb oh agree, thank you, i some fixes in my backend server
this works, but string little confuses me

example i have 5 filters, init values on client = empty strings, in future i some change on client and send to server

and this initial string look like it , before i change any of filter

http://localhost:4002/api/endpoint?page=2&filters[name]=&filters[type]=&filters[age]=&filters[date]=

this is normal?

@ljharb
Copy link
Owner

ljharb commented Aug 7, 2023

Yes, an empty string for foo stringifies as foo=.

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

5 participants