// 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';2223var stringifyPrimitive = function(v) {24switch (typeof v) {25case 'string':26return v;2728case 'boolean':29return v ? 'true' : 'false';3031case 'number':32return isFinite(v) ? v : '';3334default:35return '';36}37};3839module.exports = function(obj, sep, eq, name) {40sep = sep || '&';41eq = eq || '=';42if (obj === null) {43obj = undefined;44}4546if (typeof obj === 'object') {47return map(objectKeys(obj), function(k) {48var ks = encodeURIComponent(stringifyPrimitive(k)) + eq;49if (isArray(obj[k])) {50return map(obj[k], function(v) {51return ks + encodeURIComponent(stringifyPrimitive(v));52}).join(sep);53} else {54return ks + encodeURIComponent(stringifyPrimitive(obj[k]));55}56}).join(sep);5758}5960if (!name) return '';61return encodeURIComponent(stringifyPrimitive(name)) + eq +62encodeURIComponent(stringifyPrimitive(obj));63};6465var isArray = Array.isArray || function (xs) {66return Object.prototype.toString.call(xs) === '[object Array]';67};6869function map (xs, f) {70if (xs.map) return xs.map(f);71var res = [];72for (var i = 0; i < xs.length; i++) {73res.push(f(xs[i], i));74}75return res;76}7778var objectKeys = Object.keys || function (obj) {79var res = [];80for (var key in obj) {81if (Object.prototype.hasOwnProperty.call(obj, key)) res.push(key);82}83return res;84};858687