From 12152db995bd07a5483fa14e01c52bf20687e617 Mon Sep 17 00:00:00 2001 From: dead-horse Date: Mon, 6 Mar 2017 12:26:45 +0800 Subject: [PATCH] [Fix] support keys starting with brackets. Relates to #200. --- lib/parse.js | 11 ++++++----- test/parse.js | 11 +++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index 9530ad41..1307e9d7 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -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 diff --git a/test/parse.js b/test/parse.js index 2e1116ec..e451e91f 100644 --- a/test/parse.js +++ b/test/parse.js @@ -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(); });