-
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
CKEDITOR.tools.object helper methods improvements #3123
Comments
TBH I don't like proposed changes. Personally I'd implement functions equivalent to native functions that operate on objects:
Having these low-level functions we can implement |
Have to agree with @Comandeer that functions reflecting native ones will be more intuitive and easier to use (as you already know the API), so it's 👍 from me. One clarification, though:
Do you mean we should implement
? |
I'm also not happy with the namespace which seems inaccurate. Although, bear in mind that this function is used very often and using alias after changes will create bad looking inconsistency in our code base. Maybe it makes sense to deprecate
If you read the description carefully, you will see that both functions output follows the order of
That would be a bad idea. These functions are used extensively in our code base (or rather reimplemented over and over. They also follow the signature of object.reduce( obj, function( acc, value, key) {} ); in popular libraries like lodash. As I like making them tuple as an output for simplicity, using: object.reduce( obj, function( acc, value, key) {
acc[ key ] = value;
return acc;
}, {} ); is much more readable than object.reduce( obj, function( acc, tuple ) {
acc[ tuple[ 0 ] ] = tuple[ 1 ];
return acc;
}, {} ); |
I don't have a strong opinion here. Initially I thought about skipping them, but we can also introduce them and use proposed low-level functions inside. However I think that introducing such methods would be slightly redundant.
Deprecating it can be moved to separate ticket, with only adding new alias in this PR. I don't understand the part about signatures. What land inside TBH the issue with tuple is connected strictly with the fact that we don't use any transpiler and are forced to write directly in ES3. In modern JS the problem is just nonexistent, thanks to destructuring: Object.entries( obj ).reduce( obj, function( acc, [ key, value ] ) {
acc[ key ] = value;
return acc;
}, {} ); In ES3 it can be mitigated by some workarounds: var entries = CKEDITOR.tools.object.entries( obj );
CKEDITOR.tools.array.reduce( entries, function( obj, entry ) {
var key = entry[ 0 ],
value = entry[ 1 ];
obj[ key ] = value;
return obj;
}, {} ); Maybe not ideal, but still manageable. |
Won't it affect only IE8? OTOH I can't think of any case, in which I must depend on properties enumeration order. |
Yes, I already wrote it in quoted by you comment.
There may be some cases when using an object as a hashmap, cache, etc. It would be safer to keep the same behavior as a native one. But again, these are only special methods which will be out of order (like toString) - very edge case (like no case at all). Still, exceptions from native implementation may provide some confusion.
There is no native
I would argue, you are mostly using
How it's related to the discussion? 🤔 We are not using ecma6 so can't take pleasure of this syntactic sugar. |
I see we didn't reach any conclusion yet? cc @jacekbogdanski @Comandeer
Since this issue is only present on IE8 and touches only few native object props it's not not a deal breaker here. I would only not that in the method docs that for IE8 there is such inconsistent behavior.
We could do this. But OTOH, deprecating it shouldn't be that much work (marking in docs as deprecated and some replacements in the code) so I think we could carry it in this PR. WDYT?
I'm not sure, I also find them kind of redundant (also there are no native alternatives). Are there any particular cases which made you implement these methods or you thought they could be useful in the future? cc @jacekbogdanski And we already have usages like: return CKEDITOR.tools.array.reduce( CKEDITOR.tools.objectKeys( widgets ), function( featuredWidgets, id ) { ... } ); |
I already wrote that you could rewrite mostly every From my perspective, it nicely hides an imperative API using a more declarative approach. In some cases, you also may not have access to the object used for reduction when e.g. using higher order functions. Something like: ( function() {
var obj = { foo: 1, bar: 2 };
CKEDITOR.tools.array.reduce( CKEDITOR.tools.objectKeys( obj ), doSomeStuff, {} );
} )();
function doSomeStuff( acc, id ) {
acc[ id ] = obj[ id ] // Woops! We have a problem. The scope of `obj` object is different.
} Note that these are tools functions, so mostly they are used when available. It extends your development toolkit, not fixes a particular problem. For the rest of suggestions like deprecating If you still have doubts about |
To sum up we will delay |
Type of report
Feature request
Provide description of the new feature
#3120 issue is caused by the ECMA3
DontEnum
attribute. The attribute is set on some object properties which cannot be enumerated, soCKEDITOR.tools.extend
method is unable to correctly copy object prototype properties.I would like to take advantage of the situation and improve our
CKEDITOR.tools.object
helper methods during bug fixing:CKEDITOR.tools.objectKeys
- should includeDontEnum
properties to fix the issue for IE8CKEDITOR.tools.object.forEach
- fixing the issue withfor in
loop for IE8CKEDITOR.tools.object.reduce
- the same as aboveCKEDITOR.tools.extend
- fixed by usingCKEDITOR.tools.object.forEach
function instead offor in
loopBoth
forEach
andreduce
function will have pre-implementedDontEnum
bug fix (by usingCKEDITOR.tools.objectKeys
which will reduce the possibility of the issue in the future.The text was updated successfully, but these errors were encountered: