Skip to content

Commit

Permalink
[Fix] support keys starting with brackets.
Browse files Browse the repository at this point in the history
Relates to #200.
  • Loading branch information
dead-horse authored and ljharb committed Mar 6, 2017
1 parent 4a7de7e commit 12152db
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
11 changes: 6 additions & 5 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,27 @@ var parseKeys = function parseQueryStringKeys(givenKey, val, options) {

// The regex chunks

var parent = /^([^[]*)/;
var brackets = /(\[[^[\]]*])/;
var child = /(\[[^[\]]*])/g;

// Get the parent

var segment = parent.exec(key);
var segment = brackets.exec(key);
var parent = segment ? key.slice(0, segment.index) : key;

// Stash the parent if it exists

var keys = [];
if (segment[1]) {
if (parent) {
// If we aren't using plain objects, optionally prefix keys
// that would overwrite object prototype properties
if (!options.plainObjects && has.call(Object.prototype, segment[1])) {
if (!options.plainObjects && has.call(Object.prototype, parent)) {
if (!options.allowPrototypes) {
return;
}
}

keys.push(segment[1]);
keys.push(parent);
}

// Loop through children appending to the array until we hit depth
Expand Down
11 changes: 5 additions & 6 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,16 +439,15 @@ test('parse()', function (t) {

t.test('params starting with a closing bracket', function (st) {
st.deepEqual(qs.parse(']=toString'), { ']': 'toString' });
st.deepEqual(qs.parse(']]=toString'), { ']]': 'toString' });
st.deepEqual(qs.parse(']hello]=toString'), { ']hello]': 'toString' });
st.end();
});

t.test('params starting with a starting bracket', function (st) {
st.deepEqual(qs.parse('[=toString'), {});
st.end();
});

t.test('params starting with a starting bracket', function (st) {
st.deepEqual(qs.parse('[=toString'), {});
st.deepEqual(qs.parse('[=toString'), { '[': 'toString' });
st.deepEqual(qs.parse('[[=toString'), { '[[': 'toString' });
st.deepEqual(qs.parse('[hello[=toString'), { '[hello[': 'toString' });
st.end();
});

Expand Down

0 comments on commit 12152db

Please sign in to comment.