Skip to content

Commit

Permalink
[Fix] stringify: with arrayFormat: comma, include an explicit `[]…
Browse files Browse the repository at this point in the history
…` on a single-item array

Fixes #434
  • Loading branch information
ljharb committed Jun 5, 2022
1 parent 113b990 commit 4e44019
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/stringify.js
Expand Up @@ -126,7 +126,7 @@ var stringify = function stringify(
for (var i = 0; i < valuesArray.length; ++i) {
valuesJoined += (i === 0 ? '' : ',') + formatter(encoder(valuesArray[i], defaults.encoder, charset, 'value', format));
}
return [formatter(keyValue) + '=' + valuesJoined];
return [formatter(keyValue) + (i === 1 ? '[]' : '') + '=' + valuesJoined];
}
return [formatter(keyValue) + '=' + formatter(encoder(obj, defaults.encoder, charset, 'value', format))];
}
Expand Down
10 changes: 10 additions & 0 deletions test/parse.js
Expand Up @@ -410,6 +410,16 @@ test('parse()', function (t) {
st.deepEqual(qs.parse('foo=', { comma: true }), { foo: '' });
st.deepEqual(qs.parse('foo', { comma: true }), { foo: '' });
st.deepEqual(qs.parse('foo', { comma: true, strictNullHandling: true }), { foo: null });

// test cases inversed from from stringify tests
st.deepEqual(qs.parse('a[0]=c'), { a: ['c'] });
st.deepEqual(qs.parse('a[]=c'), { a: ['c'] });
st.deepEqual(qs.parse('a[]=c', { comma: true }), { a: ['c'] });

st.deepEqual(qs.parse('a[0]=c&a[1]=d'), { a: ['c', 'd'] });
st.deepEqual(qs.parse('a[]=c&a[]=d'), { a: ['c', 'd'] });
st.deepEqual(qs.parse('a=c,d', { comma: true }), { a: ['c', 'd'] });

st.end();
});

Expand Down
14 changes: 14 additions & 0 deletions test/stringify.js
Expand Up @@ -131,6 +131,20 @@ test('stringify()', function (t) {
st.end();
});

t.test('stringifies an array value with one item vs multiple items', function (st) {
st.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[0]=c');
st.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[]=c');
st.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a[]=c'); // so it parses back as an array
st.equal(qs.stringify({ a: ['c'] }, { encodeValuesOnly: true }), 'a[0]=c');

st.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[0]=c&a[1]=d');
st.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[]=c&a[]=d');
st.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true, arrayFormat: 'comma' }), 'a=c,d');
st.equal(qs.stringify({ a: ['c', 'd'] }, { encodeValuesOnly: true }), 'a[0]=c&a[1]=d');

st.end();
});

t.test('stringifies a nested array value', function (st) {
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'indices' }), 'a[b][0]=c&a[b][1]=d');
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { encodeValuesOnly: true, arrayFormat: 'brackets' }), 'a[b][]=c&a[b][]=d');
Expand Down

0 comments on commit 4e44019

Please sign in to comment.