@@ -815,9 +815,13 @@ function arrayRemove(array, value) {
815815 </file>
816816 </example>
817817 */
818- function copy ( source , destination ) {
818+ function copy ( source , destination , maxDepth ) {
819819 var stackSource = [ ] ;
820820 var stackDest = [ ] ;
821+ var currentDepth = 0 ;
822+ if ( ! isNumber ( maxDepth ) ) {
823+ maxDepth = NaN ;
824+ }
821825
822826 if ( destination ) {
823827 if ( isTypedArray ( destination ) || isArrayBuffer ( destination ) ) {
@@ -840,43 +844,47 @@ function copy(source, destination) {
840844
841845 stackSource . push ( source ) ;
842846 stackDest . push ( destination ) ;
843- return copyRecurse ( source , destination ) ;
847+ return copyRecurse ( source , destination , currentDepth ) ;
844848 }
845849
846- return copyElement ( source ) ;
850+ return copyElement ( source , currentDepth ) ;
847851
848- function copyRecurse ( source , destination ) {
852+ function copyRecurse ( source , destination , currentDepth ) {
853+ currentDepth ++ ;
854+ if ( currentDepth > maxDepth ) {
855+ return '...' ;
856+ }
849857 var h = destination . $$hashKey ;
850858 var key ;
851859 if ( isArray ( source ) ) {
852860 for ( var i = 0 , ii = source . length ; i < ii ; i ++ ) {
853- destination . push ( copyElement ( source [ i ] ) ) ;
861+ destination . push ( copyElement ( source [ i ] , currentDepth ) ) ;
854862 }
855863 } else if ( isBlankObject ( source ) ) {
856864 // createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty
857865 for ( key in source ) {
858- destination [ key ] = copyElement ( source [ key ] ) ;
866+ destination [ key ] = copyElement ( source [ key ] , currentDepth ) ;
859867 }
860868 } else if ( source && typeof source . hasOwnProperty === 'function' ) {
861869 // Slow path, which must rely on hasOwnProperty
862870 for ( key in source ) {
863871 if ( source . hasOwnProperty ( key ) ) {
864- destination [ key ] = copyElement ( source [ key ] ) ;
872+ destination [ key ] = copyElement ( source [ key ] , currentDepth ) ;
865873 }
866874 }
867875 } else {
868876 // Slowest path --- hasOwnProperty can't be called as a method
869877 for ( key in source ) {
870878 if ( hasOwnProperty . call ( source , key ) ) {
871- destination [ key ] = copyElement ( source [ key ] ) ;
879+ destination [ key ] = copyElement ( source [ key ] , currentDepth ) ;
872880 }
873881 }
874882 }
875883 setHashKey ( destination , h ) ;
876884 return destination ;
877885 }
878886
879- function copyElement ( source ) {
887+ function copyElement ( source , currentDepth ) {
880888 // Simple values
881889 if ( ! isObject ( source ) ) {
882890 return source ;
@@ -905,7 +913,7 @@ function copy(source, destination) {
905913 stackDest . push ( destination ) ;
906914
907915 return needsRecurse
908- ? copyRecurse ( source , destination )
916+ ? copyRecurse ( source , destination , currentDepth )
909917 : destination ;
910918 }
911919
0 commit comments