# frozen_string_literal: true module TanukiEmoji # Character represents an Emoji character or sequence which can be formed by one or more Unicode code points # respectively which combined form a unique pictographic representation (known as Emoji) # # @see https://www.unicode.org/reports/tr51/ class Character attr_reader :name, :codepoints, :codepoints_alternates, :alpha_code, :aliases, :description # @param [String] name # @param [String] codepoints # @param [String] alpha_code def initialize(name, codepoints:, alpha_code:, description:) @name = name @codepoints = codepoints @codepoints_alternates = [] @alpha_code = self.class.format_alpha_code(alpha_code) @aliases = [] @description = description end # Add alternative codepoints to this character # # @param [String] codepoints def add_codepoints(codepoints) codepoints_alternates << codepoints end # Add alternative alpha_codes to this character # # @param [String] alpha_code def add_alias(alpha_code) aliases << self.class.format_alpha_code(alpha_code) end # Return a Hex formatted version of the Unicode code points # # @return [String] Hex formatted version of the unicode def hex unicode_to_hex(codepoints).join('-') end def to_s codepoints end def inspect "#<#{self.class.name}:#{name} #{codepoints}(#{hex})>" end # Convert Unicode code points to Hex format for inspection # # ensure alpha code is formatted with colons # # @param [String] alpha_code # @return [String] formatted alpha code def self.format_alpha_code(alpha_code) alpha_code.match?(/:([a-z_0-9]+):/) ? alpha_code : ":#{alpha_code}:" end private # Return each codepoint converted to its hex value as string # # @param [String] value # @return [Array] hex value as string def unicode_to_hex(value) value.unpack('U*').map { |i| i.to_s(16).rjust(4, '0') } end end end