Skip to content

sinansonmez/easy-emojis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

50 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

easy-emojis

A comprehensive TypeScript library for emoji utilities including intelligent text-to-emoji replacement, fast search, flag conversion, and emoji lookup functions.

npm version npm downloads License

Features

โœจ Smart Text Replacement โ€“ Convert natural language to emojis with contextual awareness
๐Ÿ”Ž Emoji Search (new!) โ€“ Ranked results with exact/partial matching, category filters, limits
๐Ÿ” Emoji Lookup โ€“ Find emojis by name, shortname, or get random selections
๐Ÿณ๏ธ Flag Conversion โ€“ Convert between country codes and flag emojis
๐Ÿ”ค Letter Conversion โ€“ Transform letters to regional indicator emojis
โšก High Performance โ€“ Optimized algorithms with configurable options
๐ŸŽฏ TypeScript Support โ€“ Full type definitions

Installation

# npm
npm install easy-emojis

# yarn
yarn add easy-emojis

# pnpm
pnpm add easy-emojis

Quick Start

import * as EasyEmojis from 'easy-emojis';

// Lookup
EasyEmojis.getEmoji('pizza');        // ๐Ÿ•
EasyEmojis.getEmoji(':coffee:');     // โ˜•

// NEW: Search
EasyEmojis.searchEmojis('apple')[0]?.emoji.emoji; // ๐ŸŽ (top match)

// Flags & Letters
EasyEmojis.countryCodeToFlag('JP');  // ๐Ÿ‡ฏ๐Ÿ‡ต
EasyEmojis.letterToEmoji('S');       // ๐Ÿ‡ธ

๐Ÿ”Ž New: Emoji Search

Ranked search with optional category filtering, strict (exact) mode, and result limits.

Basic Usage

import { searchEmojis } from 'easy-emojis';

const results = searchEmojis('apple');
/*
[
  { emoji: { ...๐ŸŽ }, score: 100, matchType: 'exact' },
  { emoji: { ...๐Ÿ }, score:  78, matchType: 'partial' },
  ...
]
*/

// Just the emoji characters:
const chars = results.map(r => r.emoji.emoji); // ["๐ŸŽ","๐Ÿ",...]

Shortnames & Names

  • Searches match names (e.g., "red apple") and shortnames (without colons), e.g. "apple".
  • Exact matches on name/shortname score 100.
searchEmojis('tada');  // treats shortname; exact if it matches ๐ŸŽ‰

Exact Match Mode

searchEmojis('red apple', { exactMatch: true });
// returns only exact name/shortname matches (score = 100)

Category Filtering

import { getSearchCategories, searchEmojis } from 'easy-emojis';

const categories = getSearchCategories(); // ["People & Body (family)", "Food & Drink (food-sweet)" ...]
const food = searchEmojis('cake', { category: 'Food & Drink (food-sweet)' });

Tip: Use getSearchCategories() to present a category dropdown and pass the selected value to searchEmojis.

Limiting Results

searchEmojis('face', { limit: 10 });

Scoring & Sorting (How results are ranked)

  • Exact match (name or shortname) โ†’ score = 100, matchType: 'exact'.
  • Partial match โ†’ score โˆˆ [60..90], higher if the match occurs earlier in the text.
    • Name partials are prioritized over shortname partials.
  • Secondary sort by shorter name length to bias simpler names when scores tie.

Error Handling

  • Empty or whitespaceโ€only queries throw: Error("Search query cannot be empty").

Emoji Lookup

import * as EasyEmojis from 'easy-emojis';

// Get random emoji
EasyEmojis.getRandomEmoji(); // e.g. "๐ŸŽ‰"

// Lookup by name or shortname
EasyEmojis.getEmojiByName('red apple'); // "๐ŸŽ"
EasyEmojis.getEmojiByShortName(':apple:'); // "๐ŸŽ"

// Universal lookup (accepts both)
EasyEmojis.getEmoji('red apple'); // "๐ŸŽ"
EasyEmojis.getEmoji(':apple:');   // "๐ŸŽ"

Flag & Letter Conversion

import * as EasyEmojis from 'easy-emojis';

EasyEmojis.countryCodeToFlag('US'); // "๐Ÿ‡บ๐Ÿ‡ธ"
EasyEmojis.flagToCountryCode('๐Ÿ‡บ๐Ÿ‡ธ'); // "US"

EasyEmojis.letterToEmoji('S'); // "๐Ÿ‡ธ"
EasyEmojis.emojiToLetter('๐Ÿ‡ธ'); // "S"

๐Ÿš€ Smart Text Replacement

Transform your text with intelligent emoji replacements! Perfect for social media, chat apps, and creative content.

Basic Usage

import { replaceTextWithEmojis } from 'easy-emojis';

const result = replaceTextWithEmojis("I love pizza and coffee!");
console.log(result.text); // "I โค๏ธ ๐Ÿ• and โ˜•!"

Replacement Presets

import {
  replaceTextSubtle,     // Conservative
  replaceTextBalanced,   // Moderate
  replaceTextExpressive, // Aggressive
  replaceTextSmart       // Context-aware
} from 'easy-emojis';

const text = "Having a great morning with coffee and friends!";

replaceTextSubtle(text);     // "Having a great morning with โ˜• and friends!"
replaceTextBalanced(text);   // "Having a great morning with โ˜• and friends!"
replaceTextExpressive(text); // "Having a great ๐ŸŒ… with โ˜• and friends!"
replaceTextSmart(text);      // Context-aware replacements based on sentiment

Advanced Configuration

import { replaceTextWithEmojis, EmojiReplaceMode } from 'easy-emojis';

const result = replaceTextWithEmojis("I love programming and pizza", {
  mode: EmojiReplaceMode.MODERATE,
  confidenceThreshold: 0.8,
  maxReplacements: 3,
  categories: ['food', 'emotion', 'tech'],
  customMappings: {
    programming: { emoji: '๐Ÿ’ป', confidence: 0.9, category: 'tech' }
  }
});

console.log(result.text); // "I โค๏ธ๐Ÿ’ป and ๐Ÿ•"
console.log(result.stats);
// {
//   totalReplacements: 3,
//   averageConfidence: 0.9,
//   categoriesUsed: ['emotion', 'tech', 'food']
// }

Replacement Modes Explained

Mode Description Use Case
CONSERVATIVE High-confidence, minimal replacements Professional communication
MODERATE Balanced approach with good coverage Social media posts
AGGRESSIVE Maximum replacements, lower threshold Creative content, casual chat
CONTEXTUAL AI-powered context analysis Smart assistants, adaptive UX

Configuration Options

interface EmojiReplaceOptions {
  mode: EmojiReplaceMode;
  confidenceThreshold: number;
  maxReplacements?: number;
  preserveCase?: boolean;
  categories?: string[];
  customMappings?: object;
}

Return Value

interface EmojiReplaceResult {
  text: string;
  replacements: EmojiReplacement[];
  originalText: string;
  stats: {
    totalReplacements: number;
    averageConfidence: number;
    categoriesUsed: string[];
  };
}

API Reference

Core

Function Description Example
getRandomEmoji() Returns a random emoji ๐ŸŽฒ
getEmojiByName(name) Find emoji by name getEmojiByName('pizza') โ†’ ๐Ÿ•
getEmojiByShortName(shortname) Find by shortname getEmojiByShortName(':pizza:') โ†’ ๐Ÿ•
getEmoji(query) Universal lookup (name or shortname) getEmoji('pizza') โ†’ ๐Ÿ•

Search (new)

Function Description Example
searchEmojis(query, options?) Ranked search across names & shortnames searchEmojis('apple', { limit: 10 })
getSearchCategories() List available categories (sorted) getSearchCategories() โ†’ string[]

SearchOptions

type SearchOptions = {
  limit?: number;       // default 50
  category?: string;    // exact category string
  exactMatch?: boolean; // only exact matches when true
};

SearchResult

type SearchResult = {
  emoji: Emoji;
  score: number;                 // 100 exact; 60โ€“90 partial
  matchType: 'exact' | 'partial';
};

Conversion

Function Description Example
countryCodeToFlag(code) Country code โ†’ flag countryCodeToFlag('JP') โ†’ ๐Ÿ‡ฏ๐Ÿ‡ต
flagToCountryCode(flag) Flag โ†’ country code flagToCountryCode('๐Ÿ‡ฏ๐Ÿ‡ต') โ†’ JP
letterToEmoji(letter) Letter โ†’ regional indicator letterToEmoji('A') โ†’ ๐Ÿ‡ฆ
emojiToLetter(emoji) Regional indicator โ†’ letter emojiToLetter('๐Ÿ‡ฆ') โ†’ A

Text Replacement

Function Description
replaceTextWithEmojis(text, options) Advanced replacement with full control
replaceTextSubtle(text) Conservative preset
replaceTextBalanced(text) Moderate preset
replaceTextExpressive(text) Aggressive preset
replaceTextSmart(text) Context-aware preset

Real-World Examples

Typeahead / Autocomplete with Search

import { searchEmojis } from 'easy-emojis';

function suggest(query: string) {
  if (!query.trim()) return [];
  return searchEmojis(query, { limit: 8 }).map(r => ({
    char: r.emoji.emoji,
    name: r.emoji.name,
    shortname: r.emoji.shortname,
    score: r.score
  }));
}

Social Media Enhancement

const post = "Just finished an amazing workout! Time for coffee and relaxation.";
const enhanced = replaceTextBalanced(post);
// "Just finished an amazing workout! Time for โ˜• and relaxation."

Chat Application

const message = "Good morning! Having breakfast - eggs and coffee";
const fun = replaceTextExpressive(message);
// "๐ŸŒ…! Having breakfast - ๐Ÿฅš and โ˜•"

Email Subject Lines

const subject = "Meeting tomorrow - pizza lunch provided";
const professional = replaceTextSubtle(subject);
// "Meeting tomorrow - ๐Ÿ• lunch provided"

Performance

  • โšก Fast: O(N) scoring over the (optionally filtered) dataset, plus a single sort on the result set
  • ๐Ÿ”„ Memory efficient: Smart data shapes and minimal overhead
  • ๐Ÿ“ฆ Lightweight: Tree-shakeable ES modules
  • ๐ŸŽฏ Scalable: Handles large texts efficiently

Development Setup

# Clone the repository
git clone https://github.com/sinansonmez/easy-emojis.git

# Install dependencies
npm install

# Run tests
npm run test

# Build the project
npm run build

Running Tests

npm run test

License

MIT ยฉ Sinan Chaush


Changelog

  • vNext โ€“ Added searchEmojis() and getSearchCategories() with ranked results, exact/partial matching, category filters, and result limits.

Packages

 
 
 

Contributors

โšก