* @ngdoc function * @name angular.copy * @module ng * @kind function * * @description * Creates a deep copy of `source`, which should be an object or an array. This functions is used * internally, mostly in the change-detection code. It is not intended as an all-pu
(source, destination, maxDepth)
| 1068 | </example> |
| 1069 | */ |
| 1070 | function copy(source, destination, maxDepth) { |
| 1071 | var stackSource = []; |
| 1072 | var stackDest = []; |
| 1073 | maxDepth = isValidObjectMaxDepth(maxDepth) ? maxDepth : NaN; |
| 1074 | |
| 1075 | if (destination) { |
| 1076 | if (isTypedArray(destination) || isArrayBuffer(destination)) { |
| 1077 | throw ngMinErr('cpta', 'Can\'t copy! TypedArray destination cannot be mutated.'); |
| 1078 | } |
| 1079 | if (source === destination) { |
| 1080 | throw ngMinErr('cpi', 'Can\'t copy! Source and destination are identical.'); |
| 1081 | } |
| 1082 | |
| 1083 | // Empty the destination object |
| 1084 | if (isArray(destination)) { |
| 1085 | destination.length = 0; |
| 1086 | } else { |
| 1087 | forEach(destination, function (value, key) { |
| 1088 | if (key !== '$$hashKey') { |
| 1089 | delete destination[key]; |
| 1090 | } |
| 1091 | }); |
| 1092 | } |
| 1093 | |
| 1094 | stackSource.push(source); |
| 1095 | stackDest.push(destination); |
| 1096 | return copyRecurse(source, destination, maxDepth); |
| 1097 | } |
| 1098 | |
| 1099 | return copyElement(source, maxDepth); |
| 1100 | |
| 1101 | function copyRecurse(source, destination, maxDepth) { |
| 1102 | maxDepth--; |
| 1103 | if (maxDepth < 0) { |
| 1104 | return '...'; |
| 1105 | } |
| 1106 | var h = destination.$$hashKey; |
| 1107 | var key; |
| 1108 | if (isArray(source)) { |
| 1109 | for (var i = 0, ii = source.length; i < ii; i++) { |
| 1110 | destination.push(copyElement(source[i], maxDepth)); |
| 1111 | } |
| 1112 | } else if (isBlankObject(source)) { |
| 1113 | // createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty |
| 1114 | for (key in source) { |
| 1115 | destination[key] = copyElement(source[key], maxDepth); |
| 1116 | } |
| 1117 | } else if (source && typeof source.hasOwnProperty === 'function') { |
| 1118 | // Slow path, which must rely on hasOwnProperty |
| 1119 | for (key in source) { |
| 1120 | if (source.hasOwnProperty(key)) { |
| 1121 | destination[key] = copyElement(source[key], maxDepth); |
| 1122 | } |
| 1123 | } |
| 1124 | } else { |
| 1125 | // Slowest path --- hasOwnProperty can't be called as a method |
| 1126 | for (key in source) { |
| 1127 | if (hasOwnProperty.call(source, key)) { |
no test coverage detected