Skip to content
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

Merged
merged 7 commits into from
Jan 2, 2019
Merged

Added CKEDITOR.tools.array.find #2705

merged 7 commits into from
Jan 2, 2019

Conversation

engineering-this
Copy link
Contributor

@engineering-this engineering-this commented Dec 28, 2018

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

  • Unit tests
  • Manual tests

What changes did you make?

Added new array tool: CKEDITOR.tools.array.find

Closes #2700

@mlewand mlewand self-requested a review December 28, 2018 13:55
@mlewand mlewand self-assigned this Dec 28, 2018
Copy link
Contributor

@mlewand mlewand left a 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`.
Copy link
Contributor

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.
Copy link
Contributor

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;
Copy link
Contributor

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`.
Copy link
Contributor

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 {*}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use @return*s*


assert.isUndefined( ret, 'Undefined should be returned.' );

assert.isTrue( CKEDITOR.tools.objectCompare( arr, results ), 'Each array item should be iterated.' );
Copy link
Contributor

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.


assert.isUndefined( ret, 'Undefined should be returned.' );
Copy link
Contributor

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

ret = this.array.find( arr, function( item, index ) {
results.push( item );

assert.areSame( arr.indexOf( item ), index, 'Index argument should match item index.' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fail on IE8.

}
},

'test array.find': function() {
Copy link
Contributor

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.

@@ -0,0 +1,27 @@
<style>
Copy link
Contributor

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.

@mlewand mlewand merged commit c1837c5 into major Jan 2, 2019
@CKEditorBot CKEditorBot deleted the t/2700 branch January 2, 2019 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants