* @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)
| 857 | </example> |
| 858 | */ |
| 859 | function copy(source, destination) { |
| 860 | var stackSource = []; |
| 861 | var stackDest = []; |
| 862 | |
| 863 | if (destination) { |
| 864 | if (isTypedArray(destination) || isArrayBuffer(destination)) { |
| 865 | throw ngMinErr('cpta', "Can't copy! TypedArray destination cannot be mutated."); |
| 866 | } |
| 867 | if (source === destination) { |
| 868 | throw ngMinErr('cpi', "Can't copy! Source and destination are identical."); |
| 869 | } |
| 870 | |
| 871 | // Empty the destination object |
| 872 | if (isArray(destination)) { |
| 873 | destination.length = 0; |
| 874 | } else { |
| 875 | forEach(destination, function(value, key) { |
| 876 | if (key !== '$$hashKey') { |
| 877 | delete destination[key]; |
| 878 | } |
| 879 | }); |
| 880 | } |
| 881 | |
| 882 | stackSource.push(source); |
| 883 | stackDest.push(destination); |
| 884 | return copyRecurse(source, destination); |
| 885 | } |
| 886 | |
| 887 | return copyElement(source); |
| 888 | |
| 889 | function copyRecurse(source, destination) { |
| 890 | var h = destination.$$hashKey; |
| 891 | var result, key; |
| 892 | if (isArray(source)) { |
| 893 | for (var i = 0, ii = source.length; i < ii; i++) { |
| 894 | destination.push(copyElement(source[i])); |
| 895 | } |
| 896 | } else if (isBlankObject(source)) { |
| 897 | // createMap() fast path --- Safe to avoid hasOwnProperty check because prototype chain is empty |
| 898 | for (key in source) { |
| 899 | destination[key] = copyElement(source[key]); |
| 900 | } |
| 901 | } else if (source && typeof source.hasOwnProperty === 'function') { |
| 902 | // Slow path, which must rely on hasOwnProperty |
| 903 | for (key in source) { |
| 904 | if (source.hasOwnProperty(key)) { |
| 905 | destination[key] = copyElement(source[key]); |
| 906 | } |
| 907 | } |
| 908 | } else { |
| 909 | // Slowest path --- hasOwnProperty can't be called as a method |
| 910 | for (key in source) { |
| 911 | if (hasOwnProperty.call(source, key)) { |
| 912 | destination[key] = copyElement(source[key]); |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | setHashKey(destination, h); |
no test coverage detected