Skip to content

Instantly share code, notes, and snippets.

@westc
Last active Feb 7, 2022
Embed
What would you like to do?
unicode() - Converts any string into a string of 6-character JS-compliant unicode code points.
/**
* Converts any string into a string of 6-character JS-compliant unicode code
* points.
* @param {string} input
* The input that will be converted to a string of 6-character groups
* representing unicode code points.
* @param {boolean} [asJSON=false]
* Optional, defaults to `false`. Indicates if the returned string will be
* the JSON version of `input`.
* @returns {string}
* A string where each character from `input` will be represented by one or
* more 6-character groups. Each 6-character group will be a string that
* representing a unicode code point (between "\u0000" and "\uFFFF"). If
* `asJSON` is `true` this string will begin and end with a double-quote.
*/
function unicode(input, asJSON=false) {
for (var r = '', i = input.length; i--;) {
r = '\\u' + ('000' + input.charCodeAt(i).toString(16)).slice(-4) + r;
}
return asJSON ? '"' + r + '"' : r;
}
{
// Name of the function responsable for transforming the input into the output.
"transform": "unicode",
// Type of YourJS IO-App
"type": "instant",
// Where to find the code that will be eval'd.
"files": ["unicode.js"]
}
@westc
Copy link
Author

westc commented Feb 7, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment