npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, πŸ‘‹, I’m Ryan HefnerΒ  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you πŸ™

Β© 2024 – Pkg Stats / Ryan Hefner

emoji-test

v1.1.0

Published

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

Downloads

16

Readme

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)