Commit 9cbd04d2 by Ben Turner

Don't convert all input to symbols during lookup

parent 065d5901
...@@ -13,7 +13,7 @@ module Rumoji ...@@ -13,7 +13,7 @@ module Rumoji
# Transform a cheat-sheet code into an Emoji # Transform a cheat-sheet code into an Emoji
def decode(str) def decode(str)
str.gsub(/:([^\s:]?[\w-]+):/) {|sym| (Emoji.find($1.intern) || sym).to_s } str.gsub(/:([^\s:]?[\w-]+):/) { |match| (Emoji.find($1) || match).to_s }
end end
def encode_io(readable, writeable=StringIO.new("")) def encode_io(readable, writeable=StringIO.new(""))
......
...@@ -20,7 +20,7 @@ module Rumoji ...@@ -20,7 +20,7 @@ module Rumoji
end end
def include?(symbol) def include?(symbol)
@cheat_codes.include? symbol.to_sym @cheat_codes.map(&:to_s).include? symbol.to_s
end end
def to_s def to_s
...@@ -53,8 +53,11 @@ module Rumoji ...@@ -53,8 +53,11 @@ module Rumoji
ALL_REGEXP = Regexp.new(ALL.map(&:string).join('|')) ALL_REGEXP = Regexp.new(ALL.map(&:string).join('|'))
FIND_BY_SYMBOL_CACHE = {}
def self.find(symbol) def self.find(symbol)
ALL.find {|emoji| emoji.include? symbol } symbol = symbol.to_s
FIND_BY_SYMBOL_CACHE[symbol] ||= ALL.find { |emoji| emoji.include? symbol }
end end
STRING_LOOKUP = ALL.each.with_object({}) do |emoji, lookup| STRING_LOOKUP = ALL.each.with_object({}) do |emoji, lookup|
......
...@@ -62,10 +62,14 @@ describe Rumoji::Emoji do ...@@ -62,10 +62,14 @@ describe Rumoji::Emoji do
let(:smile_str) { "\u{1F604}" } let(:smile_str) { "\u{1F604}" }
let(:smile_sym) { :smile } let(:smile_sym) { :smile }
it "finds an emoji from cheat sheet code" do it "finds an emoji from cheat sheet code symbol" do
subject.find(smile_sym).to_s.must_equal smile_str subject.find(smile_sym).to_s.must_equal smile_str
end end
it "finds an emoji from cheat sheet code string" do
subject.find(smile_sym.to_s).to_s.must_equal smile_str
end
it "finds an emoji from a string" do it "finds an emoji from a string" do
subject.find_by_string(smile_str).symbol.must_equal smile_sym subject.find_by_string(smile_str).symbol.must_equal smile_sym
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment