Skip to content

Commit

Permalink
[Fix] stringify: fix arrayFormat comma with empty array/objects
Browse files Browse the repository at this point in the history
  • Loading branch information
daggerjames authored and ljharb committed Jun 9, 2019
1 parent af9bf48 commit deada94
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/stringify.js
Expand Up @@ -80,7 +80,7 @@ var stringify = function stringify(
return serializeDate(value);
}
return value;
}).join(',');
});
}

if (obj === null) {
Expand All @@ -106,7 +106,10 @@ var stringify = function stringify(
}

var objKeys;
if (isArray(filter)) {
if (generateArrayPrefix === 'comma' && isArray(obj)) {
// we need to join elements in
objKeys = [{ value: obj.length > 0 ? obj.join(',') || null : undefined }];
} else if (isArray(filter)) {
objKeys = filter;
} else {
var keys = Object.keys(obj);
Expand All @@ -115,7 +118,7 @@ var stringify = function stringify(

for (var i = 0; i < objKeys.length; ++i) {
var key = objKeys[i];
var value = obj[key];
var value = typeof key === 'object' && key.value !== undefined ? key.value : obj[key];

if (skipNulls && value === null) {
continue;
Expand Down
21 changes: 21 additions & 0 deletions test/stringify.js
Expand Up @@ -336,6 +336,27 @@ test('stringify()', function (t) {
st.end();
});

t.test('stringifies an empty array in different arrayFormat', function (st) {
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false }), 'b[0]=&c=c');
// arrayFormat default
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'indices' }), 'b[0]=&c=c');
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'brackets' }), 'b[]=&c=c');
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'repeat' }), 'b=&c=c');
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma' }), 'b=&c=c');
// with strictNullHandling
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'indices', strictNullHandling: true }), 'b[0]&c=c');
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'brackets', strictNullHandling: true }), 'b[]&c=c');
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'repeat', strictNullHandling: true }), 'b&c=c');
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma', strictNullHandling: true }), 'b&c=c');
// with skipNulls
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'indices', skipNulls: true }), 'c=c');
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'brackets', skipNulls: true }), 'c=c');
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'repeat', skipNulls: true }), 'c=c');
st.equal(qs.stringify({ a: [], b: [null], c: 'c' }, { encode: false, arrayFormat: 'comma', skipNulls: true }), 'c=c');

st.end();
});

t.test('stringifies a null object', { skip: !Object.create }, function (st) {
var obj = Object.create(null);
obj.a = 'b';
Expand Down

0 comments on commit deada94

Please sign in to comment.