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

Commit fca94d4

Browse files
committed
fix(merge): treat dates as atomic values instead of objects.
Makes angular.merge copy dates correctly. Closes #11720
1 parent 1268b17 commit fca94d4

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
@@ -335,8 +335,12 @@ function baseExtend(dst, objs, deep) {
335335
var src = obj[key];
336336

337337
if (deep && isObject(src)) {
338-
if (!isObject(dst[key])) dst[key] = isArray(src) ? [] : {};
339-
baseExtend(dst[key], [src], true);
338+
if (isDate(src)) {
339+
dst[key] = new Date(src.valueOf());
340+
} else {
341+
if (!isObject(dst[key])) dst[key] = isArray(src) ? [] : {};
342+
baseExtend(dst[key], [src], true);
343+
}
340344
} else {
341345
dst[key] = src;
342346
}

test/AngularSpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,16 @@ describe('angular', function() {
405405
// make sure we retain the old key
406406
expect(hashKey(dst)).toEqual(h);
407407
});
408+
409+
410+
it('should copy dates by reference', function() {
411+
var src = { date: new Date() };
412+
var dst = {};
413+
414+
extend(dst, src);
415+
416+
expect(dst.date).toBe(src.date);
417+
});
408418
});
409419

410420

@@ -472,6 +482,18 @@ describe('angular', function() {
472482
});
473483
expect(dst.foo).not.toBe(src.foo);
474484
});
485+
486+
487+
it('should copy dates by value', function() {
488+
var src = { date: new Date() };
489+
var dst = {};
490+
491+
merge(dst, src);
492+
493+
expect(dst.date).not.toBe(src.date);
494+
expect(isDate(dst.date)).toBeTruthy();
495+
expect(dst.date.valueOf()).toEqual(src.date.valueOf());
496+
});
475497
});
476498

477499

0 commit comments

Comments
 (0)