Skip to content

Commit 2b94ea7

Browse files
committed
[Fix] when parseArrays is false, properly handle keys ending in []
Fixes #260.
1 parent 187aa3a commit 2b94ea7

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

lib/parse.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ var parseObject = function (chain, val, options) {
5353
var obj;
5454
var root = chain[i];
5555

56-
if (root === '[]') {
56+
if (root === '[]' && options.parseArrays) {
5757
obj = [].concat(leaf);
5858
} else {
5959
obj = options.plainObjects ? Object.create(null) : {};
6060
var cleanRoot = root.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -1) : root;
6161
var index = parseInt(cleanRoot, 10);
62-
if (
62+
if (!options.parseArrays && cleanRoot === '') {
63+
obj = { 0: leaf };
64+
} else if (
6365
!isNaN(index)
6466
&& root !== cleanRoot
6567
&& String(index) === cleanRoot

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"test": "npm run --silent coverage",
4444
"tests-only": "node test",
4545
"readme": "evalmd README.md",
46-
"prelint": "editorconfig-tools check * lib/* test/*",
46+
"postlint": "editorconfig-tools check * lib/* test/*",
4747
"lint": "eslint lib/*.js test/*.js",
4848
"coverage": "covert test",
4949
"dist": "mkdirp dist && browserify --standalone Qs lib/index.js > dist/qs.js"

test/parse.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,14 @@ test('parse()', function (t) {
302302
});
303303

304304
t.test('allows disabling array parsing', function (st) {
305-
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { parseArrays: false }), { a: { 0: 'b', 1: 'c' } });
305+
var indices = qs.parse('a[0]=b&a[1]=c', { parseArrays: false });
306+
st.deepEqual(indices, { a: { 0: 'b', 1: 'c' } });
307+
st.equal(Array.isArray(indices.a), false, 'parseArrays:false, indices case is not an array');
308+
309+
var emptyBrackets = qs.parse('a[]=b', { parseArrays: false });
310+
st.deepEqual(emptyBrackets, { a: { 0: 'b' } });
311+
st.equal(Array.isArray(emptyBrackets.a), false, 'parseArrays:false, empty brackets case is not an array');
312+
306313
st.end();
307314
});
308315

0 commit comments

Comments
 (0)