Skip to content

Commit

Permalink
[Fix] parse: with comma true, handle field that holds an array of a…
Browse files Browse the repository at this point in the history
…rrays (#335)

Fixes #327.
  • Loading branch information
Mohamed Omar authored and ljharb committed Oct 4, 2019
1 parent 99a8181 commit 77c2846
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/parse.js
Expand Up @@ -3,6 +3,7 @@
var utils = require('./utils');

var has = Object.prototype.hasOwnProperty;
var isArray = Array.isArray;

var defaults = {
allowDots: false,
Expand Down Expand Up @@ -87,6 +88,10 @@ var parseValues = function parseQueryStringValues(str, options) {
val = val.split(',');
}

if (part.indexOf('[]=') > -1) {
val = isArray(val) ? [val] : val;
}

if (has.call(obj, key)) {
obj[key] = utils.combine(obj[key], val);
} else {
Expand Down
9 changes: 9 additions & 0 deletions test/parse.js
Expand Up @@ -414,6 +414,15 @@ test('parse()', function (t) {
st.end();
});

t.test('parses brackets holds array of arrays when having two parts of strings with comma as array divider', function (st) {
st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=4,5,6', { comma: true }), { foo: [['1', '2', '3'], ['4', '5', '6']] });
st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=', { comma: true }), { foo: [['1', '2', '3'], ''] });
st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=,', { comma: true }), { foo: [['1', '2', '3'], ['', '']] });
st.deepEqual(qs.parse('foo[]=1,2,3&foo[]=a', { comma: true }), { foo: [['1', '2', '3'], 'a'] });

st.end();
});

t.test('parses an object in dot notation', function (st) {
var input = {
'user.name': { 'pop[bob]': 3 },
Expand Down

0 comments on commit 77c2846

Please sign in to comment.