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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] parsing a field that holds array of arrays #335

Merged
merged 1 commit into from Oct 6, 2019
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
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