"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/qs/lib/utils.js" (11 Apr 2017, 4891 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 has = Object.prototype.hasOwnProperty;
    4 
    5 var hexTable = (function () {
    6     var array = [];
    7     for (var i = 0; i < 256; ++i) {
    8         array.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase());
    9     }
   10 
   11     return array;
   12 }());
   13 
   14 exports.arrayToObject = function (source, options) {
   15     var obj = options && options.plainObjects ? Object.create(null) : {};
   16     for (var i = 0; i < source.length; ++i) {
   17         if (typeof source[i] !== 'undefined') {
   18             obj[i] = source[i];
   19         }
   20     }
   21 
   22     return obj;
   23 };
   24 
   25 exports.merge = function (target, source, options) {
   26     if (!source) {
   27         return target;
   28     }
   29 
   30     if (typeof source !== 'object') {
   31         if (Array.isArray(target)) {
   32             target.push(source);
   33         } else if (typeof target === 'object') {
   34             if (options.plainObjects || options.allowPrototypes || !has.call(Object.prototype, source)) {
   35                 target[source] = true;
   36             }
   37         } else {
   38             return [target, source];
   39         }
   40 
   41         return target;
   42     }
   43 
   44     if (typeof target !== 'object') {
   45         return [target].concat(source);
   46     }
   47 
   48     var mergeTarget = target;
   49     if (Array.isArray(target) && !Array.isArray(source)) {
   50         mergeTarget = exports.arrayToObject(target, options);
   51     }
   52 
   53     if (Array.isArray(target) && Array.isArray(source)) {
   54         source.forEach(function (item, i) {
   55             if (has.call(target, i)) {
   56                 if (target[i] && typeof target[i] === 'object') {
   57                     target[i] = exports.merge(target[i], item, options);
   58                 } else {
   59                     target.push(item);
   60                 }
   61             } else {
   62                 target[i] = item;
   63             }
   64         });
   65         return target;
   66     }
   67 
   68     return Object.keys(source).reduce(function (acc, key) {
   69         var value = source[key];
   70 
   71         if (Object.prototype.hasOwnProperty.call(acc, key)) {
   72             acc[key] = exports.merge(acc[key], value, options);
   73         } else {
   74             acc[key] = value;
   75         }
   76         return acc;
   77     }, mergeTarget);
   78 };
   79 
   80 exports.decode = function (str) {
   81     try {
   82         return decodeURIComponent(str.replace(/\+/g, ' '));
   83     } catch (e) {
   84         return str;
   85     }
   86 };
   87 
   88 exports.encode = function (str) {
   89     // This code was originally written by Brian White (mscdex) for the io.js core querystring library.
   90     // It has been adapted here for stricter adherence to RFC 3986
   91     if (str.length === 0) {
   92         return str;
   93     }
   94 
   95     var string = typeof str === 'string' ? str : String(str);
   96 
   97     var out = '';
   98     for (var i = 0; i < string.length; ++i) {
   99         var c = string.charCodeAt(i);
  100 
  101         if (
  102             c === 0x2D || // -
  103             c === 0x2E || // .
  104             c === 0x5F || // _
  105             c === 0x7E || // ~
  106             (c >= 0x30 && c <= 0x39) || // 0-9
  107             (c >= 0x41 && c <= 0x5A) || // a-z
  108             (c >= 0x61 && c <= 0x7A) // A-Z
  109         ) {
  110             out += string.charAt(i);
  111             continue;
  112         }
  113 
  114         if (c < 0x80) {
  115             out = out + hexTable[c];
  116             continue;
  117         }
  118 
  119         if (c < 0x800) {
  120             out = out + (hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)]);
  121             continue;
  122         }
  123 
  124         if (c < 0xD800 || c >= 0xE000) {
  125             out = out + (hexTable[0xE0 | (c >> 12)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]);
  126             continue;
  127         }
  128 
  129         i += 1;
  130         c = 0x10000 + (((c & 0x3FF) << 10) | (string.charCodeAt(i) & 0x3FF));
  131         out += hexTable[0xF0 | (c >> 18)] + hexTable[0x80 | ((c >> 12) & 0x3F)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]; // eslint-disable-line max-len
  132     }
  133 
  134     return out;
  135 };
  136 
  137 exports.compact = function (obj, references) {
  138     if (typeof obj !== 'object' || obj === null) {
  139         return obj;
  140     }
  141 
  142     var refs = references || [];
  143     var lookup = refs.indexOf(obj);
  144     if (lookup !== -1) {
  145         return refs[lookup];
  146     }
  147 
  148     refs.push(obj);
  149 
  150     if (Array.isArray(obj)) {
  151         var compacted = [];
  152 
  153         for (var i = 0; i < obj.length; ++i) {
  154             if (obj[i] && typeof obj[i] === 'object') {
  155                 compacted.push(exports.compact(obj[i], refs));
  156             } else if (typeof obj[i] !== 'undefined') {
  157                 compacted.push(obj[i]);
  158             }
  159         }
  160 
  161         return compacted;
  162     }
  163 
  164     var keys = Object.keys(obj);
  165     keys.forEach(function (key) {
  166         obj[key] = exports.compact(obj[key], refs);
  167     });
  168 
  169     return obj;
  170 };
  171 
  172 exports.isRegExp = function (obj) {
  173     return Object.prototype.toString.call(obj) === '[object RegExp]';
  174 };
  175 
  176 exports.isBuffer = function (obj) {
  177     if (obj === null || typeof obj === 'undefined') {
  178         return false;
  179     }
  180 
  181     return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
  182 };