1+ 'use strict' ;
2+
3+ var _typeof = typeof Symbol === "function" && typeof Symbol . iterator === "symbol" ? function ( obj ) { return typeof obj ; } : function ( obj ) { return obj && typeof Symbol === "function" && obj . constructor === Symbol && obj !== Symbol . prototype ? "symbol" : typeof obj ; } ;
4+
5+ var clone = function ( ) {
6+ 'use strict' ;
7+
8+ function _instanceof ( obj , type ) {
9+ return type != null && obj instanceof type ;
10+ }
11+
12+ var nativeMap ;
13+ try {
14+ nativeMap = Map ;
15+ } catch ( _ ) {
16+ nativeMap = function nativeMap ( ) { } ;
17+ }
18+
19+ var nativeSet ;
20+ try {
21+ nativeSet = Set ;
22+ } catch ( _ ) {
23+ nativeSet = function nativeSet ( ) { } ;
24+ }
25+
26+ var nativePromise ;
27+ try {
28+ nativePromise = Promise ;
29+ } catch ( _ ) {
30+ nativePromise = function nativePromise ( ) { } ;
31+ }
32+
33+ function clone ( parent , circular , depth , prototype , includeNonEnumerable ) {
34+ if ( ( typeof circular === 'undefined' ? 'undefined' : _typeof ( circular ) ) === 'object' ) {
35+ depth = circular . depth ;
36+ prototype = circular . prototype ;
37+ includeNonEnumerable = circular . includeNonEnumerable ;
38+ circular = circular . circular ;
39+ }
40+
41+ var allParents = [ ] ;
42+ var allChildren = [ ] ;
43+
44+ var useBuffer = typeof Buffer != 'undefined' ;
45+
46+ if ( typeof circular == 'undefined' ) circular = true ;
47+
48+ if ( typeof depth == 'undefined' ) depth = Infinity ;
49+
50+ function _clone ( parent , depth ) {
51+ if ( parent === null ) return null ;
52+
53+ if ( depth === 0 ) return parent ;
54+
55+ var child ;
56+ var proto ;
57+ if ( ( typeof parent === 'undefined' ? 'undefined' : _typeof ( parent ) ) != 'object' ) {
58+ return parent ;
59+ }
60+
61+ if ( _instanceof ( parent , nativeMap ) ) {
62+ child = new nativeMap ( ) ;
63+ } else if ( _instanceof ( parent , nativeSet ) ) {
64+ child = new nativeSet ( ) ;
65+ } else if ( _instanceof ( parent , nativePromise ) ) {
66+ child = new nativePromise ( function ( resolve , reject ) {
67+ parent . then ( function ( value ) {
68+ resolve ( _clone ( value , depth - 1 ) ) ;
69+ } , function ( err ) {
70+ reject ( _clone ( err , depth - 1 ) ) ;
71+ } ) ;
72+ } ) ;
73+ } else if ( clone . __isArray ( parent ) ) {
74+ child = [ ] ;
75+ } else if ( clone . __isRegExp ( parent ) ) {
76+ child = new RegExp ( parent . source , __getRegExpFlags ( parent ) ) ;
77+ if ( parent . lastIndex ) child . lastIndex = parent . lastIndex ;
78+ } else if ( clone . __isDate ( parent ) ) {
79+ child = new Date ( parent . getTime ( ) ) ;
80+ } else if ( useBuffer && Buffer . isBuffer ( parent ) ) {
81+ child = new Buffer ( parent . length ) ;
82+ parent . copy ( child ) ;
83+ return child ;
84+ } else if ( _instanceof ( parent , Error ) ) {
85+ child = Object . create ( parent ) ;
86+ } else {
87+ if ( typeof prototype == 'undefined' ) {
88+ proto = Object . getPrototypeOf ( parent ) ;
89+ child = Object . create ( proto ) ;
90+ } else {
91+ child = Object . create ( prototype ) ;
92+ proto = prototype ;
93+ }
94+ }
95+
96+ if ( circular ) {
97+ var index = allParents . indexOf ( parent ) ;
98+
99+ if ( index != - 1 ) {
100+ return allChildren [ index ] ;
101+ }
102+ allParents . push ( parent ) ;
103+ allChildren . push ( child ) ;
104+ }
105+
106+ if ( _instanceof ( parent , nativeMap ) ) {
107+ parent . forEach ( function ( value , key ) {
108+ var keyChild = _clone ( key , depth - 1 ) ;
109+ var valueChild = _clone ( value , depth - 1 ) ;
110+ child . set ( keyChild , valueChild ) ;
111+ } ) ;
112+ }
113+ if ( _instanceof ( parent , nativeSet ) ) {
114+ parent . forEach ( function ( value ) {
115+ var entryChild = _clone ( value , depth - 1 ) ;
116+ child . add ( entryChild ) ;
117+ } ) ;
118+ }
119+
120+ for ( var i in parent ) {
121+ var attrs ;
122+ if ( proto ) {
123+ attrs = Object . getOwnPropertyDescriptor ( proto , i ) ;
124+ }
125+
126+ if ( attrs && attrs . set == null ) {
127+ continue ;
128+ }
129+ child [ i ] = _clone ( parent [ i ] , depth - 1 ) ;
130+ }
131+
132+ if ( Object . getOwnPropertySymbols ) {
133+ var symbols = Object . getOwnPropertySymbols ( parent ) ;
134+ for ( var i = 0 ; i < symbols . length ; i ++ ) {
135+ var symbol = symbols [ i ] ;
136+ var descriptor = Object . getOwnPropertyDescriptor ( parent , symbol ) ;
137+ if ( descriptor && ! descriptor . enumerable && ! includeNonEnumerable ) {
138+ continue ;
139+ }
140+ child [ symbol ] = _clone ( parent [ symbol ] , depth - 1 ) ;
141+ if ( ! descriptor . enumerable ) {
142+ Object . defineProperty ( child , symbol , {
143+ enumerable : false
144+ } ) ;
145+ }
146+ }
147+ }
148+
149+ if ( includeNonEnumerable ) {
150+ var allPropertyNames = Object . getOwnPropertyNames ( parent ) ;
151+ for ( var i = 0 ; i < allPropertyNames . length ; i ++ ) {
152+ var propertyName = allPropertyNames [ i ] ;
153+ var descriptor = Object . getOwnPropertyDescriptor ( parent , propertyName ) ;
154+ if ( descriptor && descriptor . enumerable ) {
155+ continue ;
156+ }
157+ child [ propertyName ] = _clone ( parent [ propertyName ] , depth - 1 ) ;
158+ Object . defineProperty ( child , propertyName , {
159+ enumerable : false
160+ } ) ;
161+ }
162+ }
163+
164+ return child ;
165+ }
166+
167+ return _clone ( parent , depth ) ;
168+ }
169+
170+ clone . clonePrototype = function clonePrototype ( parent ) {
171+ if ( parent === null ) return null ;
172+
173+ var c = function c ( ) { } ;
174+ c . prototype = parent ;
175+ return new c ( ) ;
176+ } ;
177+
178+ function __objToStr ( o ) {
179+ return Object . prototype . toString . call ( o ) ;
180+ }
181+ clone . __objToStr = __objToStr ;
182+
183+ function __isDate ( o ) {
184+ return ( typeof o === 'undefined' ? 'undefined' : _typeof ( o ) ) === 'object' && __objToStr ( o ) === '[object Date]' ;
185+ }
186+ clone . __isDate = __isDate ;
187+
188+ function __isArray ( o ) {
189+ return ( typeof o === 'undefined' ? 'undefined' : _typeof ( o ) ) === 'object' && __objToStr ( o ) === '[object Array]' ;
190+ }
191+ clone . __isArray = __isArray ;
192+
193+ function __isRegExp ( o ) {
194+ return ( typeof o === 'undefined' ? 'undefined' : _typeof ( o ) ) === 'object' && __objToStr ( o ) === '[object RegExp]' ;
195+ }
196+ clone . __isRegExp = __isRegExp ;
197+
198+ function __getRegExpFlags ( re ) {
199+ var flags = '' ;
200+ if ( re . global ) flags += 'g' ;
201+ if ( re . ignoreCase ) flags += 'i' ;
202+ if ( re . multiline ) flags += 'm' ;
203+ return flags ;
204+ }
205+ clone . __getRegExpFlags = __getRegExpFlags ;
206+
207+ return clone ;
208+ } ( ) ;
209+
210+ if ( ( typeof module === 'undefined' ? 'undefined' : _typeof ( module ) ) === 'object' && module . exports ) {
211+ module . exports = clone ;
212+ }
0 commit comments