"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/clone/clone.js" (8 Mar 2017, 4219 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 var clone = (function() {
    2 'use strict';
    3 
    4 /**
    5  * Clones (copies) an Object using deep copying.
    6  *
    7  * This function supports circular references by default, but if you are certain
    8  * there are no circular references in your object, you can save some CPU time
    9  * by calling clone(obj, false).
   10  *
   11  * Caution: if `circular` is false and `parent` contains circular references,
   12  * your program may enter an infinite loop and crash.
   13  *
   14  * @param `parent` - the object to be cloned
   15  * @param `circular` - set to true if the object to be cloned may contain
   16  *    circular references. (optional - true by default)
   17  * @param `depth` - set to a number if the object is only to be cloned to
   18  *    a particular depth. (optional - defaults to Infinity)
   19  * @param `prototype` - sets the prototype to be used when cloning an object.
   20  *    (optional - defaults to parent prototype).
   21 */
   22 function clone(parent, circular, depth, prototype) {
   23   var filter;
   24   if (typeof circular === 'object') {
   25     depth = circular.depth;
   26     prototype = circular.prototype;
   27     filter = circular.filter;
   28     circular = circular.circular
   29   }
   30   // maintain two arrays for circular references, where corresponding parents
   31   // and children have the same index
   32   var allParents = [];
   33   var allChildren = [];
   34 
   35   var useBuffer = typeof Buffer != 'undefined';
   36 
   37   if (typeof circular == 'undefined')
   38     circular = true;
   39 
   40   if (typeof depth == 'undefined')
   41     depth = Infinity;
   42 
   43   // recurse this function so we don't reset allParents and allChildren
   44   function _clone(parent, depth) {
   45     // cloning null always returns null
   46     if (parent === null)
   47       return null;
   48 
   49     if (depth == 0)
   50       return parent;
   51 
   52     var child;
   53     var proto;
   54     if (typeof parent != 'object') {
   55       return parent;
   56     }
   57 
   58     if (clone.__isArray(parent)) {
   59       child = [];
   60     } else if (clone.__isRegExp(parent)) {
   61       child = new RegExp(parent.source, __getRegExpFlags(parent));
   62       if (parent.lastIndex) child.lastIndex = parent.lastIndex;
   63     } else if (clone.__isDate(parent)) {
   64       child = new Date(parent.getTime());
   65     } else if (useBuffer && Buffer.isBuffer(parent)) {
   66       child = new Buffer(parent.length);
   67       parent.copy(child);
   68       return child;
   69     } else {
   70       if (typeof prototype == 'undefined') {
   71         proto = Object.getPrototypeOf(parent);
   72         child = Object.create(proto);
   73       }
   74       else {
   75         child = Object.create(prototype);
   76         proto = prototype;
   77       }
   78     }
   79 
   80     if (circular) {
   81       var index = allParents.indexOf(parent);
   82 
   83       if (index != -1) {
   84         return allChildren[index];
   85       }
   86       allParents.push(parent);
   87       allChildren.push(child);
   88     }
   89 
   90     for (var i in parent) {
   91       var attrs;
   92       if (proto) {
   93         attrs = Object.getOwnPropertyDescriptor(proto, i);
   94       }
   95 
   96       if (attrs && attrs.set == null) {
   97         continue;
   98       }
   99       child[i] = _clone(parent[i], depth - 1);
  100     }
  101 
  102     return child;
  103   }
  104 
  105   return _clone(parent, depth);
  106 }
  107 
  108 /**
  109  * Simple flat clone using prototype, accepts only objects, usefull for property
  110  * override on FLAT configuration object (no nested props).
  111  *
  112  * USE WITH CAUTION! This may not behave as you wish if you do not know how this
  113  * works.
  114  */
  115 clone.clonePrototype = function clonePrototype(parent) {
  116   if (parent === null)
  117     return null;
  118 
  119   var c = function () {};
  120   c.prototype = parent;
  121   return new c();
  122 };
  123 
  124 // private utility functions
  125 
  126 function __objToStr(o) {
  127   return Object.prototype.toString.call(o);
  128 };
  129 clone.__objToStr = __objToStr;
  130 
  131 function __isDate(o) {
  132   return typeof o === 'object' && __objToStr(o) === '[object Date]';
  133 };
  134 clone.__isDate = __isDate;
  135 
  136 function __isArray(o) {
  137   return typeof o === 'object' && __objToStr(o) === '[object Array]';
  138 };
  139 clone.__isArray = __isArray;
  140 
  141 function __isRegExp(o) {
  142   return typeof o === 'object' && __objToStr(o) === '[object RegExp]';
  143 };
  144 clone.__isRegExp = __isRegExp;
  145 
  146 function __getRegExpFlags(re) {
  147   var flags = '';
  148   if (re.global) flags += 'g';
  149   if (re.ignoreCase) flags += 'i';
  150   if (re.multiline) flags += 'm';
  151   return flags;
  152 };
  153 clone.__getRegExpFlags = __getRegExpFlags;
  154 
  155 return clone;
  156 })();
  157 
  158 if (typeof module === 'object' && module.exports) {
  159   module.exports = clone;
  160 }