Skip to content

Commit 0096c16

Browse files
committed
fix(merge): treat dates as atomic values instead of objects.
Makes angular.merge copy dates correctly. Closes angular#11720
1 parent 291d7c4 commit 0096c16

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
@@ -420,6 +420,16 @@ describe('angular', function() {
420420
// make sure we retain the old key
421421
expect(hashKey(dst)).toEqual(h);
422422
});
423+
424+
425+
it('should copy dates by reference', function() {
426+
var src = { date: new Date() };
427+
var dst = {};
428+
429+
extend(dst, src);
430+
431+
expect(dst.date).toBe(src.date);
432+
});
423433
});
424434

425435

@@ -487,6 +497,18 @@ describe('angular', function() {
487497
});
488498
expect(dst.foo).not.toBe(src.foo);
489499
});
500+
501+
502+
it('should copy dates by value', function() {
503+
var src = { date: new Date() };
504+
var dst = {};
505+
506+
merge(dst, src);
507+
508+
expect(dst.date).not.toBe(src.date);
509+
expect(isDate(dst.date)).toBeTruthy();
510+
expect(dst.date.valueOf()).toEqual(src.date.valueOf());
511+
});
490512
});
491513

492514

0 commit comments

Comments
 (0)