Skip to content

Commit

Permalink
fix: support key with starting brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Mar 6, 2017
1 parent 556ee0a commit 4e55be4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/parse.js
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
9 changes: 9 additions & 0 deletions test/parse.js
Expand Up @@ -441,6 +441,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 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();
});

Expand Down

0 comments on commit 4e55be4

Please sign in to comment.