@@ -815,9 +815,14 @@ function arrayRemove(array, value) {
815
815
</file>
816
816
</example>
817
817
*/
818
- function copy ( source , destination ) {
818
+ function copy ( source , destination , maxDepth ) {
819
819
var stackSource = [ ] ;
820
820
var stackDest = [ ] ;
821
+ var currentDepth = 0 ;
822
+
823
+ if ( ! isNumber ( maxDepth ) ) {
824
+ maxDepth = NaN ;
825
+ }
821
826
822
827
if ( destination ) {
823
828
if ( isTypedArray ( destination ) || isArrayBuffer ( destination ) ) {
@@ -840,43 +845,47 @@ function copy(source, destination) {
840
845
841
846
stackSource . push ( source ) ;
842
847
stackDest . push ( destination ) ;
843
- return copyRecurse ( source , destination ) ;
848
+ return copyRecurse ( source , destination , currentDepth ) ;
844
849
}
845
850
846
- return copyElement ( source ) ;
851
+ return copyElement ( source , currentDepth ) ;
847
852
848
- function copyRecurse ( source , destination ) {
853
+ function copyRecurse ( source , destination , currentDepth ) {
854
+ currentDepth ++ ;
855
+ if ( currentDepth > maxDepth ) {
856
+ return '...' ;
857
+ }
849
858
var h = destination . $$hashKey ;
850
859
var key ;
851
860
if ( isArray ( source ) ) {
852
861
for ( var i = 0 , ii = source . length ; i < ii ; i ++ ) {
853
- destination . push ( copyElement ( source [ i ] ) ) ;
862
+ destination . push ( copyElement ( source [ i ] , currentDepth ) ) ;
854
863
}
855
864
} else if ( isBlankObject ( source ) ) {
856
865
// createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty
857
866
for ( key in source ) {
858
- destination [ key ] = copyElement ( source [ key ] ) ;
867
+ destination [ key ] = copyElement ( source [ key ] , currentDepth ) ;
859
868
}
860
869
} else if ( source && typeof source . hasOwnProperty === 'function' ) {
861
870
// Slow path, which must rely on hasOwnProperty
862
871
for ( key in source ) {
863
872
if ( source . hasOwnProperty ( key ) ) {
864
- destination [ key ] = copyElement ( source [ key ] ) ;
873
+ destination [ key ] = copyElement ( source [ key ] , currentDepth ) ;
865
874
}
866
875
}
867
876
} else {
868
877
// Slowest path --- hasOwnProperty can't be called as a method
869
878
for ( key in source ) {
870
879
if ( hasOwnProperty . call ( source , key ) ) {
871
- destination [ key ] = copyElement ( source [ key ] ) ;
880
+ destination [ key ] = copyElement ( source [ key ] , currentDepth ) ;
872
881
}
873
882
}
874
883
}
875
884
setHashKey ( destination , h ) ;
876
885
return destination ;
877
886
}
878
887
879
- function copyElement ( source ) {
888
+ function copyElement ( source , currentDepth ) {
880
889
// Simple values
881
890
if ( ! isObject ( source ) ) {
882
891
return source ;
@@ -905,7 +914,7 @@ function copy(source, destination) {
905
914
stackDest . push ( destination ) ;
906
915
907
916
return needsRecurse
908
- ? copyRecurse ( source , destination )
917
+ ? copyRecurse ( source , destination , currentDepth )
909
918
: destination ;
910
919
}
911
920
0 commit comments