Created
June 10, 2016 14:43
-
-
Save nesk/7b4b4824e5af84c5048cd52f2fdbc5be to your computer and use it in GitHub Desktop.
Interpreting Unicode values and writing them to a string to render the corresponding Emoji
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on this amazing article: | |
// http://crocodillon.com/blog/parsing-emoji-unicode-in-javascript | |
function findSurrogatePair(point) { | |
var offset = point - 0x10000, | |
lead = 0xd800 + (offset >> 10), | |
trail = 0xdc00 + (offset & 0x3ff); | |
return [lead.toString(16), trail.toString(16)]; | |
} | |
var WOMAN_KISS_WOMAN_UNICODE_VALUES = "1F469-200D-2764-FE0F-200D-1F48B-200D-1F469"; | |
var output = WOMAN_KISS_WOMAN_UNICODE_VALUES.split('-').map(function(codepoint) { | |
codepoint = parseInt(codepoint, 16); | |
if (codepoint <= 0xffff) { | |
return String.fromCharCode(codepoint); | |
} else { | |
return findSurrogatePair(codepoint).reduce(function(string, surrogate) { | |
return string + String.fromCharCode(parseInt(surrogate, 16)); | |
}, ''); | |
} | |
}).join(''); | |
console.log(output); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment