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