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