-
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
Changes from 9 commits
eb8bafb
13c2d7d
0a9877c
adb70ba
cc89bab
b935d85
d2e6992
7b619c7
d420169
27ab4a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1991,6 +1991,59 @@ | |
} | ||
|
||
return null; | ||
}, | ||
|
||
/** | ||
* Merges two objects and returns new one. | ||
* | ||
* var obj1 = { | ||
* a: 1, | ||
* conflicted: 10, | ||
* obj: { | ||
* c: 1 | ||
* } | ||
* }, | ||
* obj2 = { | ||
* b: 2, | ||
* conflicted: 20, | ||
* obj: { | ||
* d: 2 | ||
* } | ||
* }; | ||
* | ||
* CKEDITOR.tools.object.merge( obj1, obj2 ); | ||
* | ||
* This code produces the following object; | ||
* | ||
* { | ||
* a: 1, | ||
* b: 2, | ||
* conflicted: 20, | ||
* obj: { | ||
* c: 1, | ||
* d: 2 | ||
* } | ||
* } | ||
* | ||
* @param {Object} obj1 Source object, which will be used to create a new 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 commentThe reason will be displayed to describe this comment to others. Learn more.
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:
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 commentThe reason will be displayed to describe this comment to others. Learn more. Maybe something like that?
And then There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Comandeer works for me 👍 |
||
* @returns {Object} Merged object. | ||
* @member CKEDITOR.tools.object | ||
*/ | ||
merge: function( obj1, obj2 ) { | ||
var tools = CKEDITOR.tools, | ||
copy1 = tools.clone( obj1 ), | ||
copy2 = tools.clone( obj2 ); | ||
|
||
tools.array.forEach( tools.objectKeys( copy2 ), function( key ) { | ||
if ( typeof copy2[ key ] === 'object' && typeof copy1[ key ] === 'object' ) { | ||
copy1[ key ] = tools.object.merge( copy1[ key ], copy2[ key ] ); | ||
} else { | ||
copy1[ key ] = copy2[ key ]; | ||
} | ||
} ); | ||
|
||
return copy1; | ||
} | ||
} | ||
}; | ||
|
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.