Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support key with starting brackets #202

Merged
merged 1 commit into from Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
11 changes: 5 additions & 6 deletions test/parse.js
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