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.
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.
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.
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
.