Skip to content

Commit f884e2d

Browse files
Mohamed Omarljharb
authored andcommitted
[Fix] parse: with comma true, handle field that holds an array of arrays
Fixes #327.
1 parent 698b683 commit f884e2d

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/parse.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var utils = require('./utils');
44

55
var has = Object.prototype.hasOwnProperty;
6+
var isArray = Array.isArray;
67

78
var defaults = {
89
allowDots: false,
@@ -87,6 +88,10 @@ var parseValues = function parseQueryStringValues(str, options) {
8788
val = val.split(',');
8889
}
8990

91+
if (part.indexOf('[]=') > -1) {
92+
val = isArray(val) ? [val] : val;
93+
}
94+
9095
if (has.call(obj, key)) {
9196
obj[key] = utils.combine(obj[key], val);
9297
} else {

test/parse.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,15 @@ test('parse()', function (t) {
414414
st.end();
415415
});
416416

417+
t.test('parses brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {
418+
st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=4,5,6', { comma: true }), { foo: [['1', '2', '3'], ['4', '5', '6']] });
419+
st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=', { comma: true }), { foo: [['1', '2', '3'], ''] });
420+
st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=,', { comma: true }), { foo: [['1', '2', '3'], ['', '']] });
421+
st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });
422+
423+
st.end();
424+
});
425+
417426
t.test('parses an object in dot notation', function (st) {
418427
var input = {
419428
'user.name': { 'pop[bob]': 3 },

0 commit comments

Comments
 (0)