Skip to content

Query the next or previous emoji using a code point-based trie 🌳

License

Notifications You must be signed in to change notification settings

codex-src/emoji-trie.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation



emoji-trie.js 🌳✨




emoji-trie.js makes querying the next or previous emoji in a string possible — that’s 4,168 emojis as of Unicode 13.0! This package does not rely on regex — emoji-trie.js uses two internal data structures that hierarchically map all emoji code points in both descending and ascending order, enabling querying for emojis forwards and backwards.

Installation

You can import this package like so:

yarn add https://github.com/codex-src/emoji-trie.js or npm add ...

This package is not currently published to NPM.

import * as emojiTrie from "emoji-trie"
// Or import { atStart, atEnd } from "emoji-trie"

emojiTrie.atStart("🧜🏻‍♀️ 🎵 Under the sea! 🎵")
// {
//   "group": "People & Body",
//   "subgroup": "person-fantasy",
//   "codePoints": [
//     129500,
//     127995,
//     8205,
//     9792,
//     65039
//   ],
//   "status": "fully-qualified",
//   "emoji": "🧜🏻‍♀️",
//   "tag": "E5.0",
//   "description": "mermaid: light skin tone"
// }

Usage with CommonJS modules has not been tested nor is guaranteed to work.

How does emoji-trie.js work?

First, let’s answer the question, What is a trie?

In computer science, a trie, also called digital tree or prefix tree, is a kind of search tree—an ordered tree data structure used to store a dynamic set or associative array where the keys are usually strings.

Wikipedia

And in simpler terms: A deeply nested hash map.

Time for an example! Here’s a slice of one of the internal data structures emoji-trie.js uses to query an emoji:

{
  129500: {
    127995: {
      8205: {
        9792: {
          65039: {
            match: {
              group: "People & Body",
              subgroup: "person-fantasy",
              codePoints: [
                129500,
                127995,
                8205,
                9792,
                65039
              ],
              status: "fully-qualified",
              emoji: "🧜🏻‍♀️",
              tag: "E5.0",
              description: "mermaid: light skin tone"
            }
          }
        }
      }
    }
  }
}

Can you see what’s going on here? Every emoji can be dissected into its constituent code points, and emojis can be anywhere from 1-8 code points (as of Unicode 13.0). So how emojiTrie.atStart works is by traversing the descending order data structure for a match, and emojiTrie.atEnd works inversely; by traversing the ascending order data structure for a match.

About

Query the next or previous emoji using a code point-based trie 🌳

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published