diff --git a/lib/parse.js b/lib/parse.js index 317bc6b2..c4aeaa7e 100755 --- a/lib/parse.js +++ b/lib/parse.js @@ -32,8 +32,9 @@ var parseValues = function parseQueryStringValues(str, options) { key = options.decoder(part.slice(0, pos)); val = options.decoder(part.slice(pos + 1)); } + if (has.call(obj, key)) { - obj[key] = [].concat(obj[key]).concat(val); + obj[key] = utils.combine(obj[key], val); } else { obj[key] = val; } diff --git a/lib/utils.js b/lib/utils.js index 0d410c6a..11eadd42 100755 --- a/lib/utils.js +++ b/lib/utils.js @@ -178,3 +178,24 @@ exports.isBuffer = function (obj) { return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj)); }; + +exports.combine = function combine(a, b) { + // we always use both of these, so, let's calculate them now + var firstIsArray = Array.isArray(a); + var secondIsArray = Array.isArray(b); + + // mutate `a` to append `b` and then return it + if (firstIsArray) { + secondIsArray ? a.push.apply(a, b) : a.push(b); + return a; + } + + // mutate `b` to prepend `a` and then return it + if (secondIsArray) { + b.unshift(a); + return b; + } + + // neither are arrays, so, create a new array with both + return [a, b]; +};