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

Commit 6cbbd96

Browse files
gabrielmaldiNarretz
authored andcommitted
fix(merge): treat dates as atomic values instead of objects.
Makes angular.merge copy dates correctly. Closes #11720 Closes #11720
1 parent d0cb693 commit 6cbbd96

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/Angular.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,12 @@ function baseExtend(dst, objs, deep) {
352352
var src = obj[key];
353353

354354
if (deep && isObject(src)) {
355-
if (!isObject(dst[key])) dst[key] = isArray(src) ? [] : {};
356-
baseExtend(dst[key], [src], true);
355+
if (isDate(src)) {
356+
dst[key] = new Date(src.valueOf());
357+
} else {
358+
if (!isObject(dst[key])) dst[key] = isArray(src) ? [] : {};
359+
baseExtend(dst[key], [src], true);
360+
}
357361
} else {
358362
dst[key] = src;
359363
}

test/AngularSpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,16 @@ describe('angular', function() {
481481
// make sure we retain the old key
482482
expect(hashKey(dst)).toEqual(h);
483483
});
484+
485+
486+
it('should copy dates by reference', function() {
487+
var src = { date: new Date() };
488+
var dst = {};
489+
490+
extend(dst, src);
491+
492+
expect(dst.date).toBe(src.date);
493+
});
484494
});
485495

486496

@@ -548,6 +558,18 @@ describe('angular', function() {
548558
});
549559
expect(dst.foo).not.toBe(src.foo);
550560
});
561+
562+
563+
it('should copy dates by value', function() {
564+
var src = { date: new Date() };
565+
var dst = {};
566+
567+
merge(dst, src);
568+
569+
expect(dst.date).not.toBe(src.date);
570+
expect(isDate(dst.date)).toBeTruthy();
571+
expect(dst.date.valueOf()).toEqual(src.date.valueOf());
572+
});
551573
});
552574

553575

0 commit comments

Comments
 (0)