Skip to content

Instantly share code, notes, and snippets.

@leodutra
Last active December 27, 2015 17:29
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 leodutra/7362927 to your computer and use it in GitHub Desktop.
Save leodutra/7362927 to your computer and use it in GitHub Desktop.
Text to HTML char entities.
function toCharEntity(str, useHexa) {
if (str !== undefined && str !== null) {
str = '' + str;
var codeCache;
var res = '';
for(var i = 0, l = str.length; i < l ;) {
res += '&#';
codeCache = str.charCodeAt(i++);
res += useHexa ? 'x' + codeCache.toString('16') : codeCache;
res += ';';
}
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment