* @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 destina
(source, destination)
| 835 | </example> |
| 836 | */ |
| 837 | function copy(source, destination) { |
| 838 | var stackSource = []; |
| 839 | var stackDest = []; |
| 840 | |
| 841 | if (destination) { |
| 842 | if (isTypedArray(destination) || isArrayBuffer(destination)) { |
| 843 | throw ngMinErr('cpta', "Can't copy! TypedArray destination cannot be mutated."); |
| 844 | } |
| 845 | if (source === destination) { |
| 846 | throw ngMinErr('cpi', "Can't copy! Source and destination are identical."); |
| 847 | } |
| 848 | |
| 849 | // Empty the destination object |
| 850 | if (isArray(destination)) { |
| 851 | destination.length = 0; |
| 852 | } else { |
| 853 | forEach(destination, |
| 854 | function (value, key) { |
| 855 | if (key !== '$$hashKey') { |
| 856 | delete destination[key]; |
| 857 | } |
| 858 | }); |
| 859 | } |
| 860 | |
| 861 | stackSource.push(source); |
| 862 | stackDest.push(destination); |
| 863 | return copyRecurse(source, destination); |
| 864 | } |
| 865 | |
| 866 | return copyElement(source); |
| 867 | |
| 868 | function copyRecurse(source, destination) { |
| 869 | var h = destination.$$hashKey; |
| 870 | var result, key; |
| 871 | if (isArray(source)) { |
| 872 | for (var i = 0, ii = source.length; i < ii; i++) { |
| 873 | destination.push(copyElement(source[i])); |
| 874 | } |
| 875 | } else if (isBlankObject(source)) { |
| 876 | // createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty |
| 877 | for (key in source) { |
| 878 | destination[key] = copyElement(source[key]); |
| 879 | } |
| 880 | } else if (source && typeof source.hasOwnProperty === 'function') { |
| 881 | // Slow path, which must rely on hasOwnProperty |
| 882 | for (key in source) { |
| 883 | if (source.hasOwnProperty(key)) { |
| 884 | destination[key] = copyElement(source[key]); |
| 885 | } |
| 886 | } |
| 887 | } else { |
| 888 | // Slowest path --- hasOwnProperty can't be called as a method |
| 889 | for (key in source) { |
| 890 | if (hasOwnProperty.call(source, key)) { |
| 891 | destination[key] = copyElement(source[key]); |
| 892 | } |
| 893 | } |
| 894 | } |
no test coverage detected