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

Commit a603e20

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

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/Angular.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -800,9 +800,13 @@ function copy(source, destination, stackSource, stackDest) {
800800
}
801801
} else {
802802
var h = destination.$$hashKey;
803-
forEach(destination, function(value, key) {
804-
delete destination[key];
805-
});
803+
if (isArray(destination)) {
804+
destination.length = 0;
805+
} else {
806+
forEach(destination, function(value, key) {
807+
delete destination[key];
808+
});
809+
}
806810
for ( var key in source) {
807811
if(source.hasOwnProperty(key)) {
808812
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)