-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#1053 Introduce CKEDITOR.tools.object.merge #1055
Conversation
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.
Just few comments.
core/tools.js
Outdated
|
||
tools.array.forEach( tools.objectKeys( copy2 ), function( key ) { | ||
if ( typeof copy2[ key ] === 'object' && typeof copy1[ key ] === 'object' ) { | ||
copy1[ key ] = merge( copy1[ key ], copy2[ key ] ); |
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.
Can't we use simply tools.merge? merge
is defined in a hacky way, very uncommon to our code base.
core/tools.js
Outdated
*/ | ||
merge: function merge( obj1, obj2 ) { | ||
var copy1 = CKEDITOR.tools.clone( obj1 ), | ||
copy2 = CKEDITOR.tools.clone( obj2 ), |
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.
both members could reuse tools
that was defined just a line below.
}, | ||
|
||
/** | ||
* Merges two objects and returns new one. |
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.
An example would on how it actually merges would be a brilliant idea.
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.
It would be good if example would also illustrate what happens if both objects have properties with the same name/key.
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.
Just small changes suggested in comments.
@Comandeer do you think the merge
function should do some type checking (if passed objects are really an objects, not array, null, etc) to prevent misuse?
}, | ||
|
||
/** | ||
* Merges two objects and returns new one. |
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.
It would be good if example would also illustrate what happens if both objects have properties with the same name/key.
for ( var name in expected ) { | ||
if ( expected.hasOwnProperty( name ) ) { | ||
if ( expected[ name ] && typeof expected[ name ] === 'object' ) { | ||
areDeepEqual( expected[ name ], actual[ name ] ); |
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.
Same as https://github.com/ckeditor/ckeditor-dev/pull/1055/files#r144820711, couldn't it be just bender.objectAssert.areDeepEqual( ... );
There are no such checkings in other |
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.
Only one tiny, but important detail to fix in docs.
core/tools.js
Outdated
* } | ||
* | ||
* @param {Object} obj1 A base object. | ||
* @param {Object} obj2 An object, which properties will be merged to the base one. |
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.
An object, which properties will be merged to the base one.
I think this is a little misleading, as properties are not merged to the base object, but to its copy, so the base object is not modified. And it is also inconsistent with method description which suggest the new object is created (Merges two objects and returns new one).
ATM I don't have any clear idea how it could be described. I checked in other libs/docs how it is described there:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
- https://lodash.com/docs/#merge
- http://api.jquery.com/jQuery.merge/
- http://ramdajs.com/docs/#merge
but still not sure. Maybe you could come up with something reasonable?
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.
Maybe something like that?
obj1 Source object, which will be used to create a new base object.
And then obj2
description is already good :D WDYT @f1ames?
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.
@Comandeer works for me 👍
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.
LGTM 👍
What is the purpose of this pull request?
New feature
Does your PR contain necessary tests?
All patches which change the editor code must include tests. You can always read more
on PR testing,
how to set the testing environment and
how to create tests
in the official CKEditor documentation.
This PR contains
What changes did you make?
Introduce
CKEDITOR.tools.object.merge
, which merges two objects.Closes #1053.