"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/qs/lib/parse.js" (11 Apr 2017, 5492 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 '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 parseQueryStringValues(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 parseObjectRecursive(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.charAt(0) === '[' && root.charAt(root.length - 1) === ']' ? root.slice(1, -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 parseQueryStringKeys(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 brackets = /(\[[^[\]]*])/;
   88     var child = /(\[[^[\]]*])/g;
   89 
   90     // Get the parent
   91 
   92     var segment = brackets.exec(key);
   93     var parent = segment ? key.slice(0, segment.index) : key;
   94 
   95     // Stash the parent if it exists
   96 
   97     var keys = [];
   98     if (parent) {
   99         // If we aren't using plain objects, optionally prefix keys
  100         // that would overwrite object prototype properties
  101         if (!options.plainObjects && has.call(Object.prototype, parent)) {
  102             if (!options.allowPrototypes) {
  103                 return;
  104             }
  105         }
  106 
  107         keys.push(parent);
  108     }
  109 
  110     // Loop through children appending to the array until we hit depth
  111 
  112     var i = 0;
  113     while ((segment = child.exec(key)) !== null && i < options.depth) {
  114         i += 1;
  115         if (!options.plainObjects && has.call(Object.prototype, segment[1].slice(1, -1))) {
  116             if (!options.allowPrototypes) {
  117                 return;
  118             }
  119         }
  120         keys.push(segment[1]);
  121     }
  122 
  123     // If there's a remainder, just add whatever is left
  124 
  125     if (segment) {
  126         keys.push('[' + key.slice(segment.index) + ']');
  127     }
  128 
  129     return parseObject(keys, val, options);
  130 };
  131 
  132 module.exports = function (str, opts) {
  133     var options = opts || {};
  134 
  135     if (options.decoder !== null && options.decoder !== undefined && typeof options.decoder !== 'function') {
  136         throw new TypeError('Decoder has to be a function.');
  137     }
  138 
  139     options.delimiter = typeof options.delimiter === 'string' || utils.isRegExp(options.delimiter) ? options.delimiter : defaults.delimiter;
  140     options.depth = typeof options.depth === 'number' ? options.depth : defaults.depth;
  141     options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : defaults.arrayLimit;
  142     options.parseArrays = options.parseArrays !== false;
  143     options.decoder = typeof options.decoder === 'function' ? options.decoder : defaults.decoder;
  144     options.allowDots = typeof options.allowDots === 'boolean' ? options.allowDots : defaults.allowDots;
  145     options.plainObjects = typeof options.plainObjects === 'boolean' ? options.plainObjects : defaults.plainObjects;
  146     options.allowPrototypes = typeof options.allowPrototypes === 'boolean' ? options.allowPrototypes : defaults.allowPrototypes;
  147     options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : defaults.parameterLimit;
  148     options.strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling;
  149 
  150     if (str === '' || str === null || typeof str === 'undefined') {
  151         return options.plainObjects ? Object.create(null) : {};
  152     }
  153 
  154     var tempObj = typeof str === 'string' ? parseValues(str, options) : str;
  155     var obj = options.plainObjects ? Object.create(null) : {};
  156 
  157     // Iterate over the keys and setup the new object
  158 
  159     var keys = Object.keys(tempObj);
  160     for (var i = 0; i < keys.length; ++i) {
  161         var key = keys[i];
  162         var newObj = parseKeys(key, tempObj[key], options);
  163         obj = utils.merge(obj, newObj, options);
  164     }
  165 
  166     return utils.compact(obj);
  167 };