@@ -796,6 +796,7 @@ function arrayRemove(array, value) {
796
796
* are deleted and then all elements/properties from the source are copied to it.
797
797
* * If `source` is not an object or array (inc. `null` and `undefined`), `source` is returned.
798
798
* * If `source` is identical to `destination` an exception will be thrown.
799
+ * * If `maxDepth`is supplied, all properties of the source will be copied until reaching the maxDepth.
799
800
*
800
801
* <br />
801
802
* <div class="alert alert-warning">
@@ -807,6 +808,7 @@ function arrayRemove(array, value) {
807
808
* Can be any type, including primitives, `null`, and `undefined`.
808
809
* @param {(Object|Array)= } destination Destination into which the source is copied. If
809
810
* provided, must be of the same type as `source`.
811
+ * @param {Number } maxDepth all properties of the source will be copied until reaching the maxDepth.
810
812
* @returns {* } The copy or updated `destination`, if `destination` was specified.
811
813
*
812
814
* @example
@@ -847,9 +849,13 @@ function arrayRemove(array, value) {
847
849
</file>
848
850
</example>
849
851
*/
850
- function copy ( source , destination ) {
852
+ function copy ( source , destination , maxDepth ) {
851
853
var stackSource = [ ] ;
852
854
var stackDest = [ ] ;
855
+ var currentDepth = 0 ;
856
+ if ( ! isNumber ( maxDepth ) ) {
857
+ maxDepth = NaN ;
858
+ }
853
859
854
860
if ( destination ) {
855
861
if ( isTypedArray ( destination ) || isArrayBuffer ( destination ) ) {
@@ -872,43 +878,47 @@ function copy(source, destination) {
872
878
873
879
stackSource . push ( source ) ;
874
880
stackDest . push ( destination ) ;
875
- return copyRecurse ( source , destination ) ;
881
+ return copyRecurse ( source , destination , currentDepth ) ;
876
882
}
877
883
878
- return copyElement ( source ) ;
884
+ return copyElement ( source , currentDepth ) ;
879
885
880
- function copyRecurse ( source , destination ) {
886
+ function copyRecurse ( source , destination , currentDepth ) {
887
+ currentDepth ++ ;
888
+ if ( currentDepth > maxDepth ) {
889
+ return '...' ;
890
+ }
881
891
var h = destination . $$hashKey ;
882
892
var key ;
883
893
if ( isArray ( source ) ) {
884
894
for ( var i = 0 , ii = source . length ; i < ii ; i ++ ) {
885
- destination . push ( copyElement ( source [ i ] ) ) ;
895
+ destination . push ( copyElement ( source [ i ] , currentDepth ) ) ;
886
896
}
887
897
} else if ( isBlankObject ( source ) ) {
888
898
// createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty
889
899
for ( key in source ) {
890
- destination [ key ] = copyElement ( source [ key ] ) ;
900
+ destination [ key ] = copyElement ( source [ key ] , currentDepth ) ;
891
901
}
892
902
} else if ( source && typeof source . hasOwnProperty === 'function' ) {
893
903
// Slow path, which must rely on hasOwnProperty
894
904
for ( key in source ) {
895
905
if ( source . hasOwnProperty ( key ) ) {
896
- destination [ key ] = copyElement ( source [ key ] ) ;
906
+ destination [ key ] = copyElement ( source [ key ] , currentDepth ) ;
897
907
}
898
908
}
899
909
} else {
900
910
// Slowest path --- hasOwnProperty can't be called as a method
901
911
for ( key in source ) {
902
912
if ( hasOwnProperty . call ( source , key ) ) {
903
- destination [ key ] = copyElement ( source [ key ] ) ;
913
+ destination [ key ] = copyElement ( source [ key ] , currentDepth ) ;
904
914
}
905
915
}
906
916
}
907
917
setHashKey ( destination , h ) ;
908
918
return destination ;
909
919
}
910
920
911
- function copyElement ( source ) {
921
+ function copyElement ( source , currentDepth ) {
912
922
// Simple values
913
923
if ( ! isObject ( source ) ) {
914
924
return source ;
@@ -937,7 +947,7 @@ function copy(source, destination) {
937
947
stackDest . push ( destination ) ;
938
948
939
949
return needsRecurse
940
- ? copyRecurse ( source , destination )
950
+ ? copyRecurse ( source , destination , currentDepth )
941
951
: destination ;
942
952
}
943
953
0 commit comments