Commit a6c23da9 by Mark Wunsch

Merge pull request #27 from delighted/avoid-excessive-symbol-creation

Avoid creating symbols based on user input
parents 74c5080a 675bf34b
......@@ -13,7 +13,7 @@ module Rumoji
# Transform a cheat-sheet code into an Emoji
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
def encode_io(readable, writeable=StringIO.new(""))
......
......@@ -12,7 +12,11 @@ module Rumoji
end
def symbol
@cheat_codes.first
symbols.first
end
def symbols
@cheat_codes
end
def code
......@@ -20,7 +24,7 @@ module Rumoji
end
def include?(symbol)
@cheat_codes.include? symbol.to_sym
symbols.map(&:to_s).include? symbol.to_s
end
def to_s
......@@ -53,8 +57,14 @@ module Rumoji
ALL_REGEXP = Regexp.new(ALL.map(&:string).join('|'))
SYMBOL_LOOKUP = ALL.each.with_object({}) do |emoji, lookup|
emoji.symbols.each do |symbol|
lookup[symbol.to_s] = emoji
end
end
def self.find(symbol)
ALL.find {|emoji| emoji.include? symbol }
SYMBOL_LOOKUP[symbol.to_s]
end
STRING_LOOKUP = ALL.each.with_object({}) do |emoji, lookup|
......
......@@ -62,10 +62,14 @@ describe Rumoji::Emoji do
let(:smile_str) { "\u{1F604}" }
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
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
subject.find_by_string(smile_str).symbol.must_equal smile_sym
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