emoji-unicode
Version:
Get the unicode code of an emoji.
52 lines (47 loc) • 1.5 kB
JavaScript
/**
* emojiUnicode
* Get the unicode code of an emoji in base 16.
*
* @name emojiUnicode
* @function
* @param {String} input The emoji character.
* @returns {String} The base 16 unicode code.
*/
function emojiUnicode(input) {
return emojiUnicode.raw(input).split(' ').map(function (val) {
return parseInt(val).toString(16);
}).join(' ');
}
/**
* emojiunicode.raw
* Get the unicode code points of an emoji in base 16.
*
* @name emojiunicode.raw
* @function
* @param {String} input The emoji character.
* @returns {String} The unicode code points.
*/
emojiUnicode.raw = function (input) {
if (input.length === 1) {
return input.charCodeAt(0).toString();
} else if (input.length > 1) {
var pairs = [];
for (var i = 0; i < input.length; i++) {
if (
// high surrogate
input.charCodeAt(i) >= 0xd800 && input.charCodeAt(i) <= 0xdbff) {
if (input.charCodeAt(i + 1) >= 0xdc00 && input.charCodeAt(i + 1) <= 0xdfff) {
// low surrogate
pairs.push((input.charCodeAt(i) - 0xd800) * 0x400 + (input.charCodeAt(i + 1) - 0xdc00) + 0x10000);
}
} else if (input.charCodeAt(i) < 0xd800 || input.charCodeAt(i) > 0xdfff) {
// modifiers and joiners
pairs.push(input.charCodeAt(i));
}
}
return pairs.join(' ');
}
return '';
};
module.exports = emojiUnicode;
;