lib/tanuki_emoji/character.rb in tanuki_emoji-0.3.0 vs lib/tanuki_emoji/character.rb in tanuki_emoji-0.4.0

- old
+ new

@@ -7,10 +7,11 @@ # @see https://www.unicode.org/reports/tr51/ class Character IMAGE_PREFIX = 'emoji_u' IMAGE_EXTENSION = '.png' FLAG_REGEXP = /[🇦-🇿]{2}/u.freeze + ALPHA_CODE_REGEXP = /:(?<alpha_text>[_+\-a-z0-9]+):/.freeze # This denotes a "color" or "emoji" version EMOJI_VARIATION_SELECTOR = 0xFE0F # This denotes a "plain" (black/white) or "textual" version @@ -23,11 +24,11 @@ # @param [String] name # @param [String] codepoints # @param [String] alpha_code def initialize(name, codepoints:, alpha_code:, description:) - @name = name + @name = self.class.format_name(name) @codepoints = codepoints @codepoints_alternates = [] @alpha_code = self.class.format_alpha_code(alpha_code) @aliases = [] @description = description @@ -89,17 +90,21 @@ alpha_code == other.alpha_code && aliases == other.aliases && description == other.description end - # Convert Unicode code points to Hex format for inspection + # Ensure alpha code is formatted with colons # - # 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.to_s.match?(/:([_+\-a-z0-9]+):/) ? alpha_code.to_s : ":#{alpha_code}:" + alpha_code.to_s.match?(ALPHA_CODE_REGEXP) ? alpha_code.to_s : ":#{alpha_code}:" + end + + def self.format_name(raw_name) + matched = raw_name.match(ALPHA_CODE_REGEXP) + + matched ? matched['alpha_text'] : raw_name end private # Return each codepoint converted to its hex value as string