lib/emoji_data.rb in emoji_data-0.0.2 vs lib/emoji_data.rb in emoji_data-0.0.3

- old
+ new

@@ -6,10 +6,13 @@ GEM_ROOT = File.join(File.dirname(__FILE__), '..') RAW_JSON = IO.read(File.join(GEM_ROOT, 'vendor/emoji-data/emoji.json')) EMOJI_MAP = JSON.parse( RAW_JSON ) EMOJI_CHARS = EMOJI_MAP.map { |em| EmojiChar.new(em) } + # hashmap for fast unified lookups + EMOJICHAR_UNIFIED_MAP = Hash[EMOJI_CHARS.map { |u| [u.unified, u] }] + def self.all EMOJI_CHARS end def self.chars @@ -27,10 +30,11 @@ def self.unified_to_char(cp) find_by_unified(cp).char end def self.find_by_unified(cp) - EMOJI_CHARS.detect { |ec| ec.unified == cp.upcase } + # EMOJI_CHARS.detect { |ec| ec.unified == cp.upcase } + EMOJICHAR_UNIFIED_MAP[cp.upcase] end def self.find_by_str(str) matches = EMOJI_CHARS.select { |ec| str.include? ec.char } matches.sort_by { |matched_char| str.index(matched_char.char) }