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

@spotxyz/is-emoji-supported

v0.0.7

Published

Detect if the current device support the specified emoji

Downloads

25

Readme

🦄 is-emoji-supported

is-emoji-supported is a library allowing you to detect if the running device supports the specified emoji.

No dependency license: MIT lint e2e

As of March 2020, the Unicode Standard includes a total of 3 304 emojis. The latest version introduced 117 new ones and vendors have troubles implementing them. In fact there are no operating system supporting all of them. Therefore there is a need to know if a specified emoji isn't supported by the browser to fallback to an image.


🚀 Installation

Install with yarn:

$ yarn add is-emoji-supported

Or install using npm:

$ npm i is-emoji-supported

⏳ Running the tests

$ npm test

📖 Examples

Basic usage

The most basic usage is to use the function directly to detect is the current device support the emoji.

import {isEmojiSupported} from 'is-emoji-supported';

if (isEmojiSupported('🦄')) {
  alert('Houra 🦄 is supported');
} else {
  alert('No support for unicorn emoji yet');
}

Usage with your own cache handler

This library is doing pixel comparison to determine if an emoji is supported. This check can be slow so there is a memory cache implemented. For some reasons you may want to use your own cache implementation to store the result in either localStorage, IndexedDB or anything else for persistent cache. You only need to match the Map interface.

import {setCacheHandler} from 'is-emoji-supported';

const key = 'emoji-cache';
const cache = JSON.parse(localStorage.getItem(key) || {});

setCacheHandler({
  has: (unicode: string) => unicode in cache,
  get: (unicode: string) => cache[unicode],
  set: (unicode: string, supported: boolean) => {
    cache[unicode] = supported;
    localStorage.setItem(key, JSON.stringify(cache));
  }
});

Fallback to images

In most of the cases, you will want to fallback to images to handle unsupported emojis. The best way for this is to build an object with a fallback to all supported images. You can build your own or use the one given by JoyPixel, Twemoji or others services.

import React from 'react';
import {isEmojiSupported} from 'is-emoji-supported';

const emojiMap = {
  '🦄': {
    alt: 'unicorn',
    src: '/images/unicorn.png'
  },
  ...
};

export const Emoji = ({ unicode }) => {
  const attrs = emojiMap[unicode];

  return !attrs ? null : isEmojiSupported(unicode) ? (
    <span role="img" aria-label={attrs.alt}>
      {unicode}
    </span>
  ) : (
    <img {...attrs} />
  );
};