emoji-test
TypeScript icon, indicating that this package has built-in type declarations

1.1.0Β β€’Β PublicΒ β€’Β Published

emoji-test

Stable Release gzip size license

Patterns to contruct reliable regular expressions to match emojis in a string. πŸ‘‹πŸ’ΈπŸ’»

Most solutions try to be clever by using character ranges to guess what should be an emoji, but these ranges are often incomplete and are hard to keep up-to-date as the Unicode emoji list changes over time. This package generates regex patterns using an object map of all real emojis, generated using the information extracted from the Emoji source data.

When new updates are released by Unicode, this library can easily generate a new map object and cut new releases quickly and efficiently.

The tradeoff with this approach is a slightly larger bundle in exchange for reliability.

This package is a fork of tonton-pixel/emoji-test-patterns, which was designed for use in Node. emoji-test can be used in Node or the browser.

Notes

  • Providing patterns as strings instead of regular expressions does require the extra step of using new RegExp () to actually make use of them, but it has two main advantages:
    • Flags can be set differently depending on how the patterns are used. In any case, the regular expressions must include the 'u' flag, since the patterns make use of the new type of Unicode escape sequences: \u{1F4A9}.
    • The patterns can be further modified before being turned into regular expressions; for instance, the pattern can be embedded in a larger one (see examples below).

Installation

Navigate to your project directory and run

npm install emoji-test
# or
yarn add emoji-test

Testing

A basic test can be performed by running the following command from the package directory:

yarn test

Examples

Testing whether an emoji is fully-qualified (keyboard) or non-fully-qualified (display)

import emojiPatterns from "emoji-test";
let emojiKeyboardRegex = new RegExp("^" + emojiPatterns.keyboard + "$", "gu");
console.log(emojiKeyboardRegex.test("❀️")); // true!
console.log(emojiKeyboardRegex.test("πŸ•·")); // false!

Extracting all emojis from a string

import emojiPatterns from "emoji-test";
let emojiAllRegex = new RegExp(emojiPatterns.all, "gu");
console.log(
	"AaĀā#*0β€πŸ‡¦ζ„›ηˆ±β€οΈμ• πŸ’œ πŸ‡¨πŸ‡¦πŸ‡«πŸ‡·πŸ‡¬πŸ‡§πŸ‡―πŸ‡΅πŸ‡ΊπŸ‡Έ πŸ‘ͺβ¬ŒπŸ‘¨β€πŸ‘©β€πŸ‘¦ πŸ’‘β¬ŒπŸ‘©β€β€οΈβ€πŸ‘¨ πŸ’β¬ŒπŸ‘©β€β€οΈβ€πŸ’‹β€πŸ‘¨".match(emojiPatterns)
);
// ["❀","❀️","πŸ’œ","πŸ‡¨πŸ‡¦","πŸ‡«πŸ‡·","πŸ‡¬πŸ‡§","πŸ‡―πŸ‡΅","πŸ‡ΊπŸ‡Έ","πŸ‘ͺ","πŸ‘¨β€πŸ‘©β€πŸ‘¦","πŸ’‘","πŸ‘©β€β€οΈβ€πŸ‘¨","πŸ’","πŸ‘©β€β€οΈβ€πŸ’‹β€πŸ‘¨"]

Extracting all fully-qualified (keyboard) emoji from a string

import emojiPatterns from "emoji-test";
let emojiAllRegex = new RegExp(emojiPatterns.all, "gu");
let emojiKeyboardRegex = new RegExp("^" + emojiPatterns.keyboard + "$", "u");
let emojiList = "AaĀā#*0β€πŸ‡¦ζ„›ηˆ±β€οΈμ• πŸ’œ πŸ‡¨πŸ‡¦πŸ‡«πŸ‡·πŸ‡¬πŸ‡§πŸ‡―πŸ‡΅πŸ‡ΊπŸ‡Έ πŸ‘ͺβ¬ŒπŸ‘¨β€πŸ‘©β€πŸ‘¦ πŸ’‘β¬ŒπŸ‘©β€β€οΈβ€πŸ‘¨ πŸ’β¬ŒπŸ‘©β€β€οΈβ€πŸ’‹β€πŸ‘¨".match(
	emojiAllRegex
);
if (emojiList) {
	emojiList = emojiList.filter((emoji) => emojiKeyboardRegex.test(emoji));
}
console.log(emojiList);
// ["❀️","πŸ’œ","πŸ‡¨πŸ‡¦","πŸ‡«πŸ‡·","πŸ‡¬πŸ‡§","πŸ‡―πŸ‡΅","πŸ‡ΊπŸ‡Έ","πŸ‘ͺ","πŸ‘¨β€πŸ‘©β€πŸ‘¦","πŸ’‘","πŸ‘©β€β€οΈβ€πŸ‘¨","πŸ’","πŸ‘©β€β€οΈβ€πŸ’‹β€πŸ‘¨"]

Removing all emoji from a string

import emojiPatterns from "emoji-test";
let emojiAllRegex = new RegExp(emojiPatterns.all, "gu");
console.log(
	"AaĀā#*0β€πŸ‡¦ζ„›ηˆ±β€οΈμ• πŸ’œ πŸ‡¨πŸ‡¦πŸ‡«πŸ‡·πŸ‡¬πŸ‡§πŸ‡―πŸ‡΅πŸ‡ΊπŸ‡Έ πŸ‘ͺβ¬ŒπŸ‘¨β€πŸ‘©β€πŸ‘¦ πŸ’‘β¬ŒπŸ‘©β€β€οΈβ€πŸ‘¨ πŸ’β¬ŒπŸ‘©β€β€οΈβ€πŸ’‹β€πŸ‘¨".replace(emojiAllRegex, "")
);
// "AaĀā#*0πŸ‡¦ζ„›ηˆ±μ•   ⬌ ⬌ ⬌"

License

The MIT License (MIT)

Readme

Keywords

none

Package Sidebar

Install

npm i emoji-test

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

1.76 MB

Total Files

16

Last publish

Collaborators

  • chancestrickland