question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`skipNulls` option for queryString.stringify

See original GitHub issue

Hi!

I am now comparing 2 libs for future use on my project: qs and query-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 skip null and undefined values:


const obj1 = {
  a: 1,
  b: '2',
  c: [3, 4, 5],
  d: true,
  e: 'just string',
  f: null,
  g: undefined,
};

// qs
// can skip null/undefined values
const ex2 = qs.stringify(obj1, {
  addQueryPrefix: true,
  arrayFormat: 'comma',
  skipNulls: true,
});
const ex3 = qs.parse(ex2, {
  parseArrays: true,
  comma: true,
  ignoreQueryPrefix: true,
});

// query-string
// can store numbers and booleans
const ex4 = queryString.stringify(obj1, {
  strict: true,
});
const ex5 = queryString.parse(ex4, {
  parseNumbers: true,
  parseBooleans: true,
});

console.log('ex2', ex2);
console.log('ex3', ex3);
console.log('ex4', ex4);
console.log('ex5', ex5);

Screenshot from 2019-07-26 10-48-56

Does it sound reasonable to implement this option?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Ashtoniancommented, Aug 17, 2019

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
}
2reactions
sindresorhuscommented, Jul 26, 2019

I think the option should be skipNullAndUndefined to make it clear what it does.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Remove keys with empty or null values in qs.stringify when ...
qs package added skipNulls option since v5.1.0. To completely skip rendering keys with null values, use the skipNulls flag:.
Read more >
query-string - npm.io
Extract a query string from a URL that can be passed into .parse() . Note: This behaviour can be changed with the skipNull...
Read more >
Node.js Query String - eduCBA
guide to Node.js Query String. Here we discuss the introduction to Node.js Query String,along with working and js query string methods respectively.
Read more >
Query string | Node.js v19.3.0 Documentation
The querystring.encode() function is an alias for querystring.stringify() . ... if necessary by assigning querystring.escape to an alternative function.
Read more >
query-string@7.1.3 - jsDocs.io
Overview. Parse and stringify URL query strings ... Note: This behaviour can be changed with the skipNull option.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found