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

Hexstring converter to base64 #951

Merged
merged 7 commits into from
Sep 26, 2017
Merged

Hexstring converter to base64 #951

merged 7 commits into from
Sep 26, 2017

Conversation

msamsel
Copy link
Contributor

@msamsel msamsel commented Sep 21, 2017

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?

  • Add tools for converting hexstrings to base64.

Required by #662

@msamsel msamsel changed the base branch from major to t/662 September 21, 2017 11:10
@mlewand mlewand self-requested a review September 21, 2017 12:40
core/tools.js Outdated
* @param {String} hexstring string contained only hexadecimal values, e.g. "0AB7"
* @returns {String} base64 string
*/
hexstring2base64: function( hexstring ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

When picking a name, check the surrounding names. Any other convert methods uses convertXyzToXyz so you're missing convert prefix and it should be "to" rather than "2".

Copy link
Contributor

Choose a reason for hiding this comment

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

Use consistent letter casing: hexString rather than hexstring.

core/tools.js Outdated
@@ -1518,6 +1518,71 @@
},

/**
* Converts hexstring to base64 coode.
Copy link
Contributor

Choose a reason for hiding this comment

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

This markdown does not get converted at all:
image
Take the time and set up the ckeditor-docs builder, and preview any docs changes before pushing the code.

Copy link
Contributor

Choose a reason for hiding this comment

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

Reason for the above is probably no new line before the ordered list.

core/tools.js Outdated
* decimal: 2 10 4 29 34 45 40 43 -> base64: "CKEditor"
*
* @since 4.8.0
* @param {String} hexstring string contained only hexadecimal values, e.g. "0AB7"
Copy link
Contributor

Choose a reason for hiding this comment

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

@param and @returns contain full sentences. So start with a capital letter and end with a dot.

core/tools.js Outdated
* 5. If there was added extra zeros bytes fill them with '=' sign.
*
* Example:
* hex: 08A11D8ADA2B -> binary: 0000 1000 1010 0001 0001 1101 1000 1010 1101 1010 0010 1011
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't be afraid to wrap code-related parts with back ticks, like so: 08A11D8ADA2B.

},
{
hex: '08A11D8ADA2B',
base64: 'CKEditor'
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

@mlewand
Copy link
Contributor

mlewand commented Sep 21, 2017

@msamsel and please, don't put these dots in a pull request title :D

@msamsel msamsel changed the title Hexstring converter to base64. Hexstring converter to base64 Sep 22, 2017
core/tools.js Outdated
* @param {String} hexString String contained only hexadecimal values, e.g. `0AB7`.
* @returns {String} Base64 string represnet input value.
*/
convertHexstringToBase64: function( hexString ) {
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 one more thing, I thought that I have included it in original review, but turns out I forgot to paste it in review summary.

Rather than adding convertHexstringToBase64 which does the job for your particular purpose, you should do two separate function:

  • convertHexStringToBytes
  • convertBytesToBase64

That way this code will be more reusable, and can be used by a developer that looks only to use bytes to base64 conversion, etc.

Copy link
Contributor

@f1ames f1ames left a comment

Choose a reason for hiding this comment

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

One tiny detail to fix and we are ready to go.

core/tools.js Outdated
*
* @since 4.8.0
* @param bytesArray Array which stores 1 byte in each cell as Integer number.
* @returns Base64 string which represents input bytes.
Copy link
Contributor

Choose a reason for hiding this comment

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

There is no such type as bytesArray or Base64, it should probably be Array and String. You can include more information (that it is array of bytes / string in base64 format) in the @param/@returns description. Also remember about brackets when defining type - {Array}.

@mlewand mlewand self-requested a review September 26, 2017 13:11
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.

Two minor changes and we're ready to go.

core/tools.js Outdated
/**
* Convert bytes array into base64 code.
*
* Bytes are `8bit` numbers, where base64 use `6bit` to store data. That's why we process 3 Bytes into 4 characters representing base64.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this information in any way needed for the person using this API? I think not - this is an implementation detail of base64 and are not relevant for the person looking for this method.

Same goes for the "Algorithm" part. It's great that you have added it, but you did simply in a wrong place.

You don't want to put all of this information in our API docs, as there should be just summary. Instead it should be added as a inline comment insde the function, so that only programmers who work on this internals know it.

So I'll ask you to just do that, and we'll keep this great value of having these comments. 🙂

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed, first of all - kudos for explaining this in details, great job.

However, I'd recommend moving the detailed explanation of the algorithm to (internal) comments and leaving the public API docs more straightforward/basic. A nice example how we usually do it is the Magicline plugin with excellent comments by @oleq: https://github.com/ckeditor/ckeditor-dev/blob/major/plugins/magicline/plugin.js#L851

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Credit where it's due! Learn from the master, guys! :)

}
];
CKEDITOR.tools.array.forEach( testCases, function( test ) {
assert.isTrue( CKEDITOR.tools.arrayCompare( test.bytes, CKEDITOR.tools.convertHexStringToBytes( test.hex ) ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

There are dedicated assertions for array assertion (arrayAssert), see arrayAssert.itemsAreEqual.

@mlewand mlewand self-requested a review September 26, 2017 14:28
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.

Added one commit with API docs adjustments. LGTM.

@mlewand mlewand requested a review from f1ames September 26, 2017 14:30
Copy link
Contributor

@f1ames f1ames left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@f1ames f1ames merged commit 3ddfe4c into t/662 Sep 26, 2017
@f1ames f1ames deleted the t/662-2972 branch September 26, 2017 16:06
core/tools.js Outdated
*
* @since 4.8.0
* @param {Array} bytesArray An array which stores 1 byte in each cell as Integer number.
* @returns {String} Base64 string which represents input bytes.
Copy link
Contributor

Choose a reason for hiding this comment

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

Base64-encoded string.

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.

5 participants