Commit ca7af064 by Mark Wunsch

An emoji can print its own cheat sheet code

parent 325970fd
......@@ -6,14 +6,16 @@ require 'stringio'
module Rumoji
extend self
# Transform emoji into its cheat-sheet code
def encode(str)
remapped_codepoints = str.codepoints.flat_map do |codepoint|
emoji = Emoji.find_by_codepoint(codepoint)
emoji ? ":#{emoji.code}:".codepoints.entries : codepoint
emoji ? emoji.code.codepoints.entries : codepoint
end
remapped_codepoints.pack("U*")
end
# Transform a cheat-sheet code into an Emoji
def decode(str)
str.gsub(/:(\S?\w+):/) {|sym| Emoji.find($1.intern).to_s }
end
......@@ -21,7 +23,7 @@ module Rumoji
def encode_io(readable, writeable=StringIO.new(""))
readable.each_codepoint do |codepoint|
emoji = Emoji.find_by_codepoint(codepoint)
emoji_or_character = emoji ? ":#{emoji.code}:" : [codepoint].pack("U")
emoji_or_character = emoji ? emoji.code : [codepoint].pack("U")
writeable.write emoji_or_character
end
writeable
......
......@@ -10,10 +10,14 @@ module Rumoji
@name = name || @cheat_codes.first.to_s.upcase.gsub("_", " ")
end
def code
def symbol
@cheat_codes.first
end
def code
":#{symbol}:"
end
def include?(symbol)
@cheat_codes.include? symbol.to_sym
end
......
......@@ -13,7 +13,7 @@ describe Rumoji::Emoji do
end
it("has a name") { subject.name.must_equal name }
it("has a cheat sheet code") { symbols.must_include subject.code }
it("has a cheat sheet code") { symbols.must_include subject.code[1...-1].intern }
it("can test if it includes a cheat sheet code") { symbols.all?{|symbol| subject.include?(symbol) }.must_equal true }
it("converts to the emoji string") { subject.to_s.must_equal poo_string }
it("converts to a hex code") { subject.hex.must_equal "1F4A9" }
......@@ -51,7 +51,8 @@ describe Rumoji::Emoji do
Rumoji::Emoji.new(us_string, symbol, name)
end
it("has one code") { subject.code.must_equal symbol }
it("has one symbol, representing the code") { subject.symbol.must_equal symbol }
it("has one cheat sheet code") { subject.code[1...-1].intern.must_equal symbol }
it("includes the symbol") { subject.must_include symbol }
it("transforms to the correct string") { subject.to_s.must_equal us_string }
end
......@@ -66,12 +67,12 @@ describe Rumoji::Emoji do
end
it "finds an emoji from a string" do
subject.find_by_string(smile_str).code.must_equal smile_sym
subject.find_by_string(smile_str).symbol.must_equal smile_sym
end
it "finds an emoji from a codepoint" do
smile_str.codepoints.map do |point|
subject.find_by_codepoint(point).code.must_equal smile_sym
subject.find_by_codepoint(point).symbol.must_equal smile_sym
end
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