From ea21ba805ce56c39690790de7f9cad9653d2d29c Mon Sep 17 00:00:00 2001 From: Mohamed Omar Date: Fri, 4 Oct 2019 05:50:34 +0200 Subject: [PATCH] [Fix] `parse`: with comma true, handle field that holds an array of arrays (#335) Fixes #327. --- lib/parse.js | 5 +++++ test/parse.js | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/parse.js b/lib/parse.js index d7ac6e21..4308ca92 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -3,6 +3,7 @@ var utils = require('./utils'); var has = Object.prototype.hasOwnProperty; +var isArray = Array.isArray; var defaults = { allowDots: false, @@ -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 { diff --git a/test/parse.js b/test/parse.js index 5513ad63..60d322bb 100644 --- a/test/parse.js +++ b/test/parse.js @@ -426,6 +426,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 },