Skip to content

Commit

Permalink
[Fix] when parseArrays is false, properly handle keys ending in []
Browse files Browse the repository at this point in the history
Fixes #260.
  • Loading branch information
ljharb committed May 13, 2018
1 parent 187aa3a commit 2b94ea7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/parse.js
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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"
Expand Down
9 changes: 8 additions & 1 deletion test/parse.js
Expand Up @@ -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();
});

Expand Down

0 comments on commit 2b94ea7

Please sign in to comment.