-
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
Added CKEDITOR.tools.array.find #2705
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.
Quite a few minor things related to API docs and the tests.
Even though I noted a few things in API docs, I really liked the initial proposal so good job! There are some improvements remaining.
core/tools.js
Outdated
* | ||
* @param {Array} array An array to be iterated over. | ||
* @param {Function} fn A function with the signature `(a, index, array) -> b`. | ||
* @param [thisArg=undefined] The context object for `fn`. |
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.
Missing type information.
core/tools.js
Outdated
@@ -1950,6 +1950,40 @@ | |||
return ret; | |||
}, | |||
|
|||
/** | |||
* Returns the first element in the array for which the given callback `fn` returns true. |
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.
true
might be wrapped in backticks, just like undefined
is a line below.
core/tools.js
Outdated
* var array = [ 'foo', 'bar', 'baz', 'faz' ]; | ||
* | ||
* CKEDITOR.tools.array.find( array, function( item ) { | ||
* return item.indexOf( 'az' ) > -1; |
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.
This sample is pretty nice, but uses nasty indexOf
, I'd suggest using even simpler example, with an array of numbers. And find should simply look for a value that is higher than some number.
That would be even simpler than this example.
core/tools.js
Outdated
* ``` | ||
* | ||
* @param {Array} array An array to be iterated over. | ||
* @param {Function} fn A function with the signature `(a, index, array) -> b`. |
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.
JSDuck allows you to explicitly specify callback (fn
in this case) signature. You don't have to do it in a custom way.
See https://github.com/senchalabs/jsduck/wiki/%40param#callback-parameters for more info.
core/tools.js
Outdated
* @param {Array} array An array to be iterated over. | ||
* @param {Function} fn A function with the signature `(a, index, array) -> b`. | ||
* @param [thisArg=undefined] The context object for `fn`. | ||
* @return {*} |
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.
We use @return*s*
tests/core/tools/array.js
Outdated
|
||
assert.isUndefined( ret, 'Undefined should be returned.' ); | ||
|
||
assert.isTrue( CKEDITOR.tools.objectCompare( arr, results ), 'Each array item should be iterated.' ); |
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.
There's a dedicated arrayAssert
object that contains better asserting methods for what you're trying to do here.
tests/core/tools/array.js
Outdated
|
||
assert.isUndefined( ret, 'Undefined should be returned.' ); |
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.
Undefined should be returned
it is not a good practice to put expected value if you use proper assertion.
In this case you used the proper assertion, assert.isUndefined()
which already tells exactly what value is expected. If assertion fails, there is an information that undefined
was expected. And this should be the only source of truth.
Duplicating this information in assertion msg you add a risk that someone will change the method, but will leave msg unchanged.
Simply make it value agnostic, but keep the information what has failed, like: "Proper value is returned" or even simpler as we do in many other places "Returned value".
tests/core/tools/array.js
Outdated
ret = this.array.find( arr, function( item, index ) { | ||
results.push( item ); | ||
|
||
assert.areSame( arr.indexOf( item ), index, 'Index argument should match item index.' ); |
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.
Will fail on IE8.
tests/core/tools/array.js
Outdated
} | ||
}, | ||
|
||
'test array.find': function() { |
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.
Technically you're doing two test cases in one. Please, split it into two clearly defined test cases.
tests/core/tools/manual/find.html
Outdated
@@ -0,0 +1,27 @@ | |||
<style> |
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.
This is purely API addition and should not be tested manually, as it's possible to test it 100% using automatic testing.
a3d7245
to
7132201
Compare
…age. Changed some assertions to use dedicated arrayAssert.
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?
Added new array tool:
CKEDITOR.tools.array.find
Closes #2700