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

fix(merge): treat dates as atomic values instead of objects. #11720

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,12 @@ function baseExtend(dst, objs, deep) {
var src = obj[key];

if (deep && isObject(src)) {
if (!isObject(dst[key])) dst[key] = isArray(src) ? [] : {};
baseExtend(dst[key], [src], true);
if (isDate(src)) {
dst[key] = new Date(src.valueOf());
} else {
if (!isObject(dst[key])) dst[key] = isArray(src) ? [] : {};
baseExtend(dst[key], [src], true);
}
} else {
dst[key] = src;
}
Expand Down
22 changes: 22 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,16 @@ describe('angular', function() {
// make sure we retain the old key
expect(hashKey(dst)).toEqual(h);
});


it('should copy dates by reference', function() {
var src = { date: new Date() };
var dst = {};

extend(dst, src);

expect(dst.date).toBe(src.date);
});
});


Expand Down Expand Up @@ -548,6 +558,18 @@ describe('angular', function() {
});
expect(dst.foo).not.toBe(src.foo);
});


it('should copy dates by value', function() {
var src = { date: new Date() };
var dst = {};

merge(dst, src);

expect(dst.date).not.toBe(src.date);
expect(isDate(dst.date)).toBeTruthy();
expect(dst.date.valueOf()).toEqual(src.date.valueOf());
});
});


Expand Down