Module: UnicodeNamecode::Emoji

Defined in:
lib/unicode_namecode/emoji.rb

Overview

Handles emoji-related functionality and bidirectional lookup

Constant Summary collapse

EMOJI_PATH =
File.expand_path('../../../data/emoji-test.txt', __FILE__)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.codepoint_to_emojiObject (readonly)

Returns the value of attribute codepoint_to_emoji.



12
13
14
# File 'lib/unicode_namecode/emoji.rb', line 12

def codepoint_to_emoji
  @codepoint_to_emoji
end

.emoji_to_codepointsObject (readonly)

Returns the value of attribute emoji_to_codepoints.



12
13
14
# File 'lib/unicode_namecode/emoji.rb', line 12

def emoji_to_codepoints
  @emoji_to_codepoints
end

.emoji_to_nameObject (readonly)

Returns the value of attribute emoji_to_name.



12
13
14
# File 'lib/unicode_namecode/emoji.rb', line 12

def emoji_to_name
  @emoji_to_name
end

Class Method Details

.codepoint_for_emoji(emoji) ⇒ Object

Get the codepoint(s) for an emoji character



46
47
48
49
# File 'lib/unicode_namecode/emoji.rb', line 46

def codepoint_for_emoji(emoji)
  load_emoji_data unless @emoji_to_codepoints
  @emoji_to_codepoints[emoji]
end

.emoji_for_codepoint(codepoint_or_array) ⇒ Object

Get the emoji character for a codepoint or codepoint sequence



58
59
60
61
62
63
64
65
# File 'lib/unicode_namecode/emoji.rb', line 58

def emoji_for_codepoint(codepoint_or_array)
  load_emoji_data unless @codepoint_to_emoji
  if codepoint_or_array.is_a?(Array)
    @codepoint_to_emoji[codepoint_or_array] || @codepoint_to_emoji[codepoint_or_array.map(&:to_i)]
  else
    @codepoint_to_emoji[codepoint_or_array.to_i]
  end
end

.load_emoji_dataObject

Parse emoji-test.txt and build lookup tables



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/unicode_namecode/emoji.rb', line 15

def load_emoji_data
  @emoji_to_codepoints = {}
  @emoji_to_name = {}
  @codepoint_to_emoji = {}
  return unless File.exist?(EMOJI_PATH)
  
  File.foreach(EMOJI_PATH) do |line|
    next if line.strip.empty? || line.start_with?('#')
    
    # Parse lines like: "1F60A ; fully-qualified # 😊 E1.0 smiling face with smiling eyes"
    if line =~ /^([0-9A-F ]+)\s*;\s*fully-qualified\s*#\s*(\S+)\s+E[0-9.]+\s+(.+)$/
      codepoints_hex, emoji, name = $1, $2, $3
      
      # Convert hex codepoints to integers
      codepoints = codepoints_hex.strip.split.map { |cp| cp.to_i(16) }
      
      # Store emoji -> codepoint(s) mapping
      @emoji_to_codepoints[emoji] = codepoints.length == 1 ? codepoints.first : codepoints
      @emoji_to_name[emoji] = name.upcase
      
      # Store codepoint(s) -> emoji mapping for reverse lookup
      if codepoints.length == 1
        @codepoint_to_emoji[codepoints.first] = emoji
      else
        @codepoint_to_emoji[codepoints] = emoji
      end
    end
  end
end

.name_for_emoji(emoji) ⇒ Object

Get the official Unicode name for an emoji character



52
53
54
55
# File 'lib/unicode_namecode/emoji.rb', line 52

def name_for_emoji(emoji)
  load_emoji_data unless @emoji_to_name
  @emoji_to_name[emoji]
end