emogeez-parser

A toolbox for parsing emojis in texts.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
emogeez-parser
0.21.15 years ago6 years agoMinified + gzip package size for emogeez-parser in KB

Readme

Emogeez parser
This module helps you parsing emojis in text and find emojis to replace it with the emoji name, or utf8 or image. It works with emogeez-generator data generated. You can either use the fetchTheme to fetch the JSON file containing the emojis data from the opensource cdn jsdeliver, https://cdn.jsdelivr.net/gh/arthur-feral/emogeez@latest/packages/emogeez-generator/emojis/apple/apple.png or setTheme passing your data if you want to use a custom json. :warning: the, JSON data must be properly formated as the generator whould do if you use custom json.

Installation

$ yarn i emogeez-parser

How to use

Once installed, the different tools can be imported in your projects:
First configure the package for your personal use. It require an url where to get the resources generated by the package emogeez-generator. After running the emogeez-generator package, the generated files can be hosted by you or you can use the default generated hosted by the repository.
import parserFactory from 'emogeez-parser';

const {
  store,
  replacer,
  matcher,
} = parserFactory();

store.fetchTheme('apple')
    .then(() => {
      let text = 'Hello ! :) how are you 😊';
      const hasOnlyOneEmoji = matcher.hasOnlyOneEmoji('apple', text);
      // => hasOnlyOneEmoji = false
      const hasOnlyEmojis = matcher.hasOnlyEmojis('apple', text);
      // => hasOnlyEmojis = false

      text = replacer.toUTF8('apple', text);
      // text = Hello ! πŸ™‚ how are you 😊
      
      text = replacer.UTF8ToHTML('apple', text, (emoji) => {
        return `<%${emoji.name}%>`;
      });
      // text = Hello ! <%grinning-face%> how are you <%smiling-face-with-smiling-eyes%>
    });

configuration
  • When you initialize the library you can provide a configuration.

blackList {array}

The names you want to blacklist. When initialized, it will not store them, so they will not be matched in any use. Default []

theme {string}

The default thème. Default apple

themesUrl {string}

The url when all emojis themes are hosted. Default the emogeez-generator repository: https://cdn.jsdelivr.net/gh/arthur-feral/emogeez@latest/packages/emogeez-generator/emojis/apple/apple.json
store
  • It stores the emojis data. You can use it to load more themes

fetchTheme

It fetch theme from cdn or the url you provide arguments
  • theme {string} the theme name check the emogeez-generator for full list
  • data {object} the json containing emojis data for the theme

setTheme

You set your theme manually same args as fetchTheme
replacer
  • The tools provided by replacer should be used to replace emojis with what you want.

aliasesToNames

It will replace all aliases to the emoji name.
arguments
  • text {string} the text in which we want replace alias to name

returns {string}
const text = 'Hello :)';
replacer.aliasesToNames('apple', text);
-> 'Hello :slightly-smiling-face:'

toUTF8

It will replace all emojis name :emoji-name: to the utf8 char.
arguments
  • theme {string} the theme for what we want to match emojis
  • text {string} the text in which we want to replace

returns {string}
const text = 'Hello :slightly-smiling-face:';
replacer.toUTF8('apple', text);
-> 'Hello πŸ™‚'

utf8ToNames

It will replace all utf8 char to the emoji name.
arguments
  • theme {string} the theme for what we want to match emojis
  • text {string} the text in which we want to replace

returns {string}
const text = 'Hello πŸ™‚';
replacer.utf8ToNames('apple', text);
-> 'Hello :slightly-smiling-face:'

namesToHTML

It will replace all names to an HTML element. You can use the classname generated by the emogeez-generator to display the sprite.
arguments
  • theme {string} the theme for what we want to match emojis
  • text {string} the text in which we want to replace
  • HTMLRenderer {function} The function returning the html. It will provide the matched emoji data to be used in your renderer.

returns {string}
const text = 'Hello :slightly-smiling-face:';
replacer.namesToHTML('apple', text, (emoji) => {
  return `<span class="emoji-${emoji.name}"></span>`;
});
-> 'Hello <span class="emoji-slightly-smiling-face"></span>'

UTF8ToHTML

It will replace all utf8 char to an HTML element. You can use the classname generated by the emogeez-generator to display the sprite.
arguments
  • theme {string} the theme for what we want to match emojis
  • text {string} the text in which we want to replace
  • HTMLRenderer {function} The function returning the html. It will provide the matched emoji data to be used in your renderer.

returns {string}
const text = 'Hello πŸ™‚';
replacer.UTF8ToHTML('apple', text, (emoji) => {
  return `<span class="emoji-${emoji.name}"></span>`;
});
-> 'Hello <span class="emoji-slightly-smiling-face"></span>'

matcher
  • The tools provided by matcher should be used to find or match emojis in text.

hasEmojis

If you want to check if there is emojis in a text.
arguments
  • theme {string} the theme for what we want to match emojis
  • text {string} the text in which we want to find if there is emojis

returns {boolean}

getNames

Find the list of emojis's names in the text
arguments
  • theme {string} the theme for what we want to match emojis
  • text {string} the text in which we want to find if there is emojis

returns {array}

hasOnlyEmojis

Return true if the string contains only emojis in it
arguments
  • theme {string} the theme for what we want to match emojis
  • text {string} the text in which we want to find if there is emojis

returns {boolean}

hasOnlyOneEmoji

Return true if the string contains only one emoji in it
arguments
  • theme {string} the theme for what we want to match emojis
  • text {string} the text in which we want to find if there is emojis

returns {boolean}
Notes
Please contribute if you found it useful! ❀️
return 'enjoy';