UNPKG

emoji-describer

Version:

An npm package to convert emojis into textual description

386 lines (336 loc) โ€ข 11.5 kB
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; }