// Copyright Joyent, Inc. and other Node contributors.1//2// Permission is hereby granted, free of charge, to any person obtaining a3// copy of this software and associated documentation files (the4// "Software"), to deal in the Software without restriction, including5// without limitation the rights to use, copy, modify, merge, publish,6// distribute, sublicense, and/or sell copies of the Software, and to permit7// persons to whom the Software is furnished to do so, subject to the8// following conditions:9//10// The above copyright notice and this permission notice shall be included11// in all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF15// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN16// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,17// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR18// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE19// USE OR OTHER DEALINGS IN THE SOFTWARE.2021'use strict';2223// If obj.hasOwnProperty has been overridden, then calling24// obj.hasOwnProperty(prop) will break.25// See: https://github.com/joyent/node/issues/170726function hasOwnProperty(obj, prop) {27return Object.prototype.hasOwnProperty.call(obj, prop);28}2930module.exports = function(qs, sep, eq, options) {31sep = sep || '&';32eq = eq || '=';33var obj = {};3435if (typeof qs !== 'string' || qs.length === 0) {36return obj;37}3839var regexp = /\+/g;40qs = qs.split(sep);4142var maxKeys = 1000;43if (options && typeof options.maxKeys === 'number') {44maxKeys = options.maxKeys;45}4647var len = qs.length;48// maxKeys <= 0 means that we should not limit keys count49if (maxKeys > 0 && len > maxKeys) {50len = maxKeys;51}5253for (var i = 0; i < len; ++i) {54var x = qs[i].replace(regexp, '%20'),55idx = x.indexOf(eq),56kstr, vstr, k, v;5758if (idx >= 0) {59kstr = x.substr(0, idx);60vstr = x.substr(idx + 1);61} else {62kstr = x;63vstr = '';64}6566k = decodeURIComponent(kstr);67v = decodeURIComponent(vstr);6869if (!hasOwnProperty(obj, k)) {70obj[k] = v;71} else if (isArray(obj[k])) {72obj[k].push(v);73} else {74obj[k] = [obj[k], v];75}76}7778return obj;79};8081var isArray = Array.isArray || function (xs) {82return Object.prototype.toString.call(xs) === '[object Array]';83};848586