diff --git a/lib/parse.js b/lib/parse.js index c57f4691..2110e74a 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -50,14 +50,16 @@ var parseObject = function parseObject(chain, val, options) { var root = chain.shift(); var obj; - if (root === '[]') { + if (root === '[]' && options.parseArrays) { obj = []; obj = obj.concat(parseObject(chain, val, options)); } else { obj = options.plainObjects ? Object.create(null) : {}; var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root; var index = parseInt(cleanRoot, 10); - if ( + if (!options.parseArrays && cleanRoot === '') { + obj = { 0: val }; + } else if ( !isNaN(index) && root !== cleanRoot && String(index) === cleanRoot && @@ -96,8 +98,10 @@ var parseKeys = function parseKeys(givenKey, val, options) { var keys = []; if (parent) { - // If we aren't using plain objects, optionally prefix keys - // that would overwrite object prototype properties + /* + * If we aren't using plain objects, optionally prefix keys + * that would overwrite object prototype properties + */ if (!options.plainObjects && has.call(Object.prototype, parent)) { if (!options.allowPrototypes) { return; diff --git a/test/parse.js b/test/parse.js index 2ce27f2f..25c929ff 100644 --- a/test/parse.js +++ b/test/parse.js @@ -289,7 +289,14 @@ test('parse()', function (t) { }); t.test('allows disabling array parsing', function (st) { - st.deepEqual(qs.parse('a[0]=b&a[1]=c', { parseArrays: false }), { a: { '0': 'b', '1': 'c' } }); + var indices = qs.parse('a[0]=b&a[1]=c', { parseArrays: false }); + st.deepEqual(indices, { a: { 0: 'b', 1: 'c' } }); + st.equal(Array.isArray(indices.a), false, 'parseArrays:false, indices case is not an array'); + + var emptyBrackets = qs.parse('a[]=b', { parseArrays: false }); + st.deepEqual(emptyBrackets, { a: { 0: 'b' } }); + st.equal(Array.isArray(emptyBrackets.a), false, 'parseArrays:false, empty brackets case is not an array'); + st.end(); });