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