This set of questions are related to a project I've published for converting characters, or strings, to Hex based Unicode; eg...
toUnicode.fromCharacter('🍍'); //> 1f34d toUnicode.fromString('Spam!', '0x'); //> ['0x53', '0x70', '0x61', '0x6d', '0x21']
Questions
Are there any mistakes, such as unaccounted for edge-cases?
Have I missed any test cases?
Any suggestions on making the code more readable and/or easier to extend?
Are there any features that are wanted?
Setup and Usage
The Source code is maintained on GitHub and may be cloned via the following commands. A Live demo is hosted online, thanks to GitHub Pages.
mkdir -vp ~/git/hub/javascript-utilities cd ~/git/hub/javascript-utilities git clone [email protected]:javascript-utilities/to-unicode.git
The build target is ECMAScript version 6, and so far both manual tests and automated JestJS tests show that the toUnicode
methods function as intended; for both Browser and NodeJS environments.
Example NodeJS Usage
const toUnicode = require('./to-unicode.js'); var panda_code = toUnicode.fromCharacter('🐼'); console.log(panda_code); //> '1f43c'
Source Code
I am concerned with improving the JavaScript, and TypeScript; ie. HTML is intended to be simple and functional.
'use strict'; /** * Namespace for static methods that convert characters and strings to Unicode */ class toUnicode { /** * Converts character to Hex Unicode * @param {string} character * @return {string} * @author S0AndS0 * @copyright AGPL-3.0 * @example * toUnicode.fromCharacter('🐼'); * //> "1f43c" */ static fromCharacter(character) { return character.codePointAt(undefined).toString(16); } /** * Converts string to character array of Unicode(s) * @param {string} characters * @return {string[]} * @author S0AndS0 * @copyright AGPL-3.0 * @example * toUnicode.fromString('🎉 👋'); * //> [ '1f389', '20', '1f44b' ] */ static fromString(characters, prefix = '') { return [...characters].reduce((accumulator, character) => { const unicode = toUnicode.fromCharacter(character); accumulator.push(`${prefix}${unicode}`); return accumulator; }, []); } } /* istanbul ignore next */ if (typeof module !== 'undefined') { module.exports = toUnicode; }
<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>toUnicode Usage Example</title> <script type="text/javascript" src="assets/js/modules/to-unicode.js" differ></script> <script type="text/javascript" differ> const text_input__callback = (_event) => { const client_input = document.getElementById('client__text--input').value; const client_prefix = document.getElementById('client__text--prefix').value; const output_element = document.getElementById('client__text--output'); const unicode_list = toUnicode.fromString(client_input, client_prefix); console.log(unicode_list); output_element.innerText = unicode_list.join('\n'); }; window.addEventListener('load', () => { const client_text_input = document.getElementById('client__text--input'); const client_text_prefix = document.getElementById('client__text--prefix'); client_text_input.addEventListener('input', text_input__callback); client_text_prefix.addEventListener('input', text_input__callback); }); </script> </head> <body> <span>Prefix: </span> <input type="text" id="client__text--prefix" value="0x"> <br> <span>Input: </span> <input type="text" id="client__text--input" value=""> <pre id="client__text--output"></pre> </body> </html>
JestJS Tests
For completeness here are the JestJS tests.
'use strict'; /** * Tests modules within `to-unicode.js` script * @author S0AndS0 * @copyright AGPL-3.0 */ class toUnicode_Test { constructor(min_code_point = 161, max_code_point = 1114111) { this.toUnicode = require('../to-unicode.js'); this.min_code_point = min_code_point; this.max_code_point = max_code_point; } randomCodePoint() { return Math.random() * (this.max_code_point - this.min_code_point + 1) + this.min_code_point | 0; } runTests() { this.testInvariance(); } /** * Tests if `fromCharacter()` and `fromString()` functions are reversible. */ testInvariance() { const character_code_list = Array(99).fill(0).map((_) => { return this.randomCodePoint(); }); let unicode_list = []; let characters_string = ''; test('Is `fromCharacter()` reversible?', () => { character_code_list.forEach((code_point) => { const character = String.fromCodePoint(code_point); const unicode = this.toUnicode.fromCharacter(character); const decimal = Number(`0x${unicode}`); expect(decimal).toEqual(code_point); unicode_list.push(unicode); characters_string += character; }); }); test('Is `fromString()` reversible?', () => { expect(this.toUnicode.fromString(characters_string)).toStrictEqual(unicode_list); }); } } const test_toUnicode = new toUnicode_Test(); test_toUnicode.runTests();