* @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)
| 573 | * @returns {*} The copy or updated `destination`, if `destination` was specified. |
| 574 | */ |
| 575 | function copy(source, destination){ |
| 576 | if (isWindow(source) || isScope(source)) throw Error("Can't copy Window or Scope"); |
| 577 | if (!destination) { |
| 578 | destination = source; |
| 579 | if (source) { |
| 580 | if (isArray(source)) { |
| 581 | destination = copy(source, []); |
| 582 | } else if (isDate(source)) { |
| 583 | destination = new Date(source.getTime()); |
| 584 | } else if (isObject(source)) { |
| 585 | destination = copy(source, {}); |
| 586 | } |
| 587 | } |
| 588 | } else { |
| 589 | if (source === destination) throw Error("Can't copy equivalent objects or arrays"); |
| 590 | if (isArray(source)) { |
| 591 | destination.length = 0; |
| 592 | for ( var i = 0; i < source.length; i++) { |
| 593 | destination.push(copy(source[i])); |
| 594 | } |
| 595 | } else { |
| 596 | var h = destination.$$hashKey; |
| 597 | forEach(destination, function(value, key){ |
| 598 | delete destination[key]; |
| 599 | }); |
| 600 | for ( var key in source) { |
| 601 | destination[key] = copy(source[key]); |
| 602 | } |
| 603 | setHashKey(destination,h); |
| 604 | } |
| 605 | } |
| 606 | return destination; |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * Create a shallow copy of an object |
no test coverage detected