"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/request/node_modules/qs/lib/stringify.js" (7 Feb 2017, 5467 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 'use strict';
    2 
    3 var utils = require('./utils');
    4 var formats = require('./formats');
    5 
    6 var arrayPrefixGenerators = {
    7     brackets: function brackets(prefix) {
    8         return prefix + '[]';
    9     },
   10     indices: function indices(prefix, key) {
   11         return prefix + '[' + key + ']';
   12     },
   13     repeat: function repeat(prefix) {
   14         return prefix;
   15     }
   16 };
   17 
   18 var toISO = Date.prototype.toISOString;
   19 
   20 var defaults = {
   21     delimiter: '&',
   22     encode: true,
   23     encoder: utils.encode,
   24     serializeDate: function serializeDate(date) {
   25         return toISO.call(date);
   26     },
   27     skipNulls: false,
   28     strictNullHandling: false
   29 };
   30 
   31 var stringify = function stringify(object, prefix, generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots, serializeDate, formatter) {
   32     var obj = object;
   33     if (typeof filter === 'function') {
   34         obj = filter(prefix, obj);
   35     } else if (obj instanceof Date) {
   36         obj = serializeDate(obj);
   37     } else if (obj === null) {
   38         if (strictNullHandling) {
   39             return encoder ? encoder(prefix) : prefix;
   40         }
   41 
   42         obj = '';
   43     }
   44 
   45     if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean' || utils.isBuffer(obj)) {
   46         if (encoder) {
   47             return [formatter(encoder(prefix)) + '=' + formatter(encoder(obj))];
   48         }
   49         return [formatter(prefix) + '=' + formatter(String(obj))];
   50     }
   51 
   52     var values = [];
   53 
   54     if (typeof obj === 'undefined') {
   55         return values;
   56     }
   57 
   58     var objKeys;
   59     if (Array.isArray(filter)) {
   60         objKeys = filter;
   61     } else {
   62         var keys = Object.keys(obj);
   63         objKeys = sort ? keys.sort(sort) : keys;
   64     }
   65 
   66     for (var i = 0; i < objKeys.length; ++i) {
   67         var key = objKeys[i];
   68 
   69         if (skipNulls && obj[key] === null) {
   70             continue;
   71         }
   72 
   73         if (Array.isArray(obj)) {
   74             values = values.concat(stringify(
   75                 obj[key],
   76                 generateArrayPrefix(prefix, key),
   77                 generateArrayPrefix,
   78                 strictNullHandling,
   79                 skipNulls,
   80                 encoder,
   81                 filter,
   82                 sort,
   83                 allowDots,
   84                 serializeDate,
   85                 formatter
   86             ));
   87         } else {
   88             values = values.concat(stringify(
   89                 obj[key],
   90                 prefix + (allowDots ? '.' + key : '[' + key + ']'),
   91                 generateArrayPrefix,
   92                 strictNullHandling,
   93                 skipNulls,
   94                 encoder,
   95                 filter,
   96                 sort,
   97                 allowDots,
   98                 serializeDate,
   99                 formatter
  100             ));
  101         }
  102     }
  103 
  104     return values;
  105 };
  106 
  107 module.exports = function (object, opts) {
  108     var obj = object;
  109     var options = opts || {};
  110     var delimiter = typeof options.delimiter === 'undefined' ? defaults.delimiter : options.delimiter;
  111     var strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling;
  112     var skipNulls = typeof options.skipNulls === 'boolean' ? options.skipNulls : defaults.skipNulls;
  113     var encode = typeof options.encode === 'boolean' ? options.encode : defaults.encode;
  114     var encoder = encode ? (typeof options.encoder === 'function' ? options.encoder : defaults.encoder) : null;
  115     var sort = typeof options.sort === 'function' ? options.sort : null;
  116     var allowDots = typeof options.allowDots === 'undefined' ? false : options.allowDots;
  117     var serializeDate = typeof options.serializeDate === 'function' ? options.serializeDate : defaults.serializeDate;
  118     if (typeof options.format === 'undefined') {
  119         options.format = formats.default;
  120     } else if (!Object.prototype.hasOwnProperty.call(formats.formatters, options.format)) {
  121         throw new TypeError('Unknown format option provided.');
  122     }
  123     var formatter = formats.formatters[options.format];
  124     var objKeys;
  125     var filter;
  126 
  127     if (options.encoder !== null && options.encoder !== undefined && typeof options.encoder !== 'function') {
  128         throw new TypeError('Encoder has to be a function.');
  129     }
  130 
  131     if (typeof options.filter === 'function') {
  132         filter = options.filter;
  133         obj = filter('', obj);
  134     } else if (Array.isArray(options.filter)) {
  135         filter = options.filter;
  136         objKeys = filter;
  137     }
  138 
  139     var keys = [];
  140 
  141     if (typeof obj !== 'object' || obj === null) {
  142         return '';
  143     }
  144 
  145     var arrayFormat;
  146     if (options.arrayFormat in arrayPrefixGenerators) {
  147         arrayFormat = options.arrayFormat;
  148     } else if ('indices' in options) {
  149         arrayFormat = options.indices ? 'indices' : 'repeat';
  150     } else {
  151         arrayFormat = 'indices';
  152     }
  153 
  154     var generateArrayPrefix = arrayPrefixGenerators[arrayFormat];
  155 
  156     if (!objKeys) {
  157         objKeys = Object.keys(obj);
  158     }
  159 
  160     if (sort) {
  161         objKeys.sort(sort);
  162     }
  163 
  164     for (var i = 0; i < objKeys.length; ++i) {
  165         var key = objKeys[i];
  166 
  167         if (skipNulls && obj[key] === null) {
  168             continue;
  169         }
  170 
  171         keys = keys.concat(stringify(
  172             obj[key],
  173             key,
  174             generateArrayPrefix,
  175             strictNullHandling,
  176             skipNulls,
  177             encoder,
  178             filter,
  179             sort,
  180             allowDots,
  181             serializeDate,
  182             formatter
  183         ));
  184     }
  185 
  186     return keys.join(delimiter);
  187 };