Skip to content

Commit

Permalink
Handle stringifying empty objects with addQueryPrefix
Browse files Browse the repository at this point in the history
Stringifying an empty object would produce a string such as '?'. With
this fix, we output an empty line instead.
  • Loading branch information
nerfologist committed Jun 27, 2017
1 parent b9519c9 commit 229b0e9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"extends": "@ljharb",

"rules": {
"complexity": [2, 27],
"complexity": [2, 28],
"consistent-return": 1,
"id-length": [2, { "min": 1, "max": 25, "properties": "never" }],
"indent": [2, 4],
"max-params": [2, 12],
"max-statements": [2, 44],
"max-statements": [2, 45],
"no-continue": 1,
"no-magic-numbers": 0,
"no-restricted-syntax": [2, "BreakStatement", "DebuggerStatement", "ForInStatement", "LabeledStatement", "WithStatement"],
Expand Down
3 changes: 2 additions & 1 deletion lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ module.exports = function (object, opts) {
));
}

var joined = keys.join(delimiter);
var prefix = options.addQueryPrefix === true ? '?' : '';

return prefix + keys.join(delimiter);
return joined.length > 0 ? prefix + joined : '';
};
5 changes: 5 additions & 0 deletions test/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ test('stringify()', function (t) {
st.end();
});

t.test('with query prefix, outputs blank string given an empty object', function (st) {
st.equal(qs.stringify({}, { addQueryPrefix: true }), '');
st.end();
});

t.test('stringifies a nested object', function (st) {
st.equal(qs.stringify({ a: { b: 'c' } }), 'a%5Bb%5D=c');
st.equal(qs.stringify({ a: { b: { c: { d: 'e' } } } }), 'a%5Bb%5D%5Bc%5D%5Bd%5D=e');
Expand Down

0 comments on commit 229b0e9

Please sign in to comment.