Reputation: 31
Is there any function or something that I can use to convert an emoji or something into the Javascript Encoding equivalent? For example, if I have a variable that contains "👍", I want to be able to convert it to its corresponding javascript encoding, like "\ud83d\udc4d". Any way this can be accomplished?
Upvotes: 2
Views: 2269
Reputation: 1
function emojiToUnicodeEscape(emoji) {
return emoji
.split('')
.map(char => "\\u" + char.charCodeAt(0).toString(16))
.join('');
}
const myEmoji = "😀";
const unicodeEscapeSequence = emojiToUnicodeEscape(myEmoji);
console.log(unicodeEscapeSequence);
Upvotes: 0
Reputation: 2528
You can use punycode library.npm i punycode
or visit https://www.npmjs.com/package/punycode
for example.
console.log(punycode.ucs2.decode('👍')); // to get corresponding like "\ud83d\udc4d"code
and
console.log('\ud83d\udc4d')
to get corresponding image/emoji;
You can visit http://speakingjs.com/es5/ch24.html and https://flaviocopes.com/javascript-unicode/ for more information.
You can have refrence from
Here here is the example.
<p>I will display ☺</p>
<p>I will display ☺</p>
Upvotes: 1