-
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
Hexstring converter to base64 #951
Conversation
core/tools.js
Outdated
* @param {String} hexstring string contained only hexadecimal values, e.g. "0AB7" | ||
* @returns {String} base64 string | ||
*/ | ||
hexstring2base64: function( hexstring ) { |
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.
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".
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.
Use consistent letter casing: hexString
rather than hexstring
.
core/tools.js
Outdated
@@ -1518,6 +1518,71 @@ | |||
}, | |||
|
|||
/** | |||
* Converts hexstring to base64 coode. |
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 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.
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" |
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.
@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 |
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.
Don't be afraid to wrap code-related parts with back ticks, like so: 08A11D8ADA2B
.
tests/core/tools.js
Outdated
}, | ||
{ | ||
hex: '08A11D8ADA2B', | ||
base64: 'CKEditor' |
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.
❤️
@msamsel and please, don't put these dots in a pull request title :D |
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 ) { |
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 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.
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.
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. |
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 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}
.
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.
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. |
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.
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. 🙂
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.
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
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 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.
Credit where it's due! Learn from the master, guys! :)
tests/core/tools.js
Outdated
} | ||
]; | ||
CKEDITOR.tools.array.forEach( testCases, function( test ) { | ||
assert.isTrue( CKEDITOR.tools.arrayCompare( test.bytes, CKEDITOR.tools.convertHexStringToBytes( test.hex ) ) ); |
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 are dedicated assertions for array assertion (arrayAssert
), see arrayAssert.itemsAreEqual
.
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.
Added one commit with API docs adjustments. LGTM.
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.
LGTM 👍
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. |
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.
Base64-encoded string.
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?
Required by #662