Skip to content

Commit

Permalink
fix(copy): clear array destinations correctly for non-array sources
Browse files Browse the repository at this point in the history
  • Loading branch information
caitp committed Aug 21, 2014
1 parent 0872388 commit 6817c89
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,7 @@ function copy(source, destination, stackSource, stackDest) {
forEach(destination, function(value, key) {
delete destination[key];
});
if (isArray(destination)) destination.length = 0;
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 6817c89

Please sign in to comment.