* @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-purpose copy * function, and
(source, destination, maxDepth)
| 1007 | </example> |
| 1008 | */ |
| 1009 | function copy(source, destination, maxDepth) { |
| 1010 | var stackSource = []; |
| 1011 | var stackDest = []; |
| 1012 | maxDepth = isValidObjectMaxDepth(maxDepth) ? maxDepth : NaN; |
| 1013 | |
| 1014 | if (destination) { |
| 1015 | if (isTypedArray(destination) || isArrayBuffer(destination)) { |
| 1016 | throw ngMinErr('cpta', 'Can\'t copy! TypedArray destination cannot be mutated.'); |
| 1017 | } |
| 1018 | if (source === destination) { |
| 1019 | throw ngMinErr('cpi', 'Can\'t copy! Source and destination are identical.'); |
| 1020 | } |
| 1021 | |
| 1022 | // Empty the destination object |
| 1023 | if (isArray(destination)) { |
| 1024 | destination.length = 0; |
| 1025 | } else { |
| 1026 | forEach(destination, function(value, key) { |
| 1027 | if (key !== '$$hashKey') { |
| 1028 | delete destination[key]; |
| 1029 | } |
| 1030 | }); |
| 1031 | } |
| 1032 | |
| 1033 | stackSource.push(source); |
| 1034 | stackDest.push(destination); |
| 1035 | return copyRecurse(source, destination, maxDepth); |
| 1036 | } |
| 1037 | |
| 1038 | return copyElement(source, maxDepth); |
| 1039 | |
| 1040 | function copyRecurse(source, destination, maxDepth) { |
| 1041 | maxDepth--; |
| 1042 | if (maxDepth < 0) { |
| 1043 | return '...'; |
| 1044 | } |
| 1045 | var h = destination.$$hashKey; |
| 1046 | var key; |
| 1047 | if (isArray(source)) { |
| 1048 | for (var i = 0, ii = source.length; i < ii; i++) { |
| 1049 | destination.push(copyElement(source[i], maxDepth)); |
| 1050 | } |
| 1051 | } else if (isBlankObject(source)) { |
| 1052 | // createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty |
| 1053 | for (key in source) { |
| 1054 | destination[key] = copyElement(source[key], maxDepth); |
| 1055 | } |
| 1056 | } else if (source && typeof source.hasOwnProperty === 'function') { |
| 1057 | // Slow path, which must rely on hasOwnProperty |
| 1058 | for (key in source) { |
| 1059 | if (source.hasOwnProperty(key)) { |
| 1060 | destination[key] = copyElement(source[key], maxDepth); |
| 1061 | } |
| 1062 | } |
| 1063 | } else { |
| 1064 | // Slowest path --- hasOwnProperty can't be called as a method |
| 1065 | for (key in source) { |
| 1066 | if (hasOwnProperty.call(source, key)) { |
no test coverage detected