"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/request/node_modules/qs/lib/parse.js" (7 Feb 2017, 5420 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 
    5 var has = Object.prototype.hasOwnProperty;
    6 
    7 var defaults = {
    8     allowDots: false,
    9     allowPrototypes: false,
   10     arrayLimit: 20,
   11     decoder: utils.decode,
   12     delimiter: '&',
   13     depth: 5,
   14     parameterLimit: 1000,
   15     plainObjects: false,
   16     strictNullHandling: false
   17 };
   18 
   19 var parseValues = function parseValues(str, options) {
   20     var obj = {};
   21     var parts = str.split(options.delimiter, options.parameterLimit === Infinity ? undefined : options.parameterLimit);
   22 
   23     for (var i = 0; i < parts.length; ++i) {
   24         var part = parts[i];
   25         var pos = part.indexOf(']=') === -1 ? part.indexOf('=') : part.indexOf(']=') + 1;
   26 
   27         var key, val;
   28         if (pos === -1) {
   29             key = options.decoder(part);
   30             val = options.strictNullHandling ? null : '';
   31         } else {
   32             key = options.decoder(part.slice(0, pos));
   33             val = options.decoder(part.slice(pos + 1));
   34         }
   35         if (has.call(obj, key)) {
   36             obj[key] = [].concat(obj[key]).concat(val);
   37         } else {
   38             obj[key] = val;
   39         }
   40     }
   41 
   42     return obj;
   43 };
   44 
   45 var parseObject = function parseObject(chain, val, options) {
   46     if (!chain.length) {
   47         return val;
   48     }
   49 
   50     var root = chain.shift();
   51 
   52     var obj;
   53     if (root === '[]') {
   54         obj = [];
   55         obj = obj.concat(parseObject(chain, val, options));
   56     } else {
   57         obj = options.plainObjects ? Object.create(null) : {};
   58         var cleanRoot = root[0] === '[' && root[root.length - 1] === ']' ? root.slice(1, root.length - 1) : root;
   59         var index = parseInt(cleanRoot, 10);
   60         if (
   61             !isNaN(index) &&
   62             root !== cleanRoot &&
   63             String(index) === cleanRoot &&
   64             index >= 0 &&
   65             (options.parseArrays && index <= options.arrayLimit)
   66         ) {
   67             obj = [];
   68             obj[index] = parseObject(chain, val, options);
   69         } else {
   70             obj[cleanRoot] = parseObject(chain, val, options);
   71         }
   72     }
   73 
   74     return obj;
   75 };
   76 
   77 var parseKeys = function parseKeys(givenKey, val, options) {
   78     if (!givenKey) {
   79         return;
   80     }
   81 
   82     // Transform dot notation to bracket notation
   83     var key = options.allowDots ? givenKey.replace(/\.([^\.\[]+)/g, '[$1]') : givenKey;
   84 
   85     // The regex chunks
   86 
   87     var parent = /^([^\[\]]*)/;
   88     var child = /(\[[^\[\]]*\])/g;
   89 
   90     // Get the parent
   91 
   92     var segment = parent.exec(key);
   93 
   94     // Stash the parent if it exists
   95 
   96     var keys = [];
   97     if (segment[1]) {
   98         // If we aren't using plain objects, optionally prefix keys
   99         // that would overwrite object prototype properties
  100         if (!options.plainObjects && has.call(Object.prototype, segment[1])) {
  101             if (!options.allowPrototypes) {
  102                 return;
  103             }
  104         }
  105 
  106         keys.push(segment[1]);
  107     }
  108 
  109     // Loop through children appending to the array until we hit depth
  110 
  111     var i = 0;
  112     while ((segment = child.exec(key)) !== null && i < options.depth) {
  113         i += 1;
  114         if (!options.plainObjects && has.call(Object.prototype, segment[1].replace(/\[|\]/g, ''))) {
  115             if (!options.allowPrototypes) {
  116                 continue;
  117             }
  118         }
  119         keys.push(segment[1]);
  120     }
  121 
  122     // If there's a remainder, just add whatever is left
  123 
  124     if (segment) {
  125         keys.push('[' + key.slice(segment.index) + ']');
  126     }
  127 
  128     return parseObject(keys, val, options);
  129 };
  130 
  131 module.exports = function (str, opts) {
  132     var options = opts || {};
  133 
  134     if (options.decoder !== null && options.decoder !== undefined && typeof options.decoder !== 'function') {
  135         throw new TypeError('Decoder has to be a function.');
  136     }
  137 
  138     options.delimiter = typeof options.delimiter === 'string' || utils.isRegExp(options.delimiter) ? options.delimiter : defaults.delimiter;
  139     options.depth = typeof options.depth === 'number' ? options.depth : defaults.depth;
  140     options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : defaults.arrayLimit;
  141     options.parseArrays = options.parseArrays !== false;
  142     options.decoder = typeof options.decoder === 'function' ? options.decoder : defaults.decoder;
  143     options.allowDots = typeof options.allowDots === 'boolean' ? options.allowDots : defaults.allowDots;
  144     options.plainObjects = typeof options.plainObjects === 'boolean' ? options.plainObjects : defaults.plainObjects;
  145     options.allowPrototypes = typeof options.allowPrototypes === 'boolean' ? options.allowPrototypes : defaults.allowPrototypes;
  146     options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : defaults.parameterLimit;
  147     options.strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling;
  148 
  149     if (str === '' || str === null || typeof str === 'undefined') {
  150         return options.plainObjects ? Object.create(null) : {};
  151     }
  152 
  153     var tempObj = typeof str === 'string' ? parseValues(str, options) : str;
  154     var obj = options.plainObjects ? Object.create(null) : {};
  155 
  156     // Iterate over the keys and setup the new object
  157 
  158     var keys = Object.keys(tempObj);
  159     for (var i = 0; i < keys.length; ++i) {
  160         var key = keys[i];
  161         var newObj = parseKeys(key, tempObj[key], options);
  162         obj = utils.merge(obj, newObj, options);
  163     }
  164 
  165     return utils.compact(obj);
  166 };