react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / qs / lib / stringify.js
81146 views// Load modules12var Utils = require('./utils');345// Declare internals67var internals = {8delimiter: '&',9arrayPrefixGenerators: {10brackets: function (prefix, key) {1112return prefix + '[]';13},14indices: function (prefix, key) {1516return prefix + '[' + key + ']';17},18repeat: function (prefix, key) {1920return prefix;21}22},23strictNullHandling: false24};252627internals.stringify = function (obj, prefix, generateArrayPrefix, strictNullHandling, filter) {2829if (typeof filter === 'function') {30obj = filter(prefix, obj);31}32else if (Utils.isBuffer(obj)) {33obj = obj.toString();34}35else if (obj instanceof Date) {36obj = obj.toISOString();37}38else if (obj === null) {39if (strictNullHandling) {40return Utils.encode(prefix);41}4243obj = '';44}4546if (typeof obj === 'string' ||47typeof obj === 'number' ||48typeof obj === 'boolean') {4950return [Utils.encode(prefix) + '=' + Utils.encode(obj)];51}5253var values = [];5455if (typeof obj === 'undefined') {56return values;57}5859var objKeys = Array.isArray(filter) ? filter : Object.keys(obj);60for (var i = 0, il = objKeys.length; i < il; ++i) {61var key = objKeys[i];6263if (Array.isArray(obj)) {64values = values.concat(internals.stringify(obj[key], generateArrayPrefix(prefix, key), generateArrayPrefix, strictNullHandling, filter));65}66else {67values = values.concat(internals.stringify(obj[key], prefix + '[' + key + ']', generateArrayPrefix, strictNullHandling, filter));68}69}7071return values;72};737475module.exports = function (obj, options) {7677options = options || {};78var delimiter = typeof options.delimiter === 'undefined' ? internals.delimiter : options.delimiter;79var strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : internals.strictNullHandling;80var objKeys;81var filter;82if (typeof options.filter === 'function') {83filter = options.filter;84obj = filter('', obj);85}86else if (Array.isArray(options.filter)) {87objKeys = filter = options.filter;88}8990var keys = [];9192if (typeof obj !== 'object' ||93obj === null) {9495return '';96}9798var arrayFormat;99if (options.arrayFormat in internals.arrayPrefixGenerators) {100arrayFormat = options.arrayFormat;101}102else if ('indices' in options) {103arrayFormat = options.indices ? 'indices' : 'repeat';104}105else {106arrayFormat = 'indices';107}108109var generateArrayPrefix = internals.arrayPrefixGenerators[arrayFormat];110111if (!objKeys) {112objKeys = Object.keys(obj);113}114for (var i = 0, il = objKeys.length; i < il; ++i) {115var key = objKeys[i];116keys = keys.concat(internals.stringify(obj[key], key, generateArrayPrefix, strictNullHandling, filter));117}118119return keys.join(delimiter);120};121122123