emoji-describer
Version:
An npm package to convert emojis into textual description
386 lines (336 loc) โข 11.5 kB
JavaScript
import data from './emoji_pretty.js';
/* const emojiDescriptions = {
'๐': 'grinning face',
'๐': 'grinning face with big eyes',
'๐': 'grinning face with smiling eyes',
'๐': 'beaming face with smiling eyes',
'๐': 'grinning squinting face',
'๐
': 'grinning face with sweat',
'๐คฃ': 'rolling on the floor laughing',
'๐': 'face with tears of joy',
'๐': 'slightly smiling face',
'๐': 'upside-down face',
'๐ซ ': 'melting face',
'๐': 'winking face',
'๐': 'smiling face with smiling eyes',
'๐': 'smiling face with halo',
'๐ฅฐ': 'smiling face with hearts',
'๐': 'smiling face with heart-eyes',
'๐คฉ': 'star-struck',
'๐': 'face blowing a kiss',
'๐': 'kissing face',
'๐': 'kissing face with closed eyes',
'๐': 'kissing face with smiling eyes',
'๐ฅฒ': 'smiling face with tear',
'๐': 'face savoring food',
'๐': 'face with tongue',
'๐': 'winking face with tongue',
'๐คช': 'zany face',
'๐': 'squinting face with tongue',
'๐ค': 'money-mouth face',
'๐ค': 'smiling face with open hands',
'๐คญ': 'face with hand over mouth',
'๐ซข': 'face with open eyes and hand over mouth',
'๐ซฃ': 'face with peeking eye',
'๐คซ': 'shushing face',
'๐ค': 'thinking face',
'๐ซก': 'saluting face',
'๐ค': 'zipper-mouth face',
'๐คจ': 'face with raised eyebrow',
'๐': 'neutral face',
'๐': 'expressionless face',
'๐ถ': 'face without mouth',
'๐ซฅ': 'dotted line face',
'๐ถโ๐ซ๏ธ': 'face in clouds',
'๐': 'smirking face',
'๐': 'unamused face',
'๐': 'face with rolling eyes',
'๐ฌ': 'grimacing face',
'๐ฎโ๐จ': 'face exhaling',
'๐คฅ': 'lying face',
'๐': 'relieved face',
'๐': 'pensive face',
'๐ช': 'sleepy face',
'๐คค': 'drooling face',
'๐ด': 'sleeping face',
'๐ท': 'face with medical mask',
'๐ค': 'face with thermometer',
'๐ค': 'face with head-bandage',
'๐คข': 'nauseated face',
'๐คฎ': 'face vomiting',
'๐คง': 'sneezing face',
'๐ฅต': 'hot face',
'๐ฅถ': 'cold face',
'๐ฅด': 'woozy face',
'๐ต': 'face with crossed-out eyes',
'๐ตโ๐ซ': 'face with spiral eyes',
'๐คฏ': 'exploding head',
'๐ค ': 'cowboy hat face',
'๐ฅณ': 'partying face',
'๐ฅธ': 'disguised face',
'๐': 'smiling face with sunglasses',
'๐ค': 'nerd face',
'๐ง': 'face with monocle',
'๐': 'confused face',
'๐': 'worried face',
'๐': 'slightly frowning face',
'โน๏ธ': 'frowning face',
'๐ฎ': 'face with open mouth',
'๐ฏ': 'hushed face',
'๐ฒ': 'astonished face',
'๐ณ': 'flushed face',
'๐ฅบ': 'pleading face',
'๐ฅน': 'face holding back tears',
'๐ฆ': 'frowning face with open mouth',
'๐ง': 'anguished face',
'๐จ': 'fearful face',
'๐ฐ': 'anxious face with sweat',
'๐ฅ': 'sad but relieved face',
'๐ข': 'crying face',
'๐ญ': 'loudly crying face',
'๐ฑ': 'face screaming in fear',
'๐': 'confounded face',
'๐ฃ': 'persevering face',
'๐': 'disappointed face',
'๐': 'downcast face with sweat',
'๐ฉ': 'weary face',
'๐ซ': 'tired face',
'๐ฅฑ': 'yawning face',
'๐ค': 'face with steam from nose',
'๐ก': 'pouting face',
'๐ ': 'angry face',
'๐คฌ': 'face with symbols on mouth',
'๐': 'smiling face with horns',
'๐ฟ': 'angry face with horns',
'๐': 'skull',
'โ ๏ธ': 'skull and crossbones',
'๐ฉ': 'pile of poo',
'๐คก': 'clown face',
'๐ป': 'ghost',
'๐ฝ': 'alien',
'๐พ': 'alien monster',
'๐ค': 'robot',
'๐บ': 'grinning cat',
'๐ธ': 'grinning cat with smiling eyes',
'๐น': 'cat with tears of joy',
'๐ป': 'smiling cat with heart-eyes',
'๐ผ': 'cat with wry smile',
'๐ฝ': 'kissing cat',
'๐': 'weary cat',
'๐ฟ': 'crying cat',
'๐พ': 'pouting cat',
'๐': 'see-no-evil monkey',
'๐': 'hear-no-evil monkey',
'๐': 'speak-no-evil monkey',
'๐': 'kiss mark',
'๐': 'love letter',
'๐': 'heart with arrow',
'๐': 'heart with ribbon',
'๐': 'sparkling heart',
'๐': 'growing heart',
'๐': 'beating heart',
'๐': 'revolving hearts',
'๐': 'two hearts',
'๐': 'heart decoration',
'โฃ๏ธ': 'heart exclamation',
'๐': 'broken heart',
'โค๏ธโ๐ฅ': 'heart on fire',
'โค๏ธโ๐ฉน': 'mending heart',
'โค๏ธ': 'red heart',
'๐งก': 'orange heart',
'๐': 'yellow heart',
'๐': 'green heart',
'๐': 'blue heart',
'๐': 'purple heart',
'๐ค': 'brown heart',
'๐ค': 'black heart',
'๐ค': 'white heart',
'๐ฏ': 'hundred points',
'๐ข': 'anger symbol',
'๐ฅ': 'collision',
'๐ซ': 'dizzy',
'๐ฆ': 'sweat droplets',
'๐จ': 'dashing away',
'๐ณ๏ธ': 'hole',
'๐ฃ': 'bomb',
'๐ค': 'zzz',
'โณ': 'hourglass',
'๐งข': 'billed cap',
'๐ฏ': 'bullseye',
'๐': 'waving hand',
'๐': 'OK hand',
'๐ค': 'pinched fingers',
'๐ค': 'pinching hand',
'โ๏ธ': 'victory hand',
'๐ค': 'crossed fingers',
'๐ค': 'love-you gesture',
'๐ค': 'sign of the horns',
'๐ค': 'call me hand',
'๐': 'backhand index pointing left',
'๐': 'backhand index pointing right',
'๐': 'backhand index pointing up',
'๐': 'middle finger',
'๐': 'backhand index pointing down',
'โ๏ธ': 'index pointing up',
'๐': 'thumbs up',
'๐': 'thumbs down',
'โ': 'raised fist',
'๐': 'oncoming fist',
'๐': 'clapping hands',
'๐': 'raising hands',
'๐ซถ': 'heart hands',
'๐': 'open hands',
'๐คฒ': 'palms up together',
'๐ค': 'handshake',
'๐': 'folded hands',
'๐
': 'nail polish',
'โ๏ธ': 'writing hand',
'๐ช': 'flexed biceps',
'๐ง ': 'brain',
'๐ซ': 'anatomical heart',
'๐ซ': 'lungs',
'๐': 'eyes',
'๐๏ธ': 'eye',
'๐
': 'tongue',
'๐': 'mouth',
'๐ซฆ': 'biting lip',
'๐ถ': 'baby',
'๐
': 'person gesturing NO',
'๐': 'person gesturing OK',
'๐': 'person tipping hand',
'๐': 'person raising hand',
'๐ง': 'deaf person',
'๐': 'person bowing',
'๐คฆ': 'person facepalming',
'๐คท': 'person shrugging',
'๐งโ๐ป': 'technologist',
'๐ผ': 'baby angel',
'๐ฆธ': 'superhero',
'๐ฆน': 'supervillain',
'๐ง': 'fairy',
'๐ง': 'merperson',
'๐ง': 'genie',
'๐ง': 'zombie',
'๐ง': 'troll',
'๐': 'person getting massage',
'๐': 'person getting haircut',
'๐': 'person running',
'๐': 'woman dancing',
'๐บ': 'man dancing',
'๐ฏ': 'people with bunny ears',
'๐ง': 'person in steamy room',
'โจ': 'sparkles',
'โญ๏ธ': 'star',
'๐': 'glowing star',
'โก๏ธ': 'high voltage',
'๐ฅ': 'fire',
'๐': 'gem stone',
'โ๏ธ': 'snowflake',
'๐': 'lemon',
'๐': 'banana',
'๐': 'cherries',
'๐': 'peach',
'๐': 'pineapple',
'๐': 'eggplant',
'๐ฅ': 'avocado',
'๐ถ๏ธ': 'chili pepper',
'๐ฝ': 'corn',
'๐ฎ': 'taco',
'๐': 'helicopter',
'๐ฟ': 'herb',
'๐': 'leaf fluttering in wind',
'๐': 'plug',
'๐': 'lock',
'๐': 'pill',
'๐ช': 'chair',
'๐ฉ': 'triangular flag',
'๐ฆ': 'unicorn',
'๐ฃ': 'fishing pole'
};
const skinToneModifiers = [
'\u{1F3FB}', // Light skin tone
'\u{1F3FC}', // Medium-light skin tone
'\u{1F3FD}', // Medium skin tone
'\u{1F3FE}', // Medium-dark skin tone
'\u{1F3FF}', // Dark skin tone
];
function normalizeEmoji(emoji) {
return skinToneModifiers.reduce((baseEmoji, modifier) => {
return baseEmoji.replace(modifier, '');
}, emoji);
} */
/* export function getEmojiMeaning(emoji) {
emoji = normalizeEmoji(emoji);
if (emojiDescriptions.hasOwnProperty(emoji)) {
return emojiDescriptions[emoji];
} else {
return 'Meaning not found for this emoji.';
}
} */
export function getEmojiMeaning(emoji) {
const emojiHex = emoji.codePointAt(0).toString(16).toUpperCase();
/* console.log(emojiHex); */// Get hexadecimal code of emoji character
const emojiDataEntry = data.find(e => e.unified === emojiHex || e.non_qualified === emojiHex);
return emojiDataEntry ? emojiDataEntry.name : "Emoji not found"; // Return the emoji's name or a default message
}
export function hasEmoji(sentence) {
for (let i = 0; i < sentence.length; i++) {
const char = sentence.charAt(i);
if (char >= '\u{1F300}' && char <= '\u{1F9Ff}') { // Check if the character is in the emoji range
return true;
}
// Check if the character matches any emoji in emojiData
const emojiHex = char.codePointAt(0).toString(16).toUpperCase(); // Get hexadecimal code of emoji character
const emojiExists = data.some(e => e.unified === emojiHex || e.non_qualified === emojiHex);
if (emojiExists) {
return true;
}
}
return false;
}
export function extractEmojis(sentence) {
const emojiRegex = /[\u{1F300}-\u{1F9FF}\u{1F600}-\u{1F64F}\u{1F680}-\u{1F6FF}\u{2600}-\u{26FF}\u{2700}-\u{27BF}]/ug;
// Unicode ranges cover most common emojis, adjust as needed
// Use match to find all emoji characters in the sentence
const emojis = sentence.match(emojiRegex) || [];
return emojis;
}
export function processSentence(sentence) {
let result = '';
let i = 0;
while (i < sentence.length) {
const char = sentence[i];
const codePoint = char.codePointAt(0);
if(getEmojiMeaning(char)!="Emoji not found"){
if(result[result.length-1]===" "){
result+=getEmojiMeaning(char);
}
else{
result += " "+getEmojiMeaning(char);
}
i+=2 ;
continue;
}
if (codePoint >= 0xd800 && codePoint <= 0xdbff && i + 1 < sentence.length) {
const nextChar = sentence[i + 1];
const nextCodePoint = nextChar.codePointAt(0);
if (nextCodePoint >= 0xdc00 && nextCodePoint <= 0xdfff) {
const surrogatePair = char + nextChar;
const emojiMeaning = getEmojiMeaning(surrogatePair);
if(result[result.length-1]===" "){
result+=emojiMeaning;
}
else{
result += " "+emojiMeaning;
}
i += 2;
continue;
}
else {
}
}
result += char;
i++;
}
return result;
}