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

Commit 888b0f5

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

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
@@ -820,9 +820,13 @@ function copy(source, destination, stackSource, stackDest) {
820820
}
821821
} else {
822822
var h = destination.$$hashKey;
823-
forEach(destination, function(value, key) {
824-
delete destination[key];
825-
});
823+
if (isArray(destination)) {
824+
destination.length = 0;
825+
} else {
826+
forEach(destination, function(value, key) {
827+
delete destination[key];
828+
});
829+
}
826830
for ( var key in source) {
827831
result = copy(source[key], null, stackSource, stackDest);
828832
if (isObject(source[key])) {

test/AngularSpec.js

+6
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ describe('angular', function() {
174174
expect(aCopy).toBe(aCopy.self);
175175
expect(aCopy.selfs[2]).not.toBe(a.selfs[2]);
176176
});
177+
178+
it('should clear destination arrays correctly when source is non-array', function() {
179+
expect(copy(null, [1,2,3])).toEqual([]);
180+
expect(copy(undefined, [1,2,3])).toEqual([]);
181+
expect(copy({0: 1, 1: 2}, [1,2,3])).toEqual([1,2]);
182+
});
177183
});
178184

179185
describe("extend", function() {

0 commit comments

Comments
 (0)