react / wstein / node_modules / browserify / node_modules / url / node_modules / querystring / encode.js
80537 views// 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 Object.keys(obj).map(function(k) {48var ks = encodeURIComponent(stringifyPrimitive(k)) + eq;49if (Array.isArray(obj[k])) {50return obj[k].map(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};646566