-
-
Notifications
You must be signed in to change notification settings - Fork 455
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
skipNulls
option for queryString.stringify
#196
Comments
Sure, PR welcome. For the person that wants to work on this, don't forget to add docs, tests, and update the TypeScript definition. |
I think the option should be |
I'll get to work on this now. |
You can now pass an option called `skipNullAndUndefined` to the options object in stringify to prevent the generator from generating keys for those keys. Fixes sindresorhus#196.
@sindresorhus PR is ready. Looking forward it to be merged. |
@sindresorhus |
3 separate configurable options would be very nice inspired by https://www.npmjs.com/package/qs. {
"skipNulls" : true|false,
"skipUndefined": true|false, // currently default
"skipEmptyStrings":true|false
} |
@ashtonian That seems a bit over the top in configurability. You would need to present arguments for why those are needed with actual use-cases. |
@sindresorhus - ha it may be a bit over the top. For separating undefined and nulls I would prefer this because sometimes they are used differently in the back end. I've never wanted to pass undefined, but I have wanted to pass nulls, sometimes to use it as a value, or sometimes to clear a relationship/field in an edit form submission. For the // ...
data () {
return {
searchForm: {
someFieldA:"",
someFieldB:undefined,
someFieldC:null
}
}
// ...
this.$router.replace({
query: this.searchForm // passes "" strings around for optional fields
})
// ...
this.axios.get("/some/api/search",{query:this.searchForm}) I have certain 3rd part input components that self parse to a respective type that is bound to my components data objects |
Stumbled on this thread looking for the same thing. |
I found a workaround, but it would be great to have this built-in Here's my workaround: const routerQuery = queryString.parse(router.asPath.split(/\?/)[1], {
parseNumbers: true,
parseBooleans: true
});
const removeFalsy = obj => {
let newObj = {};
Object.keys(obj).forEach(prop => {
if (obj[prop]) {
newObj[prop] = obj[prop];
}
});
return newObj;
};
const query = removeFalsy(routerQuery); |
Hi!
I am now comparing 2 libs for future use on my project:
qs
andquery-string
.The second one has some advantages like encoding/decoding booleans and numbers which are important for me. But the 1st one has
skipNulls
option which allows to skipnull
andundefined
values:Does it sound reasonable to implement this option?
The text was updated successfully, but these errors were encountered: