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