• Stars
    star
    1,417
  • Rank 32,991 (Top 0.7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A JS Emoji conversion library

js-emoji - Display emoji in the browser, everywhere

NPM version NPM downloads Build Status Coverage Status

Modern computers and phones allow the display and input of emoji, but you often want to display them on older devices, or in the browser. This library converts emoji (either from character codes or colon-sequences like :smile:) into something that will work on the host computer - either native character codes, a CSS styled span or a text representation.

Installation

Either clone the git repo, or npm install emoji-js

Browser Usage

<link href="emoji.css" rel="stylesheet" type="text/css" />
<script src="emoji.js" type="text/javascript"></script>
<script type="text/javascript">

var emoji = new EmojiConvertor();

// replaces \u{1F604} with platform appropriate content
var output1 = emoji.replace_unified(input);

// replaces :smile: with platform appropriate content
var output2 = emoji.replace_colons(input);

// convert colons explicitly to unicode
emoji.replace_mode = 'unified';
emoji.allow_native = true;
var output3 = emoji.replace_colons(input);

</script>

You can view a live demo here.

Node Usage

After installing the package via npm install emoji-js:

var EmojiConvertor = require('emoji-js');

var emoji = new EmojiConvertor();

console.log(emoji.replace_colons("Hello :smile:"));

Output control

There are many options to control the format of the replacement, although the defaults should work well on all platforms. There are two overrides which ignore all other replacement-mode preferences:

  • emoji.text_mode = true - force text output mode, e.g. smile (default false)
  • emoji.colons_mode = true - force colon output mode, e.g. :smile: (default: false)

After that, the mode is determined automatically by examining the environment and determining capabilities. You can introspect the auto-detected mode by checking emoji.replace_mode, which can have the following values:

  • unified - Output Unicode code points
  • softbank - Output legacy Softbank/iOS code points
  • google - Output legacy Android code points
  • css - Output HTML images, using <span> elements with CSS background images
  • img - Output HTML images, using <img> elements

You can explicitly override the emoji.replace_mode to any of the above values. There are a few options which determine how the emoji.replace_mode value is used at run-time:

  • emoji.allow_native = true - Allow output of code points (default: true, otherwise falls back to css or img mode)
  • emoji.use_sheet = true - Use spritesheets with CSS positioning, instead of individual images (default: false, only applies in css mode)
  • emoji.use_css_imgs = true - Use individual CSS classes for each emoji, rather than inlining the positioning (default: false, only applies in css mode, requires the CSS file to be loaded)
  • emoji.avoid_ms_emoji = true - For browsers on Windows, don't allow native code points (because they look awful) (default: true)

There are also some further options that change the nature of the output under various modes:

  • emoji.wrap_native = true - Wrap native code points in <span class="emoji-native"></span> to allow styling (default: false, only applies in native, google and softbank modes)
  • emoji.include_title = true - Set the "title" property on the <span> or <img> tag to the short-name, e.g. :smile: (default: false, only applies in css and img modes)
  • emoji.include_text = true - Set the text inside the <span> tag to the short-name, e.g. :smile: (default: false, only applies in css mode)

Images

The library supports using multiple image sets, which can be selected using the emoji.img_set property. Valid values are:

  • apple (default)
  • google
  • twitter
  • facebook
  • messenger

This value is used as a lookup in the emoji.img_sets property, which defines each set. By default, it assumes your images are under the path /emoji-data/, but you can override these values:

emoji.img_sets.apple.path = 'http://my-cdn.com/emoji-apple-64/';
emoji.img_sets.apple.sheet = 'http://my-cdn.com/emoji-apple-sheet-64.png';

The .path property, the directory containing individual images, must end in a trailing slash. The .sheet property points directly to a spritesheet. The images can be found in the emoji-data repository: https://github.com/iamcal/emoji-data

Make sure you use the same version of the images that this library was built with, otherwise spritesheets will not work, and some images may be wrong or missing!

If you need to cache-bust your images, you can use the following property:

emoji.img_suffix = '?foo';

This will cause the generated URLs to have ?foo appended (default: '').

Further options

If you wish to allow :SMILE: to work the same as :smile:, you can set emoji.allow_caps = true (default: false)

You can add your own emoji aliases, even overriding built-in emoji:

emoji.addAliases({
  'doge' : '1f415',
  'cat'  : '1f346'
});

You can then remove your custom aliases, which will also reset built-in emoji back to their original state:

emoji.removeAliases([
  'doge',
  'cat',
]);

Upgrading from 1.x or 2.x

Prior to version 3.0, the emoji.js library would instantiate a global object called emoji, which you would call methods on. In versions 3.0 and later, the library exposes a single class called EmojiConvertor which needs to be instantiated manually. To upgrade old code, simply add this line in a global context:

var emoji = new EmojiConvertor();

Lifecycle

The library is designed to be used with the following flow:

  1. User enters text on a modern device, containing native emoji
  2. Data is stored by application, optionally translated to :colon: style
  3. When data is viewed by users on iPhone, Mac or Android phone, emoji appear natively
  4. When data is viewed on older devices, emoji are replaced with inline <span> elements with background images or simple images.

While the JS library can replace native emoji codepoints, it's significantly slower than replacing colon sequences. By translating to and storing colon sequences on the backend, you are able to:

  • Support older Android phones (Google emoji codepoints)
  • Support older iPhones (Softbank emoji codepoints)
  • Allow users to enter :smile: and have it appear as an emoji everywhere

Using MySQL for storage

You don't need to worry about this if you translate to colon syntax before storage.

Some special care may be needed to store emoji in your database. While some characters (e.g. Cloud, U+2601) are within the Basic Multilingual Plane (BMP), others (e.g. Close Umbrella, U+1F302) are not. As such, they require 4 bytes of storage to encode each character. Inside MySQL, this requires switching from utf8 storage to utf8mb4.

You can modify a database and table using a statement like:

ALTER DATABASE my_database DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
ALTER TABLE my_table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

You will also need to modify your connection character set.

Version History

See CHANGES.md

More Repositories

1

emoji-data

Easy to parse data and spritesheets for emoji
HTML
2,550
star
2

oembed

The oEmbed Spec
PHP
1,314
star
3

php-emoji

A PHP Emoji conversion library
PHP
1,255
star
4

enchant-order

Minecraft enchantment optimization tool
JavaScript
261
star
5

Flickr-StatsD

Mirror of code.flickr.com: Flickr-StatsD
Perl
110
star
6

PNGStore

Store JS/CSS in compressed PNGs
JavaScript
94
star
7

rfc822

PHP email address validator based on RFCs 822, 2822, 5322 & more
PHP
82
star
8

World-of-MapCraft

World of Warcraft Slippy Maps
PHP
79
star
9

service.js

Make init.d scripts for node apps
JavaScript
69
star
10

lib_autolink

PHP HTML link formatting library
PHP
60
star
11

SQLParser

Parse MySQL schemas in PHP, fast
PHP
58
star
12

brainfuck-debug

Javascript Brainfuck Interpreter / Debugger
HTML
54
star
13

lego-carcassonne

Lego Carcassonne
22
star
14

lib_sanitize

PHP input filtering library
PHP
20
star
15

lib_oauth

Very simple PHP4 OAuth library
PHP
18
star
16

rollup

Aggregate & graph high-volume sample data in real time
JavaScript
18
star
17

glitch-oauth-demos

Glitch API OAuth examples
PHP
18
star
18

logTailer

Tail logs over HTTP, using node
JavaScript
15
star
19

lib_bcrypt

Shut up and use bcrypt
PHP
14
star
20

lib_filter

PHP HTML filtering library
PHP
13
star
21

unicodey.com

Unicode tools
PHP
13
star
22

choose

Choose your own adventure, in PHP
PHP
13
star
23

iTunesRemote

iTunesRemote is a simple web interface that allows you to control iTunes over the web.
PHP
11
star
24

uploadr

Mirror of code.flickr.com: uploadr
ActionScript
10
star
25

grant-pattishall-award.com

The website
Hack
10
star
26

lib_timezones

Sensible Timezone Selection
PHP
10
star
27

logSmush

Aggregate logs from multiple servers in real time
JavaScript
9
star
28

iTunesServer

A iTunes-like web interface for remote music collections
PHP
9
star
29

WordPress-SSO

Single Sign-On for WordPress
PHP
7
star
30

slack-if

Play Interactive Fiction on Slack
JavaScript
7
star
31

perl-Flickr-API

Perl interface to the Flickr API
Perl
7
star
32

wow-NewAddon

A template for creating Lua-only addons for World of Warcraft.
Lua
7
star
33

MediaWiki-SSO

SSO for MediaWiki
PHP
7
star
34

g2

Ganglia interface tools
PHP
6
star
35

flengbot

Group messaging Jabber bot
Perl
6
star
36

homespring.js

A JavaScript interpreter for Homespring
JavaScript
6
star
37

autolink-js

JS link formatting library
JavaScript
6
star
38

bad-toys

Capture client-side Javascript errors in the wild
PHP
6
star
39

SchemaDiff

Database schema diffing tool
PHP
6
star
40

flamework-JSON

Decode bad 'JSON' in PHP
PHP
5
star
41

Wowhead-API

PHP library for turning Wowhead pages into useful data
PHP
5
star
42

perl-CSS

CSS.pm
Perl
5
star
43

php-snippets

Small/misc pieces of PHP
Groff
5
star
44

Choropleths

Tools for generating choropleth maps
Perl
4
star
45

gmaps-custom

Use a custom tile-set with Google Maps
JavaScript
4
star
46

shakefeed.com

The ShakeFeed webapp
PHP
4
star
47

gmaps-labels

Label overlays for Google Maps
JavaScript
4
star
48

mc-pixels

PHP
4
star
49

desktopr

Mirror of code.flickr.com: desktopr
JavaScript
4
star
50

clustr

Mirror of code.flickr.com: clustr
C++
4
star
51

lib_solr_query

Turn human queries into SOLR queries
PHP
3
star
52

Flickr-FTPD

An experimental upload-via-FTP interface for Flickr
Java
3
star
53

londonbloggers

A revamp of the london bloggers directory
PHP
3
star
54

perl-Algorithm-PCA

Principal component analysis
Perl
3
star
55

Flickr-Test

Mirror of code.flickr.com: Flickr-Test
Perl
3
star
56

Trac-SSO

Python
3
star
57

wow-MillHouse

WoW addon for summarizing herbs ready to mill
Lua
3
star
58

Phabricator-SSO

Single Sign-On for Phabricator
PHP
3
star
59

wow-Precedence

WoW addon that creates a dashboard for raiding hunters
Lua
3
star
60

wow-BunnyHunter

World of Warcraft addon for tracking rare pet farming.
Lua
3
star
61

lib_classify

PHP Language bayesian classifier
PHP
2
star
62

sycamore-server

A game (engine)
JavaScript
2
star
63

InsanityTracker

A web app to track players who gain the achievement Insane in the Membrane in World of Warcraft
PHP
2
star
64

wow-Non-Compos-Mentis

World of Warcraft addon for tracking the [Insane in the Membrane] achievement.
Lua
2
star
65

perl-XML-Parser-Lite-Tree-XPath

XML::Parser::Lite::Tree::XPath - Pure Perl XPath implementation
Perl
2
star
66

ease.js

Easing functions for natural animation, in JavaScript.
JavaScript
2
star
67

updog.report

what's updog?
PHP
2
star
68

thisismyj.am

Web app for tracking your Jams
PHP
2
star
69

wow-Auc-Searcher-Insane

World of Warcraft Auctioneer plugin for search for Darkmoon Faire rep items.
Lua
2
star
70

perl-Parse-EBNF

Parse::EBNF from CPAN
Perl
2
star
71

http_utils.js

HTTP utils for node.js
JavaScript
2
star
72

flamework-useragent

A Flamework library for decoding User Agent strings
PHP
2
star
73

perl-Acme-Goatse

On-demand goatse
Perl
2
star
74

wow-BuffGuide

Show missing raid buffs
Lua
2
star
75

XML_NiceTree

PHP4 xpath-like library
PHP
2
star
76

wow-TreasureHunt

WoW addon to track treasure items
Lua
2
star
77

lib_xml

Mirror of code.flickr.com: lib_xml
PHP
1
star
78

flickrPearAPI

Flickr API PHP PEAR module
PHP
1
star
79

perl-Language-Nouse

Perl interpreter for the nouse language
Perl
1
star
80

hCardFight

Microformats collectible card game
PHP
1
star
81

perl-Acme-Python

For *real* python programs
Perl
1
star
82

wow-MoveSCT

WoW addon to move the default scrolling combat text
Lua
1
star
83

lib_ap

Admin Protocol Library
PHP
1
star
84

mysql-rest-monitor-php

1
star
85

ImageMagickTips

How to actually do common things in IM
1
star
86

the5k.org

PHP
1
star
87

little-glitch

A little-printer Glitch app
PHP
1
star
88

perl-Image-Caa

Colored ASCII Art library
Perl
1
star
89

XML_Atom

PHP Atom parser, compatible with PEAR's XML::RSS
PHP
1
star
90

perl-Inline-Interp

Make Inline modules for interpreted languages, easily
Perl
1
star
91

mittens

Modular blogging platform in Django (unfinished)
JavaScript
1
star
92

perl-Flickr-misc

Misc Flickr perl utils
Perl
1
star
93

CalAmp

Record your track history from WinAmp2
C
1
star
94

pquery

jQuery style DOM traversal, in PHP
PHP
1
star
95

bingo.iamcal.com

HTML
1
star
96

perl-Language-Homespring-Visualise-GraphViz

CPAN Language::Homespring::Visualise::GraphViz
Perl
1
star
97

flickr-shell

Perl
1
star
98

perl-Inline-SMITH

Inline.pm module to let you write Perl subs in SMITH
Perl
1
star
99

GuildInfo

WoW Guild site
PHP
1
star
100

phpBB-Patches

Misc patches for phpBB
1
star