Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle stringifying empty objects with addQueryPrefix #217

Merged
merged 1 commit into from Jun 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .eslintrc
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
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
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