* @ngdoc function * @name angular.copy * @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 elements (for array) or pr
(source, destination)
| 799 | </doc:example> |
| 800 | */ |
| 801 | function copy(source, destination){ |
| 802 | if (isWindow(source) || isScope(source)) { |
| 803 | throw ngMinErr('cpws', |
| 804 | "Can't copy! Making copies of Window or Scope instances is not supported."); |
| 805 | } |
| 806 | |
| 807 | if (!destination) { |
| 808 | destination = source; |
| 809 | if (source) { |
| 810 | if (isArray(source)) { |
| 811 | destination = copy(source, []); |
| 812 | } else if (isDate(source)) { |
| 813 | destination = new Date(source.getTime()); |
| 814 | } else if (isRegExp(source)) { |
| 815 | destination = new RegExp(source.source); |
| 816 | } else if (isObject(source)) { |
| 817 | destination = copy(source, {}); |
| 818 | } |
| 819 | } |
| 820 | } else { |
| 821 | if (source === destination) throw ngMinErr('cpi', |
| 822 | "Can't copy! Source and destination are identical."); |
| 823 | if (isArray(source)) { |
| 824 | destination.length = 0; |
| 825 | for ( var i = 0; i < source.length; i++) { |
| 826 | destination.push(copy(source[i])); |
| 827 | } |
| 828 | } else { |
| 829 | var h = destination.$$hashKey; |
| 830 | forEach(destination, function(value, key){ |
| 831 | delete destination[key]; |
| 832 | }); |
| 833 | for ( var key in source) { |
| 834 | destination[key] = copy(source[key]); |
| 835 | } |
| 836 | setHashKey(destination,h); |
| 837 | } |
| 838 | } |
| 839 | return destination; |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * Create a shallow copy of an object |
no test coverage detected
searching dependent graphs…