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 1fb74cb commit d828941
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
12 changes: 8 additions & 4 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down

0 comments on commit d828941

Please sign in to comment.