Module: EmojiData

Defined in:
lib/emoji_data.rb,
lib/emoji_data/version.rb,
lib/emoji_data/emoji_char.rb

Defined Under Namespace

Classes: EmojiChar

Constant Summary collapse

VERSION =

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

"0.2.0"

Class Method Summary collapse

Class Method Details

.allArray<EmojiChar>

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


39
40
41
# File 'lib/emoji_data.rb', line 39

def self.all
  EMOJI_CHARS
end

.all_doublebyteArray<EmojiChar>

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


47
48
49
# File 'lib/emoji_data.rb', line 47

def self.all_doublebyte
  EMOJI_CHARS.select(&:doublebyte?)
end

.all_with_variantsArray<EmojiChar>

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


54
55
56
# File 'lib/emoji_data.rb', line 54

def self.all_with_variants
  EMOJI_CHARS.select(&:variant?)
end

.char_to_unified(char) ⇒ String

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.

Examples:

>> EmojiData.unified_to_char("1F47E")
=> "👾"

110
111
112
# 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 = {}) ⇒ Array<String>

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.

Options Hash (opts):

  • :include_variants (Boolean)

    whether or not to include all possible encoding variants in the list


71
72
73
74
75
76
77
78
79
80
81
# 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 = {}) ⇒ Array<String>

Returns a list of all known codepoints representing Emoji characters.

Options Hash (opts):

  • :include_variants (Boolean)

    whether or not to include all possible encoding variants in the list


87
88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Array<EmojiChar>

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


160
161
162
# 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) ⇒ Array<EmojiChar>

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


169
170
171
# 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

.from_short_name(short_name) ⇒ EmojiChar

Finds a specific EmojiChar based on the unified codepoint ID.

Must be exact match.


179
180
181
# File 'lib/emoji_data.rb', line 179

def self.from_short_name(short_name)
  EMOJICHAR_KEYWORD_MAP[short_name.downcase]
end

.from_unified(uid) ⇒ EmojiChar Also known as: find_by_unified

Finds a specific EmojiChar based on its unified codepoint ID.


130
131
132
# File 'lib/emoji_data.rb', line 130

def self.from_unified(uid)
  EMOJICHAR_UNIFIED_MAP[uid.upcase]
end

.scan(str) ⇒ Array<EmojiChar> Also known as: find_by_str

Scans a string for all encoded emoji characters contained within.

Examples:

>> EmojiData.scan("flying on my 🚀 to visit the 👾 people.")
=> [#<EmojiData::EmojiChar... @name="ROCKET", @unified="1F680", ...>,
#<EmojiData::EmojiChar... @name="ALIEN MONSTER", @unified="1F47E", ...>]

151
152
153
154
# 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

.unified_to_char(uid) ⇒ String

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

Examples:

>> EmojiData.char_to_unified("👾")
=> "1F47E"

122
123
124
# File 'lib/emoji_data.rb', line 122

def self.unified_to_char(uid)
  EmojiChar::unified_to_char(uid)
end