* @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)
| 864 | </example> |
| 865 | */ |
| 866 | function copy(source, destination, stackSource, stackDest) { |
| 867 | if (isWindow(source) || isScope(source)) { |
| 868 | throw ngMinErr('cpws', |
| 869 | "Can't copy! Making copies of Window or Scope instances is not supported."); |
| 870 | } |
| 871 | if (isTypedArray(destination)) { |
| 872 | throw ngMinErr('cpta', |
| 873 | "Can't copy! TypedArray destination cannot be mutated."); |
| 874 | } |
| 875 | |
| 876 | if (!destination) { |
| 877 | destination = source; |
| 878 | if (isObject(source)) { |
| 879 | var index; |
| 880 | if (stackSource && (index = stackSource.indexOf(source)) !== -1) { |
| 881 | return stackDest[index]; |
| 882 | } |
| 883 | |
| 884 | // TypedArray, Date and RegExp have specific copy functionality and must be |
| 885 | // pushed onto the stack before returning. |
| 886 | // Array and other objects create the base object and recurse to copy child |
| 887 | // objects. The array/object will be pushed onto the stack when recursed. |
| 888 | if (isArray(source)) { |
| 889 | return copy(source, [], stackSource, stackDest); |
| 890 | } else if (isTypedArray(source)) { |
| 891 | destination = new source.constructor(source); |
| 892 | } else if (isDate(source)) { |
| 893 | destination = new Date(source.getTime()); |
| 894 | } else if (isRegExp(source)) { |
| 895 | destination = new RegExp(source.source, source.toString().match(/[^\/]*$/)[0]); |
| 896 | destination.lastIndex = source.lastIndex; |
| 897 | } else if (isFunction(source.cloneNode)) { |
| 898 | destination = source.cloneNode(true); |
| 899 | } else { |
| 900 | var emptyObject = Object.create(getPrototypeOf(source)); |
| 901 | return copy(source, emptyObject, stackSource, stackDest); |
| 902 | } |
| 903 | |
| 904 | if (stackDest) { |
| 905 | stackSource.push(source); |
| 906 | stackDest.push(destination); |
| 907 | } |
| 908 | } |
| 909 | } else { |
| 910 | if (source === destination) throw ngMinErr('cpi', |
| 911 | "Can't copy! Source and destination are identical."); |
| 912 | |
| 913 | stackSource = stackSource || []; |
| 914 | stackDest = stackDest || []; |
| 915 | |
| 916 | if (isObject(source)) { |
| 917 | stackSource.push(source); |
| 918 | stackDest.push(destination); |
| 919 | } |
| 920 | |
| 921 | var result, key; |
| 922 | if (isArray(source)) { |
| 923 | destination.length = 0; |
no test coverage detected