diff --git a/lib/querystring.js b/lib/querystring.js index 9b4ca27640aa71..2249c88fdaf3b9 100644 --- a/lib/querystring.js +++ b/lib/querystring.js @@ -321,7 +321,7 @@ function parse(qs, sep, eq, options) { if (valEncoded) value = decodeStr(value, decode); - if (key || value || lastPos - posIdx > sepLen || i === 0) { + if (key || value || lastPos - posIdx > sepLen) { // Use a key array lookup instead of using hasOwnProperty(), which is // slower if (keys.indexOf(key) === -1) { @@ -334,20 +334,16 @@ function parse(qs, sep, eq, options) { // since we are generating all of the values being assigned. if (curValue.pop) curValue[curValue.length] = value; - else if (curValue) + else obj[key] = [curValue, value]; } - } else if (i === 1) { - // A pair with repeated sep could be added into obj in the first loop - // and it should be deleted - delete obj[key]; } if (--pairs === 0) break; keyEncoded = valEncoded = customDecode; encodeCheck = 0; key = value = ''; - posIdx = lastPos; + posIdx = i; lastPos = i + 1; sepIdx = eqIdx = 0; } diff --git a/test/parallel/test-querystring.js b/test/parallel/test-querystring.js index baa426094c77c6..ddc84954fc9890 100644 --- a/test/parallel/test-querystring.js +++ b/test/parallel/test-querystring.js @@ -59,6 +59,13 @@ const qsTestCases = [ ['&&&&', '', {}], ['&=&', '=', { '': '' }], ['&=&=', '=&=', { '': [ '', '' ]}], + ['a&&b', 'a=&b=', { 'a': '', 'b': '' }], + ['a=a&&b=b', 'a=a&b=b', { 'a': 'a', 'b': 'b' }], + ['&a', 'a=', { 'a': '' }], + ['&=', '=', { '': '' }], + ['a&a&', 'a=&a=', { a: [ '', '' ] }], + ['a&a&a&', 'a=&a=&a=', { a: [ '', '', '' ] }], + ['a&a&a&a&', 'a=&a=&a=&a=', { a: [ '', '', '', '' ] }], [null, '', {}], [undefined, '', {}] ];