* @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)
| 885 | </example> |
| 886 | */ |
| 887 | function copy(source, destination) { |
| 888 | var stackSource = []; |
| 889 | var stackDest = []; |
| 890 | |
| 891 | if (destination) { |
| 892 | if (isTypedArray(destination) || isArrayBuffer(destination)) { |
| 893 | throw ngMinErr('cpta', 'Can\'t copy! TypedArray destination cannot be mutated.'); |
| 894 | } |
| 895 | if (source === destination) { |
| 896 | throw ngMinErr('cpi', 'Can\'t copy! Source and destination are identical.'); |
| 897 | } |
| 898 | |
| 899 | // Empty the destination object |
| 900 | if (isArray(destination)) { |
| 901 | destination.length = 0; |
| 902 | } else { |
| 903 | forEach(destination, function(value, key) { |
| 904 | if (key !== '$$hashKey') { |
| 905 | delete destination[key]; |
| 906 | } |
| 907 | }); |
| 908 | } |
| 909 | |
| 910 | stackSource.push(source); |
| 911 | stackDest.push(destination); |
| 912 | return copyRecurse(source, destination); |
| 913 | } |
| 914 | |
| 915 | return copyElement(source); |
| 916 | |
| 917 | function copyRecurse(source, destination) { |
| 918 | var h = destination.$$hashKey; |
| 919 | var key; |
| 920 | if (isArray(source)) { |
| 921 | for (var i = 0, ii = source.length; i < ii; i++) { |
| 922 | destination.push(copyElement(source[i])); |
| 923 | } |
| 924 | } else if (isBlankObject(source)) { |
| 925 | // createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty |
| 926 | for (key in source) { |
| 927 | destination[key] = copyElement(source[key]); |
| 928 | } |
| 929 | } else if (source && typeof source.hasOwnProperty === 'function') { |
| 930 | // Slow path, which must rely on hasOwnProperty |
| 931 | for (key in source) { |
| 932 | if (source.hasOwnProperty(key)) { |
| 933 | destination[key] = copyElement(source[key]); |
| 934 | } |
| 935 | } |
| 936 | } else { |
| 937 | // Slowest path --- hasOwnProperty can't be called as a method |
| 938 | for (key in source) { |
| 939 | if (hasOwnProperty.call(source, key)) { |
| 940 | destination[key] = copyElement(source[key]); |
| 941 | } |
| 942 | } |
| 943 | } |
| 944 | setHashKey(destination, h); |
no test coverage detected