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

fix(merge): treat dates as atomic values instead of objects. #11720

Closed
wants to merge 1 commit into from

Conversation

gabrielmaldi
Copy link
Contributor

Makes angular.merge copy dates correctly.

Before and after this change:

image

gabrielmaldi added a commit to gabrielmaldi/angular.js that referenced this pull request Apr 24, 2015
Makes angular.merge copy dates correctly.

Closes angular#11720
@gkalpak
Copy link
Member

gkalpak commented Apr 25, 2015

In the docs, it says that merge() "recursively descends into object properties of source objects, performing a deep copy". That is so not true :(
It performs a "deep extend".

In that sense, this PR seems to be doing the right thing.

@gabrielmaldi
Copy link
Contributor Author

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.

gabrielmaldi added a commit to gabrielmaldi/angular.js that referenced this pull request Apr 28, 2015
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;
Copy link
Member

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;
}

Copy link
Contributor Author

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.

gabrielmaldi added a commit to gabrielmaldi/angular.js that referenced this pull request May 1, 2015
Makes angular.merge copy dates correctly.

Closes angular#11720
@gkalpak
Copy link
Member

gkalpak commented May 2, 2015

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) ?

@gabrielmaldi
Copy link
Contributor Author

@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.

@gabrielmaldi
Copy link
Contributor Author

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 Jasmine which Angular is using.

Here's a gist with the tests; it fails on the last line with this error:

screen shot 2015-05-02 at 3 54 45 pm

I wrote this pen which uses Jasmine 2.0.0 and Angular 1.4.0-rc.1 and the test passes just fine.

I also tried using:

var dst2 = {};
brokenMerge(dst2, src);
expect(dst2).not.toEqual({
  foo: date
});

but it fails with the same error:

screen shot 2015-05-02 at 4 07 20 pm

Thanks for your help.

EDIT: I also tried separating the tests and I get the same error.

@gkalpak
Copy link
Member

gkalpak commented May 2, 2015

Angular is using jamsine 1.3. In this pen, I have a couple of tests that are currently failing.
I would expect them to pass with your fix. (If that is indeed the case, you can just use those two (maybe update ther descriptions if necessary).)

In case it wasn't clear, you don't have a brokenMerge in your testcase.
It suffices to have tests that would have failed, were they run prior to the fix and pass with the fix.

gabrielmaldi added a commit to gabrielmaldi/angular.js that referenced this pull request May 3, 2015
Makes angular.merge copy dates correctly.

Closes angular#11720
@gabrielmaldi
Copy link
Contributor Author

Yes, your tests pass with the fix. I guess Jasmine 1.3 behaves differently and using valueOf to compare dates is a valid workaround. Thanks for your guidance.

@petebacondarwin petebacondarwin added this to the 1.4.x - jaracimrman-existence milestone May 6, 2015
gabrielmaldi added a commit to gabrielmaldi/angular.js that referenced this pull request May 6, 2015
Makes angular.merge copy dates correctly.

Closes angular#11720
gabrielmaldi added a commit to gabrielmaldi/angular.js that referenced this pull request May 12, 2015
Makes angular.merge copy dates correctly.

Closes angular#11720
gabrielmaldi added a commit to gabrielmaldi/angular.js that referenced this pull request May 21, 2015
Makes angular.merge copy dates correctly.

Closes angular#11720
gabrielmaldi added a commit to gabrielmaldi/angular.js that referenced this pull request May 27, 2015
Makes angular.merge copy dates correctly.

Closes angular#11720
@Narretz Narretz self-assigned this Jun 3, 2015
Makes angular.merge copy dates correctly.

Closes angular#11720
@Narretz Narretz closed this in 6cbbd96 Jun 22, 2015
@gabrielmaldi gabrielmaldi deleted the merge-dates branch June 23, 2015 14:05
luckylooke added a commit to luckylooke/angular.js that referenced this pull request Jul 7, 2015
netman92 pushed a commit to netman92/angular.js that referenced this pull request Aug 8, 2015
gkalpak pushed a commit to gkalpak/angular.js that referenced this pull request Nov 23, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants