module EmojiData

Constants

EMOJICHAR_KEYWORD_MAP

precomputed hashmap for fast precached lookups in .from_short_name

EMOJICHAR_UNIFIED_MAP

precomputed hashmap for fast precached lookups in .from_unified

EMOJI_CHARS

precomputed list of all possible emoji characters

FBS_REGEXP

precompile regex pattern for fast matches in ‘.scan` needs to be defined after self.chars so not at top of file for now…

GEM_ROOT

specify some location paths

VENDOR_DATA
VERSION

Current version of the module, for bundling to rubygems.org

Public Class Methods

all() click to toggle source

Returns a list of all known Emoji characters as ‘EmojiChar` objects.

@return [Array<EmojiChar>] a list of all known ‘EmojiChar`.

# File lib/emoji_data.rb, line 39
def self.all
  EMOJI_CHARS
end
all_doublebyte() click to toggle source

Returns a list of all ‘EmojiChar` that are represented with doublebyte encoding.

@return [Array<EmojiChar>] a list of all doublebyte ‘EmojiChar`.

# File lib/emoji_data.rb, line 47
def self.all_doublebyte
  EMOJI_CHARS.select(&:doublebyte?)
end
all_with_variants() click to toggle source

Returns a list of all ‘EmojiChar` that have at least one variant encoding.

@return [Array<EmojiChar>] a list of all ‘EmojiChar` with variant encoding.

# File lib/emoji_data.rb, line 54
def self.all_with_variants
  EMOJI_CHARS.select(&:variant?)
end
char_to_unified(char) click to toggle source

Convert a native UTF-8 string glyph to its unified codepoint ID.

This is a conversion operation, not a match, so it may produce unexpected results with different types of values.

@param char [String] a single rendered emoji glyph encoded as a UTF-8 string @return [String] the unified ID

@example

>> EmojiData.unified_to_char("1F47E")
=> "👾"
# File lib/emoji_data.rb, line 110
def self.char_to_unified(char)
  char.codepoints.to_a.map { |i| i.to_s(16).rjust(4,'0')}.join('-').upcase
end
chars(opts={}) click to toggle source

Returns a list of all known Emoji characters rendered as UTF-8 strings.

By default, the default rendering options for this library will be used. However, if you pass an option hash with ‘include_variants: true` then all possible renderings of a single glyph will be included, meaning that:

  1. You will have “duplicate” emojis in your list.

  2. This list is now suitable for exhaustably matching against in a search.

@option opts [Boolean] :include_variants whether or not to include all

possible encoding variants in the list

@return [Array<String>] all Emoji characters rendered as UTF-8 strings

# File lib/emoji_data.rb, line 71
def self.chars(opts={})
  options = {include_variants: false}.merge(opts)

  normals = EMOJI_CHARS.map { |c| c.render({variant_encoding: false}) }

  if options[:include_variants]
    extras  = self.all_with_variants.map { |c| c.render({variant_encoding: true}) }
    return normals + extras
  end
  normals
end
codepoints(opts={}) click to toggle source

Returns a list of all known codepoints representing Emoji characters.

@option (see .chars) @return [Array<String>] all codepoints represented as unified ID strings

# File lib/emoji_data.rb, line 87
def self.codepoints(opts={})
  options = {include_variants: false}.merge(opts)

  normals = EMOJI_CHARS.map(&:unified)

  if options[:include_variants]
    extras = self.all_with_variants.map {|c| c.variant}
    return normals + extras
  end
  normals
end
find_by_name(name) click to toggle source

Finds any ‘EmojiChar` that contains given string in its official name.

@param name [String] @return [Array<EmojiChar>]

# File lib/emoji_data.rb, line 160
def self.find_by_name(name)
  self.find_by_value(:name, name.upcase)
end
find_by_short_name(short_name) click to toggle source

Find all ‘EmojiChar` that match string in any of their associated short name keywords.

@param short_name [String] @return [Array<EmojiChar>]

# File lib/emoji_data.rb, line 169
def self.find_by_short_name(short_name)
  self.find_by_value(:short_name, short_name.downcase)
end
find_by_str(str)
Alias for: scan
find_by_unified(uid)
Alias for: from_unified
from_short_name(short_name) click to toggle source

Finds a specific ‘EmojiChar` based on the unified codepoint ID.

Must be exact match.

@param short_name [String] @return [EmojiChar]

# File lib/emoji_data.rb, line 179
def self.from_short_name(short_name)
  EMOJICHAR_KEYWORD_MAP[short_name.downcase]
end
from_unified(uid) click to toggle source

Finds a specific ‘EmojiChar` based on its unified codepoint ID.

@param uid [String] the unified codepoint ID for an emoji @return [EmojiChar]

# File lib/emoji_data.rb, line 130
def self.from_unified(uid)
  EMOJICHAR_UNIFIED_MAP[uid.upcase]
end
Also aliased as: find_by_unified
scan(str) click to toggle source

Scans a string for all encoded emoji characters contained within.

@param str [String] the target string to search @return [Array<EmojiChar>] all emoji characters contained within the target

string, in the order they appeared.

@example

>> EmojiData.scan("flying on my 🚀 to visit the 👾 people.")
=> [#<EmojiData::EmojiChar... @name="ROCKET", @unified="1F680", ...>,
#<EmojiData::EmojiChar... @name="ALIEN MONSTER", @unified="1F47E", ...>]
# File lib/emoji_data.rb, line 151
def self.scan(str)
  matches = str.scan(FBS_REGEXP)
  matches.map { |m| EmojiData.from_unified(EmojiData.char_to_unified(m)) }
end
Also aliased as: find_by_str
unified_to_char(uid) click to toggle source

Convert a unified codepoint ID directly to its UTF-8 string representation.

@param uid [String] the unified codepoint ID for an emoji @return [String] UTF-8 string rendering of the emoji character

@example

>> EmojiData.char_to_unified("👾")
=> "1F47E"
# File lib/emoji_data.rb, line 122
def self.unified_to_char(uid)
  EmojiChar::unified_to_char(uid)
end

Protected Class Methods

find_by_value(field,value) click to toggle source
# File lib/emoji_data.rb, line 192
def self.find_by_value(field,value)
  self.all.select { |char| char.send(field).include? value }
end