UNPKG

@gby/deep-copy

Version:

deep-copy 深拷贝,可对任意数据进行深度拷贝,包括 函数 function、正则 RegExp、Map、Set、Date、Array、URL 等等;支持含循环引用对象的拷贝,并且不会丢失成员的引用关系 和 类型信息,支持扩展,可根据数据类型定制拷贝逻辑,也可指定拷贝深度;所以,通过它可实现对任意类型的数据进行任意想要的拷贝

16 lines (13 loc) 5.15 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("type-reviver"),require("type-tls"),require("com-tools")):"function"==typeof define&&define.amd?define(["exports","type-reviver","type-tls","com-tools"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).deepCopy={},e.typeReviver,e.typeTls,e.comTools)}(this,(function(e,t,r,n){"use strict"; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */function o(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],n=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}var p=function(e){return n.copyConstructor(e)},a=[[Date,function(e){return new Date(e.getTime())}],[Array,function(e,t){return e.reduce((function(e,r,n){return e.push(void 0),t(r,(function(t){e.splice(n,1,t)}),n),e}),[])}],[Map,function(e,t){var r=new Map;return e.forEach((function(e,n){t(e,(function(e){r.set(n,e)}),n)})),r}],[Set,function(e,t){var r,n,p=new Set,a=0;try{for(var i=o(e),u=i.next();!u.done;u=i.next()){t(u.value,(function(e){p.add(e)}),a),++a}}catch(e){r={error:e}}finally{try{u&&!u.done&&(n=i.return)&&n.call(i)}finally{if(r)throw r.error}}return p}],[URL,function(e){return new URL(e.href)}],[RegExp,function(e){return new RegExp(e)}],[Function,p],["AsyncFunction",p],["GeneratorFunction",p]],i="default";function u(e){var t,p,a,l,c=e.value,y=e.typeReviverObject,f=e.allOwnProps,s=e.copyFun,v=e.maxDepth,h=e.startDepth,d=e.rawCopyMap,w=e.completeCB,O=e.key,b=e.host,m=w||function(){};if(v<h||!s&&"function"==typeof c||r.isBaseType(c))return m(c),c;var C=d.get(c);if(C)return C.then(m),C.value;(C=new n.Decide).then(m),d.set(c,C);var M=h+1;function x(e,t,r,n,o){var p=null==n?c:n;if(o)var a=o.allOwnProps,i=o.depth,l=o.copyFun;return u({value:e,typeReviverObject:y,allOwnProps:null==a?f:a,copyFun:null==l?s:l,maxDepth:v,startDepth:null==i?M:i,rawCopyMap:d,completeCB:t,key:r,host:p})}var g=r.getExactTypeNameOf(c),T=y[g];if("function"==typeof T){var j=T.call(c,c,x,{allOwnProps:f,key:O,host:b,type:g,depth:h,copyFun:s});return C.value=j,j}if("function"==typeof c.getCopy){var D=c.getCopy(c,x,{allOwnProps:f,key:O,host:b,type:g,depth:h,copyFun:s});return C.value=D,D}var P=i,F=y.default;if("function"==typeof F){var R=F.call(c,c,x,{allOwnProps:f,key:O,host:b,type:P,depth:h,copyFun:s});return C.value=R,R}if("function"==typeof c[Symbol.iterator]){var k=[],S=0,_=function(e){var t=S;k.push(void 0),u({value:e,typeReviverObject:y,allOwnProps:f,copyFun:s,maxDepth:v,startDepth:M,rawCopyMap:d,completeCB:function(e){k.splice(t,1,e)},key:S,host:c}),++S};try{for(var B=o(c),E=B.next();!E.done;E=B.next()){_(E.value)}}catch(e){t={error:e}}finally{try{E&&!E.done&&(p=B.return)&&p.call(B)}finally{if(t)throw t.error}}return C.value=k,k}var q,N=c.constructor;if("function"==typeof N)try{q=new N}catch(e){q=Object.create(N.prototype)}else q=Object.create(Object.getPrototypeOf(c));if(f)var A=Object.getOwnPropertyNames(c);else A=Object.keys(c);var L=function(e){u({value:c[e],typeReviverObject:y,allOwnProps:f,copyFun:s,maxDepth:v,startDepth:M,rawCopyMap:d,completeCB:function(t){var r=Object.getOwnPropertyDescriptor(c,e);Object.defineProperty(q,e,r),q[e]=t},key:e,host:c})};try{for(var U=o(A),z=U.next();!z.done;z=U.next()){L(z.value)}}catch(e){a={error:e}}finally{try{z&&!z.done&&(l=U.return)&&l.call(U)}finally{if(a)throw a.error}}return C.value=q,q}function l(e){function r(e,n,o){if(n)var p=n.maxDepth,a=n.allOwnProps,i=n.copyFun;var l=null==p?1/0:p,c=!!a,y=!!i,f=r.presetTypeCopierMap,s=t.mergeTypeRevivers(f.size>0?f:null,o);return u({value:e,typeReviverObject:t.toTypeReviverObject(s),allOwnProps:c,copyFun:y,maxDepth:l,startDepth:0,rawCopyMap:new Map})}return Object.defineProperty(r,"presetTypeCopierMap",{configurable:!0,enumerable:!0,get:function(){return this._presetTypeCopierMap||(this._presetTypeCopierMap=new Map),this._presetTypeCopierMap},set:function(e){e instanceof Map&&(this._presetTypeCopierMap=e)}}),e&&(r.presetTypeCopierMap=e),r}var c=new Map(a),y=l(new Map(a));e.createDeepCopy=l,e.deepCopy=y,e.default=y,e.defaultPresetTypeCopierMap=c,e.isCopyable=function(e){return e&&"function"==typeof e.getCopy},e.typeNameOfDefaultCopier=i,Object.defineProperty(e,"__esModule",{value:!0})}));