Skip to content

Commit 1c045ca

Browse files
dead-horseljharb
authored andcommitted
[Fix] support keys starting with brackets.
Relates to #200.
1 parent 7d58e13 commit 1c045ca

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

lib/parse.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,26 +84,27 @@ var parseKeys = function parseKeys(givenKey, val, options) {
8484

8585
// The regex chunks
8686

87-
var parent = /^([^[]*)/;
87+
var brackets = /(\[[^[\]]*])/;
8888
var child = /(\[[^[\]]*])/g;
8989

9090
// Get the parent
9191

92-
var segment = parent.exec(key);
92+
var segment = brackets.exec(key);
93+
var parent = segment ? key.slice(0, segment.index) : key;
9394

9495
// Stash the parent if it exists
9596

9697
var keys = [];
97-
if (segment[1]) {
98+
if (parent) {
9899
// If we aren't using plain objects, optionally prefix keys
99100
// that would overwrite object prototype properties
100-
if (!options.plainObjects && has.call(Object.prototype, segment[1])) {
101+
if (!options.plainObjects && has.call(Object.prototype, parent)) {
101102
if (!options.allowPrototypes) {
102103
return;
103104
}
104105
}
105106

106-
keys.push(segment[1]);
107+
keys.push(parent);
107108
}
108109

109110
// Loop through children appending to the array until we hit depth

test/parse.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,16 +428,15 @@ test('parse()', function (t) {
428428

429429
t.test('params starting with a closing bracket', function (st) {
430430
st.deepEqual(qs.parse(']=toString'), { ']': 'toString' });
431+
st.deepEqual(qs.parse(']]=toString'), { ']]': 'toString' });
432+
st.deepEqual(qs.parse(']hello]=toString'), { ']hello]': 'toString' });
431433
st.end();
432434
});
433435

434436
t.test('params starting with a starting bracket', function (st) {
435-
st.deepEqual(qs.parse('[=toString'), {});
436-
st.end();
437-
});
438-
439-
t.test('params starting with a starting bracket', function (st) {
440-
st.deepEqual(qs.parse('[=toString'), {});
437+
st.deepEqual(qs.parse('[=toString'), { '[': 'toString' });
438+
st.deepEqual(qs.parse('[[=toString'), { '[[': 'toString' });
439+
st.deepEqual(qs.parse('[hello[=toString'), { '[hello[': 'toString' });
441440
st.end();
442441
});
443442

0 commit comments

Comments
 (0)