Yet Another Escape To Unicode

Posted at

Preserve the full-meaning of the string, without encoding or breaking Unicode-pairs.                =;
String.prototype.to_char_code       = function(){ return (this+"").map(function(c){                return c.charCodeAt(0)                                                  });           };
String.prototype.escape_to_unicode  = function(){ return (this+"").to_char_code().map(function(i){ return "\\u005Cu" + ("0000" + i.toString(16).toUpperCase()).substr(-4);  }).join("");  };

//test- run:   "שh🐀".escape_to_unicode();

another variation is using regular-expressing with attention to Unicode, meaning /u,
but it won't work for you since you, still, might get a Unicode-pair (where you have to run charCodeAt(0) and charCodeAt(1).. etc...). Split by character (I'm using, but it is the same as using split("").map(...) will work around that, keeping the character whole (meaning not like the Unicode/ASCII-escape trick) but still breaking nicely characters, including breaking Unicode-pairs-
-to quickly get the \\u.. equivalent of your string. :]