Commit ca7af064 by Mark Wunsch

An emoji can print its own cheat sheet code

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