1 var clone = (function() {
5 * Clones (copies) an Object using deep copying.
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).
11 * Caution: if `circular` is false and `parent` contains circular references,
12 * your program may enter an infinite loop and crash.
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).
22 function clone(parent, circular, depth, prototype) {
24 if (typeof circular === 'object') {
25 depth = circular.depth;
26 prototype = circular.prototype;
27 filter = circular.filter;
28 circular = circular.circular
30 // maintain two arrays for circular references, where corresponding parents
31 // and children have the same index
35 var useBuffer = typeof Buffer != 'undefined';
37 if (typeof circular == 'undefined')
40 if (typeof depth == 'undefined')
43 // recurse this function so we don't reset allParents and allChildren
44 function _clone(parent, depth) {
45 // cloning null always returns null
54 if (typeof parent != 'object') {
58 if (clone.__isArray(parent)) {
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);
70 if (typeof prototype == 'undefined') {
71 proto = Object.getPrototypeOf(parent);
72 child = Object.create(proto);
75 child = Object.create(prototype);
81 var index = allParents.indexOf(parent);
84 return allChildren[index];
86 allParents.push(parent);
87 allChildren.push(child);
90 for (var i in parent) {
93 attrs = Object.getOwnPropertyDescriptor(proto, i);
96 if (attrs && attrs.set == null) {
99 child[i] = _clone(parent[i], depth - 1);
105 return _clone(parent, depth);
109 * Simple flat clone using prototype, accepts only objects, usefull for property
110 * override on FLAT configuration object (no nested props).
112 * USE WITH CAUTION! This may not behave as you wish if you do not know how this
115 clone.clonePrototype = function clonePrototype(parent) {
119 var c = function () {};
120 c.prototype = parent;
124 // private utility functions
126 function __objToStr(o) {
127 return Object.prototype.toString.call(o);
129 clone.__objToStr = __objToStr;
131 function __isDate(o) {
132 return typeof o === 'object' && __objToStr(o) === '[object Date]';
134 clone.__isDate = __isDate;
136 function __isArray(o) {
137 return typeof o === 'object' && __objToStr(o) === '[object Array]';
139 clone.__isArray = __isArray;
141 function __isRegExp(o) {
142 return typeof o === 'object' && __objToStr(o) === '[object RegExp]';
144 clone.__isRegExp = __isRegExp;
146 function __getRegExpFlags(re) {
148 if (re.global) flags += 'g';
149 if (re.ignoreCase) flags += 'i';
150 if (re.multiline) flags += 'm';
153 clone.__getRegExpFlags = __getRegExpFlags;
158 if (typeof module === 'object' && module.exports) {
159 module.exports = clone;