* @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, stackSource, stackDest)
| 778 | </example> |
| 779 | */ |
| 780 | function copy(source, destination, stackSource, stackDest) { |
| 781 | if (isWindow(source) || isScope(source)) { |
| 782 | throw ngMinErr('cpws', |
| 783 | "Can't copy! Making copies of Window or Scope instances is not supported."); |
| 784 | } |
| 785 | |
| 786 | if (!destination) { |
| 787 | destination = source; |
| 788 | if (source) { |
| 789 | if (isArray(source)) { |
| 790 | destination = copy(source, [], stackSource, stackDest); |
| 791 | } else if (isDate(source)) { |
| 792 | destination = new Date(source.getTime()); |
| 793 | } else if (isRegExp(source)) { |
| 794 | destination = new RegExp(source.source, source.toString().match(/[^\/]*$/)[0]); |
| 795 | destination.lastIndex = source.lastIndex; |
| 796 | } else if (isObject(source)) { |
| 797 | var emptyObject = Object.create(Object.getPrototypeOf(source)); |
| 798 | destination = copy(source, emptyObject, stackSource, stackDest); |
| 799 | } |
| 800 | } |
| 801 | } else { |
| 802 | if (source === destination) throw ngMinErr('cpi', |
| 803 | "Can't copy! Source and destination are identical."); |
| 804 | |
| 805 | stackSource = stackSource || []; |
| 806 | stackDest = stackDest || []; |
| 807 | |
| 808 | if (isObject(source)) { |
| 809 | var index = stackSource.indexOf(source); |
| 810 | if (index !== -1) return stackDest[index]; |
| 811 | |
| 812 | stackSource.push(source); |
| 813 | stackDest.push(destination); |
| 814 | } |
| 815 | |
| 816 | var result; |
| 817 | if (isArray(source)) { |
| 818 | destination.length = 0; |
| 819 | for (var i = 0; i < source.length; i++) { |
| 820 | result = copy(source[i], null, stackSource, stackDest); |
| 821 | if (isObject(source[i])) { |
| 822 | stackSource.push(source[i]); |
| 823 | stackDest.push(result); |
| 824 | } |
| 825 | destination.push(result); |
| 826 | } |
| 827 | } else { |
| 828 | var h = destination.$$hashKey; |
| 829 | if (isArray(destination)) { |
| 830 | destination.length = 0; |
| 831 | } else { |
| 832 | forEach(destination, function(value, key) { |
| 833 | delete destination[key]; |
| 834 | }); |
| 835 | } |
| 836 | for (var key in source) { |
| 837 | if (source.hasOwnProperty(key)) { |