Skip to content

Commit

Permalink
[Fix] stringify: fix a crash with strictNullHandling and a custom…
Browse files Browse the repository at this point in the history
… `filter`/`serializeDate` (#279)
  • Loading branch information
Neaox authored and ljharb committed Sep 16, 2018
1 parent d1d1a97 commit c1c2a9d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ var stringify = function stringify( // eslint-disable-line func-name-matching
obj = filter(prefix, obj);
} else if (obj instanceof Date) {
obj = serializeDate(obj);
} else if (obj === null) {
}

if (obj === null) {
if (strictNullHandling) {
return encoder && !encodeValuesOnly ? encoder(prefix, defaults.encoder) : prefix;
}
Expand Down
20 changes: 20 additions & 0 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,5 +593,25 @@ test('stringify()', function (t) {
st.end();
});

t.test('strictNullHandling works with custom filter', function (st) {
var filter = function (prefix, value) {
return value;
};

var options = { strictNullHandling: true, filter: filter };
st.equal(qs.stringify({ key: null }, options), 'key');
st.end();
});

t.test('strictNullHandling works with null serializeDate', function (st) {
var serializeDate = function () {
return null;
};
var options = { strictNullHandling: true, serializeDate: serializeDate };
var date = new Date();
st.equal(qs.stringify({ key: date }, options), 'key');
st.end();
});

t.end();
});

0 comments on commit c1c2a9d

Please sign in to comment.