Skip to content

Commit 6817c89

Browse files
committed
fix(copy): clear array destinations correctly for non-array sources
Closes angular#8610
1 parent 0872388 commit 6817c89

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/Angular.js

+1
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,7 @@ function copy(source, destination, stackSource, stackDest) {
803803
forEach(destination, function(value, key) {
804804
delete destination[key];
805805
});
806+
if (isArray(destination)) destination.length = 0;
806807
for ( var key in source) {
807808
if(source.hasOwnProperty(key)) {
808809
result = copy(source[key], null, stackSource, stackDest);

test/AngularSpec.js

+6
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ describe('angular', function() {
184184
expect(aCopy).toBe(aCopy.self);
185185
expect(aCopy.selfs[2]).not.toBe(a.selfs[2]);
186186
});
187+
188+
it('should clear destination arrays correctly when source is non-array', function() {
189+
expect(copy(null, [1,2,3])).toEqual([]);
190+
expect(copy(undefined, [1,2,3])).toEqual([]);
191+
expect(copy({0: 1, 1: 2}, [1,2,3])).toEqual([1,2]);
192+
});
187193
});
188194

189195
describe("extend", function() {

0 commit comments

Comments
 (0)