Skip to content

Instantly share code, notes, and snippets.

@westc
Last active February 7, 2022 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westc/938926a9a7f6f9e1d5d24e6f36f8ec71 to your computer and use it in GitHub Desktop.
Save westc/938926a9a7f6f9e1d5d24e6f36f8ec71 to your computer and use it in GitHub Desktop.
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