10
10
splice,
11
11
push,
12
12
toString,
13
+ objectMaxDepthInErrorMessage,
14
+ configureErrorHandling,
13
15
ngMinErr,
14
16
angularModule,
15
17
uid,
@@ -125,6 +127,30 @@ var VALIDITY_STATE_PROPERTY = 'validity';
125
127
126
128
var hasOwnProperty = Object . prototype . hasOwnProperty ;
127
129
130
+ var objectMaxDepthInErrorMessage = 5 ;
131
+
132
+ /**
133
+ * @ngdoc function
134
+ * @name angular.configureErrorHandling
135
+ * @module ng
136
+ * @kind function
137
+ *
138
+ * @description Use this function to configure minErr
139
+ *
140
+ * @param {Object= } config an object for defining configuration for error handling. The
141
+ * following keys are supported:
142
+ *
143
+ * * `objectMaxDepth` - used to stringify objects in error messages until reaching the max depth.
144
+ *
145
+ */
146
+ function configureErrorHandling ( config ) {
147
+ if ( isObject ( config ) ) {
148
+ if ( isNumber ( config . objectMaxDepth ) && config . objectMaxDepth > 0 ) {
149
+ objectMaxDepthInErrorMessage = config . objectMaxDepth ;
150
+ }
151
+ }
152
+ }
153
+
128
154
/**
129
155
* @ngdoc function
130
156
* @name angular.lowercase
@@ -796,6 +822,7 @@ function arrayRemove(array, value) {
796
822
* are deleted and then all elements/properties from the source are copied to it.
797
823
* * If `source` is not an object or array (inc. `null` and `undefined`), `source` is returned.
798
824
* * If `source` is identical to `destination` an exception will be thrown.
825
+ * * If `maxDepth` is supplied, all properties of the source will be copied until reaching the max depth.
799
826
*
800
827
* <br />
801
828
* <div class="alert alert-warning">
@@ -807,6 +834,7 @@ function arrayRemove(array, value) {
807
834
* Can be any type, including primitives, `null`, and `undefined`.
808
835
* @param {(Object|Array)= } destination Destination into which the source is copied. If
809
836
* provided, must be of the same type as `source`.
837
+ * @param {Number= } maxDepth All properties of the source will be copied until reaching the max depth.
810
838
* @returns {* } The copy or updated `destination`, if `destination` was specified.
811
839
*
812
840
* @example
@@ -847,10 +875,14 @@ function arrayRemove(array, value) {
847
875
</file>
848
876
</example>
849
877
*/
850
- function copy ( source , destination ) {
878
+ function copy ( source , destination , maxDepth ) {
851
879
var stackSource = [ ] ;
852
880
var stackDest = [ ] ;
853
881
882
+ if ( ! isNumber ( maxDepth ) || maxDepth < 1 ) {
883
+ maxDepth = NaN ;
884
+ }
885
+
854
886
if ( destination ) {
855
887
if ( isTypedArray ( destination ) || isArrayBuffer ( destination ) ) {
856
888
throw ngMinErr ( 'cpta' , 'Can\'t copy! TypedArray destination cannot be mutated.' ) ;
@@ -872,43 +904,47 @@ function copy(source, destination) {
872
904
873
905
stackSource . push ( source ) ;
874
906
stackDest . push ( destination ) ;
875
- return copyRecurse ( source , destination ) ;
907
+ return copyRecurse ( source , destination , maxDepth ) ;
876
908
}
877
909
878
- return copyElement ( source ) ;
910
+ return copyElement ( source , maxDepth ) ;
879
911
880
- function copyRecurse ( source , destination ) {
912
+ function copyRecurse ( source , destination , maxDepth ) {
913
+ maxDepth -- ;
914
+ if ( maxDepth < 0 ) {
915
+ return '...' ;
916
+ }
881
917
var h = destination . $$hashKey ;
882
918
var key ;
883
919
if ( isArray ( source ) ) {
884
920
for ( var i = 0 , ii = source . length ; i < ii ; i ++ ) {
885
- destination . push ( copyElement ( source [ i ] ) ) ;
921
+ destination . push ( copyElement ( source [ i ] , maxDepth ) ) ;
886
922
}
887
923
} else if ( isBlankObject ( source ) ) {
888
924
// createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty
889
925
for ( key in source ) {
890
- destination [ key ] = copyElement ( source [ key ] ) ;
926
+ destination [ key ] = copyElement ( source [ key ] , maxDepth ) ;
891
927
}
892
928
} else if ( source && typeof source . hasOwnProperty === 'function' ) {
893
929
// Slow path, which must rely on hasOwnProperty
894
930
for ( key in source ) {
895
931
if ( source . hasOwnProperty ( key ) ) {
896
- destination [ key ] = copyElement ( source [ key ] ) ;
932
+ destination [ key ] = copyElement ( source [ key ] , maxDepth ) ;
897
933
}
898
934
}
899
935
} else {
900
936
// Slowest path --- hasOwnProperty can't be called as a method
901
937
for ( key in source ) {
902
938
if ( hasOwnProperty . call ( source , key ) ) {
903
- destination [ key ] = copyElement ( source [ key ] ) ;
939
+ destination [ key ] = copyElement ( source [ key ] , maxDepth ) ;
904
940
}
905
941
}
906
942
}
907
943
setHashKey ( destination , h ) ;
908
944
return destination ;
909
945
}
910
946
911
- function copyElement ( source ) {
947
+ function copyElement ( source , maxDepth ) {
912
948
// Simple values
913
949
if ( ! isObject ( source ) ) {
914
950
return source ;
@@ -937,7 +973,7 @@ function copy(source, destination) {
937
973
stackDest . push ( destination ) ;
938
974
939
975
return needsRecurse
940
- ? copyRecurse ( source , destination )
976
+ ? copyRecurse ( source , destination , maxDepth )
941
977
: destination ;
942
978
}
943
979
0 commit comments