-
Notifications
You must be signed in to change notification settings - Fork 27.5k
fix(merge): treat dates as atomic values instead of objects. #11720
Conversation
0bad26c
to
758b9b3
Compare
Makes angular.merge copy dates correctly. Closes angular#11720
In the docs, it says that In that sense, this PR seems to be doing the right thing. |
I think we should also include this: dst[key] = isDate(src) ? new Date(src.valueOf()) : src; so that dates get cloned instead of assigning a reference to the same object. |
758b9b3
to
1a60e63
Compare
Makes angular.merge copy dates correctly. Closes angular#11720
if (!isObject(dst[key])) dst[key] = isArray(src) ? [] : {}; | ||
baseExtend(dst[key], [src], true); | ||
} else { | ||
dst[key] = src; | ||
dst[key] = isDate(src) ? new Date(src.valueOf()) : src; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is the right think to do. It will also apply to non-deep copying, where I believe it would be better to just assign the same Date object.
It's OK for deep extending I guess, so maybe something like:
if (deep && isObject(src)) {
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;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree! Done and thanks for the snippet.
Makes angular.merge copy dates correctly. Closes angular#11720
The implementation lgtm. But we should have some tests in place, in order to ensure there will be no accidental regressions in the future. @gabrielmaldi, could add a test (i.e. ones that would fail without this fix and pass with it) ? |
Sure, I'll work on it later today. |
I wrote a couple of tests but didn't push them because one is failing and I believe it's an issue with the version of Here's a gist with the tests; it fails on the last line with this error: I wrote this pen which uses I also tried using: var dst2 = {};
brokenMerge(dst2, src);
expect(dst2).not.toEqual({
foo: date
}); but it fails with the same error: Thanks for your help. EDIT: I also tried separating the tests and I get the same error. |
Angular is using jamsine 1.3. In this pen, I have a couple of tests that are currently failing. |
Makes angular.merge copy dates correctly. Closes angular#11720
Yes, your tests pass with the fix. I guess |
Makes angular.merge copy dates correctly. Closes angular#11720
Makes angular.merge copy dates correctly. Closes angular#11720
Makes angular.merge copy dates correctly. Closes angular#11720
Makes angular.merge copy dates correctly. Closes angular#11720
Makes angular.merge copy dates correctly. Closes angular#11720
0096c16
to
c3f2c90
Compare
Makes angular.merge copy dates correctly. Closes angular#11720 Closes angular#11720
Makes
angular.merge
copy dates correctly.Before and after this change: