From 2b94ea726fb4fe5d8c7b9bd2ee5cfb4d93dad89d Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sun, 13 May 2018 15:20:45 -0700 Subject: [PATCH] [Fix] when `parseArrays` is false, properly handle keys ending in `[]` Fixes #260. --- lib/parse.js | 6 ++++-- package.json | 2 +- test/parse.js | 9 ++++++++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index 47570132..4abc1a70 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -53,13 +53,15 @@ var parseObject = function (chain, val, options) { var obj; var root = chain[i]; - if (root === '[]') { + if (root === '[]' && options.parseArrays) { obj = [].concat(leaf); } 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: leaf }; + } else if ( !isNaN(index) && root !== cleanRoot && String(index) === cleanRoot diff --git a/package.json b/package.json index 2c654900..ed6ccf06 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "test": "npm run --silent coverage", "tests-only": "node test", "readme": "evalmd README.md", - "prelint": "editorconfig-tools check * lib/* test/*", + "postlint": "editorconfig-tools check * lib/* test/*", "lint": "eslint lib/*.js test/*.js", "coverage": "covert test", "dist": "mkdirp dist && browserify --standalone Qs lib/index.js > dist/qs.js" diff --git a/test/parse.js b/test/parse.js index 0f8fe457..c6d0fbfe 100644 --- a/test/parse.js +++ b/test/parse.js @@ -302,7 +302,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(); });