-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeep-copy-min.js
2 lines (2 loc) · 2.55 KB
/
deep-copy-min.js
1
2
/* Copyright 2015 Oran Looney. https://github.com/olooney/deep-copy-js/blob/master/LICENSE.txt */
owl=function(){function a(){}function b(b){return"object"==typeof b?(a.prototype=b,new a):b}function c(a){if("object"!=typeof a)return a;var c=a.valueOf();if(a!=c)return new a.constructor(c);if(a instanceof a.constructor&&a.constructor!==Object){var d=b(a.constructor.prototype);for(var e in a)a.hasOwnProperty(e)&&(d[e]=a[e])}else{var d={};for(var e in a)d[e]=a[e]}return d}function e(a){for(var b in a)this[b]=a[b]}function f(){this.copiedObjects=[],thisPass=this,this.recursiveDeepCopy=function(a){return thisPass.deepCopy(a)},this.depth=0}function g(a,b){var c=new f;return b&&(c.maxDepth=b),c.deepCopy(a)}function h(a){return window.Node?a instanceof Node:a===document?!0:"number"==typeof a.nodeType&&a.attributes&&a.childNodes&&a.cloneNode}var d=[];return e.prototype={constructor:e,canCopy:function(){return!1},create:function(){},populate:function(){}},f.prototype={constructor:f,maxDepth:256,cacheResult:function(a,b){this.copiedObjects.push([a,b])},getCachedResult:function(a){for(var b=this.copiedObjects,c=b.length,d=0;c>d;d++)if(b[d][0]===a)return b[d][1];return void 0},deepCopy:function(a){if(null===a)return null;if("object"!=typeof a)return a;var b=this.getCachedResult(a);if(b)return b;for(var c=0;c<d.length;c++){var e=d[c];if(e.canCopy(a))return this.applyDeepCopier(e,a)}throw new Error("no DeepCopier is able to copy "+a)},applyDeepCopier:function(a,b){var c=a.create(b);if(this.cacheResult(b,c),this.depth++,this.depth>this.maxDepth)throw new Error("Exceeded max recursion depth in deep copy.");return a.populate(this.recursiveDeepCopy,b,c),this.depth--,c}},g.DeepCopier=e,g.deepCopiers=d,g.register=function(a){a instanceof e||(a=new e(a)),d.unshift(a)},g.register({canCopy:function(){return!0},create:function(a){return a instanceof a.constructor?b(a.constructor.prototype):{}},populate:function(a,b,c){for(var d in b)b.hasOwnProperty(d)&&(c[d]=a(b[d]));return c}}),g.register({canCopy:function(a){return a instanceof Array},create:function(a){return new a.constructor},populate:function(a,b,c){for(var d=0;d<b.length;d++)c.push(a(b[d]));return c}}),g.register({canCopy:function(a){return a instanceof Date},create:function(a){return new Date(a)}}),g.register({canCopy:function(a){return h(a)},create:function(a){return a===document?document:a.cloneNode(!1)},populate:function(a,b,c){if(b===document)return document;if(b.childNodes&&b.childNodes.length)for(var d=0;d<b.childNodes.length;d++){var e=a(b.childNodes[d]);c.appendChild(e)}}}),{DeepCopyAlgorithm:f,copy:c,clone:b,deepCopy:g}}();