Module: EmojiDatasource

Defined in:
lib/emoji_datasource.rb,
lib/emoji_datasource/emoji.rb,
lib/emoji_datasource/version.rb

Defined Under Namespace

Classes: Emoji, Error

Constant Summary collapse

EMOJI_DATA_PATH =
File.join(__dir__, '..', 'vendor', 'emoji-datasource', 'emoji.json')
VERSION =
'14.0.3'

Class Method Summary collapse

Class Method Details

.char_to_unified(raw_emoji) ⇒ Object



59
60
61
62
63
# File 'lib/emoji_datasource.rb', line 59

def self.char_to_unified(raw_emoji)
  return unless raw_emoji

  raw_emoji.unpack('U*').map { |c| c.to_s(16).upcase }.join('-')
end

.dataObject



65
66
67
68
# File 'lib/emoji_datasource.rb', line 65

def self.data
  @data ||= JSON.parse(File.read(EMOJI_DATA_PATH))
    .map { |emoji_data| EmojiDatasource::Emoji.new(emoji_data) }
end

.find_by_char(raw_emoji) ⇒ EmojiDatasource::Emoji?

Find emoji object by raw Unicode string with a single emoji in it

Examples:

find_by_char('👍') #=> EmojiDatasource::Emoji: :+1: (👍)

Parameters:

  • raw_emoji (String)

    Single emoji

Returns:

  • (EmojiDatasource::Emoji)

    if given string contains a single known emoji

  • (nil)

    if there are no such emoji in the dataset



49
50
51
# File 'lib/emoji_datasource.rb', line 49

def self.find_by_char(raw_emoji)
  find_by_unified(char_to_unified(raw_emoji))
end

.find_by_short_name(name) ⇒ EmojiDatasource::Emoji?

Finds emoji by short code (e.g. ‘+1`, `:+1:`, `:+1::skin-tone-5:`)

Examples:

find_by_short_name('+1') #=> EmojiDatasource::Emoji: :+1: (👍)

Parameters:

  • name (String)

    short code with or without wrapping colons.

Returns:

  • (EmojiDatasource::Emoji)

    if there is an emoji matching name

  • (nil)

    if there are no emojis matching name



25
26
27
28
29
30
31
# File 'lib/emoji_datasource.rb', line 25

def self.find_by_short_name(name)
  return unless name

  name = name.delete_prefix(':').delete_suffix(':') # Allow to find `+1` by `:+1:`
  name.delete_suffix!('::skin-tone-1') # special case for default skin tone
  short_name_lookup_map[name]
end

.find_by_unified(unified) ⇒ EmojiDatasource::Emoji?

Finds emoji by unified hex representation of Unicode codepoints

Examples:

short_name_to_char('1F44D') #=> EmojiDatasource::Emoji: :+1: (👍)

Parameters:

  • unified (String)

    Dash separated hexadecimal Unicode character codes for a single emoji

Returns:

  • (EmojiDatasource::Emoji)

    if given Unicode codepoints is a known emoji

  • (nil)

    if there are no emojis with given codepoints in the dataset



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

def self.find_by_unified(unified)
  unified_lookup_map[unified.upcase]
end

.short_name_lookup_mapObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Utility hash map to search by emoji short code, including variants



72
73
74
75
76
77
78
79
80
81
# File 'lib/emoji_datasource.rb', line 72

def self.short_name_lookup_map
  @short_name_lookup_map ||= data.each_with_object({}) do |emoji, result|
    emoji.short_names.each { |short_name| result[short_name] = emoji }
    emoji.variations.each do |emoji_variant|
      emoji_variant.short_names.each do |short_name|
        result[short_name] = emoji_variant
      end
    end
  end
end

.short_name_to_char(name) ⇒ Object

Examples:

short_name_to_char('+1') #=> "👍"


15
16
17
# File 'lib/emoji_datasource.rb', line 15

def self.short_name_to_char(name)
  find_by_short_name(name)&.to_char
end

.unified_lookup_mapObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Utility hash map to search by unicode character sequence hex codes, including variants



85
86
87
88
89
90
91
92
# File 'lib/emoji_datasource.rb', line 85

def self.unified_lookup_map
  @unified_lookup_map ||= data.each_with_object({}) do |emoji, result|
    result[emoji.unified] = emoji
    emoji.variations.each do |emoji_variant|
      result[emoji_variant.unified] = emoji_variant
    end
  end
end

.unified_to_char(unified_name) ⇒ Object



53
54
55
56
57
# File 'lib/emoji_datasource.rb', line 53

def self.unified_to_char(unified_name)
  return unless unified_name

  unified_name.split('-').map(&:hex).pack('U*')
end