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

Commit

Permalink
fix(merge): treat dates as atomic values instead of objects.
Browse files Browse the repository at this point in the history
Makes angular.merge copy dates correctly.

Closes #11720
  • Loading branch information
gabrielmaldi committed Jun 10, 2015
1 parent ffac747 commit c3f2c90
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
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

0 comments on commit c3f2c90

Please sign in to comment.