Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

ignore null and undefined values #44

Closed
wants to merge 2 commits into from

3 participants

Frederico Silva Mikeal Rogers TJ Holowaychuk
Frederico Silva

Not sure if this is a correct approach but it generates a query string with less noise.

What do you think?

Mikeal Rogers

we just saw this as well using it in request, request/request#480, if it doesn't get merged before the next release i'll put us back on the core qs library.

TJ Holowaychuk tj closed this in 19fee0b
Jason Farrell jasonfarrell referenced this pull request from a commit in UseAllFive/node-querystring
TJ Holowaychuk fix ignoring of null / undefined. Closes #44 023e5ac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 11 additions and 5 deletions.
  1. +8 −4 lib/querystring.js
  2. +3 −1 test/stringify.js
12 lib/querystring.js
View
@@ -191,13 +191,17 @@ function stringifyArray(arr, prefix) {
function stringifyObject(obj, prefix) {
var ret = []
, keys = Object.keys(obj)
- , key;
+ , key
+ , val;
for (var i = 0, len = keys.length; i < len; ++i) {
key = keys[i];
- ret.push(stringify(obj[key], prefix
- ? prefix + '[' + encodeURIComponent(key) + ']'
- : encodeURIComponent(key)));
+ val = obj[key];
+ if (val !== undefined && val !== null) {
+ ret.push(stringify(val, prefix
+ ? prefix + '[' + encodeURIComponent(key) + ']'
+ : encodeURIComponent(key)));
+ }
}
return ret.join('&');
4 test/stringify.js
View
@@ -14,7 +14,9 @@ var str_identities = {
{ str: 'foo=1&bar=2', obj: {'foo' : '1', 'bar' : '2'}},
{ str: 'my%20weird%20field=q1!2%22\'w%245%267%2Fz8)%3F', obj: {'my weird field': "q1!2\"'w$5&7/z8)?"}},
{ str: 'foo%3Dbaz=bar', obj: {'foo=baz': 'bar'}},
- { str: 'foo=bar&bar=baz', obj: {foo: 'bar', bar: 'baz'}}
+ { str: 'foo=bar&bar=baz', obj: {foo: 'bar', bar: 'baz'}},
+ { str: 'foo=bar', obj: {foo: 'bar', bar: undefined}},
+ { str: 'foo=bar', obj: {foo: 'bar', bar: null}}
],
'escaping': [
{ str: 'foo=foo%20bar', obj: {foo: 'foo bar'}},
Something went wrong with that request. Please try again.