"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/qs/lib/stringify.js" (11 Apr 2017, 6086 Bytes) of package /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 // Load modules
    2 
    3 var Utils = require('./utils');
    4 
    5 
    6 // Declare internals
    7 
    8 var internals = {
    9     delimiter: '&',
   10     arrayPrefixGenerators: {
   11         brackets: function (prefix, key) {
   12             return prefix + '[]';
   13         },
   14         indices: function (prefix, key) {
   15             return prefix + '[' + key + ']';
   16         },
   17         repeat: function (prefix, key) {
   18             return prefix;
   19         }
   20     }
   21 };
   22 
   23 
   24 internals.stringify = function (obj, prefix, generateArrayPrefix) {
   25 
   26     if (Utils.isBuffer(obj)) {
   27         obj = obj.toString();
   28     }
   29     else if (obj instanceof Date) {
   30         obj = obj.toISOString();
   31     }
   32     else if (obj === null) {
   33         obj = '';
   34     }
   35 
   36     if (typeof obj === 'string' ||
   37         typeof obj === 'number' ||
   38         typeof obj === 'boolean') {
   39 
   40         return [encodeURIComponent(prefix) + '=' + encodeURIComponent(obj)];
   41     }
   42 
   43     var values = [];
   44 
   45     if (typeof obj === 'undefined') {
   46         return values;
   47     }
   48 
   49     var objKeys = Object.keys(obj);
   50     for (var i = 0, il = objKeys.length; i < il; ++i) {
   51         var key = objKeys[i];
   52         if (Array.isArray(obj)) {
   53             values = values.concat(internals.stringify(obj[key], generateArrayPrefix(prefix, key), generateArrayPrefix));
   54         }
   55         else {
   56             values = values.concat(internals.stringify(obj[key], prefix + '[' + key + ']', generateArrayPrefix));
   57         }
   58     }
   59 
   60     return values;
   61 };
   62 
   63 
   64 module.exports = function (obj, options) {
   65 
   66     options = options || {};
   67     var delimiter = typeof options.delimiter === 'undefined' ? internals.delimiter : options.delimiter;
   68 
   69     var keys = [];
   70 
   71     if (typeof obj !== 'object' ||
   72         obj === null) {
   73 
   74         return '';
   75     }
   76 
   77     var arrayFormat;
   78     if (options.arrayFormat in internals.arrayPrefixGenerators) {
   79         arrayFormat = options.arrayFormat;
   80     }
   81     else if ('indices' in options) {
   82         arrayFormat = options.indices ? 'indices' : 'repeat';
   83     }
   84     else {
   85         arrayFormat = 'indices';
   86     }
   87 
   88     var generateArrayPrefix = internals.arrayPrefixGenerators[arrayFormat];
   89 
   90     var objKeys = Object.keys(obj);
   91     for (var i = 0, il = objKeys.length; i < il; ++i) {
   92         var key = objKeys[i];
   93         keys = keys.concat(internals.stringify(obj[key], key, generateArrayPrefix));
   94     }
   95 
   96     return keys.join(delimiter);
   97 };