Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix(copy): clear array destinations correctly for non-array sources
Browse files Browse the repository at this point in the history
Closes #8610
Closes #8702
  • Loading branch information
caitp committed Aug 21, 2014
1 parent 0872388 commit a603e20
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -800,9 +800,13 @@ function copy(source, destination, stackSource, stackDest) {
}
} else {
var h = destination.$$hashKey;
forEach(destination, function(value, key) {
delete destination[key];
});
if (isArray(destination)) {
destination.length = 0;
} else {
forEach(destination, function(value, key) {
delete destination[key];
});
}
for ( var key in source) {
if(source.hasOwnProperty(key)) {
result = copy(source[key], null, stackSource, stackDest);
Expand Down
6 changes: 6 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ describe('angular', function() {
expect(aCopy).toBe(aCopy.self);
expect(aCopy.selfs[2]).not.toBe(a.selfs[2]);
});

it('should clear destination arrays correctly when source is non-array', function() {
expect(copy(null, [1,2,3])).toEqual([]);
expect(copy(undefined, [1,2,3])).toEqual([]);
expect(copy({0: 1, 1: 2}, [1,2,3])).toEqual([1,2]);
});
});

describe("extend", function() {
Expand Down

0 comments on commit a603e20

Please sign in to comment.