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.

Returns:

  • (Array<EmojiChar>)

    a list of all known EmojiChar.



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.

Returns:

  • (Array<EmojiChar>)

    a list of all doublebyte EmojiChar.



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.

Returns:

  • (Array<EmojiChar>)

    a list of all EmojiChar with 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")
=> "👾"

Parameters:

  • char (String)

    a single rendered emoji glyph encoded as a UTF-8 string

Returns:

  • (String)

    the unified ID



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.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :include_variants (Boolean)

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

Returns:

  • (Array<String>)

    all Emoji characters rendered as UTF-8 strings



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.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :include_variants (Boolean)

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

Returns:

  • (Array<String>)

    all codepoints represented as unified ID strings



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.

Parameters:

  • name (String)

Returns:



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.

Parameters:

  • short_name (String)

Returns:



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.

Parameters:

  • short_name (String)

Returns:



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.

Parameters:

  • uid (String)

    the unified codepoint ID for an emoji

Returns:



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", ...>]

Parameters:

  • str (String)

    the target string to search

Returns:

  • (Array<EmojiChar>)

    all emoji characters contained within the target string, in the order they appeared.



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"

Parameters:

  • uid (String)

    the unified codepoint ID for an emoji

Returns:

  • (String)

    UTF-8 string rendering of the emoji character



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

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