Skip to content

Commit

Permalink
implement EmojiData.from_short_name()
Browse files Browse the repository at this point in the history
  • Loading branch information
mroth committed Sep 4, 2014
1 parent 984cbd0 commit 5ee23a8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

* Rename a number of methods to be clearer and more consistent with that they
actually do:
- `EmojiChar.char``EmojiChar.render`
- `EmojiData.find_by_unified``EmojiData.from_unified`
- `EmojiData.find_by_str``EmojiData.scan`
- `EmojiChar.char()``EmojiChar.render()`
- `EmojiData.find_by_unified()``EmojiData.from_unified()`
- `EmojiData.find_by_str()``EmojiData.scan()`

Don't worry, the old names are still aliased in so you don't have to change
anything in your existing code. This change is make things clearer for
people new to the library.

* Add new `.from_short_name()` library method for fast keyword lookups.
* DEVELOPERS: Internal code cleanup and better comments.
* DEVELOPERS: Add benchmark suite for comparing method implementation time
across versions of this library.
Expand Down
25 changes: 19 additions & 6 deletions lib/emoji_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module EmojiData
vendordata.map { |em| EmojiChar.new(em) }
end

# precomputed hashmap for fast precached lookups
# precomputed hashmap for fast precached lookups in .from_unified
EMOJICHAR_UNIFIED_MAP = begin
results = Hash[EMOJI_CHARS.map { |u| [u.unified, u] }]
EMOJI_CHARS.select(&:variant?).each do |char|
Expand All @@ -26,8 +26,16 @@ module EmojiData
results
end

# precomputed hashmap for fast precached lookups in .from_short_name
EMOJICHAR_KEYWORD_MAP = {}
EMOJI_CHARS.each do |ec|
ec.short_names.each { |keyword| EMOJICHAR_KEYWORD_MAP[keyword] = ec }
end


# our constants are only for usage internally
private_constant :GEM_ROOT, :VENDOR_DATA, :EMOJI_CHARS, :EMOJICHAR_UNIFIED_MAP
private_constant :GEM_ROOT, :VENDOR_DATA
private_constant :EMOJI_CHARS, :EMOJICHAR_UNIFIED_MAP, :EMOJICHAR_KEYWORD_MAP


# Returns a list of all known Emoji characters as `EmojiChar` objects.
Expand Down Expand Up @@ -171,10 +179,15 @@ def self.find_by_short_name(short_name)
self.find_by_value(:short_name, short_name.downcase)
end


# TODO: port over .from_shortname from NodeJS version
# needs to be added to benchmarks for all versions too!

# Finds a specific `EmojiChar` based on the unified codepoint ID.
#
# Must be exact match.
#
# @param short_name [String]
# @return [EmojiChar]
def self.from_short_name(short_name)
EMOJICHAR_KEYWORD_MAP[short_name.downcase]
end

# alias old method names for legacy apps
class << self
Expand Down
19 changes: 19 additions & 0 deletions spec/emoji_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,25 @@
end
end

describe ".from_short_name" do
it "returns exact matches on a short name" do
results = EmojiData.from_short_name('scream')
results.should be_kind_of(EmojiChar)
results.name.should eq('FACE SCREAMING IN FEAR')
end
it "handles lowercasing input if required" do
EmojiData.from_short_name('SCREAM').should eq( EmojiData.from_short_name('scream') )
end
it "works on secondary keywords" do
primary = EmojiData.from_short_name('hankey')
EmojiData.from_short_name('poop').should eq(primary)
EmojiData.from_short_name('shit').should eq(primary)
end
it "returns nil if nothing matches" do
EmojiData.from_short_name('taco').should be_nil
end
end

describe ".char_to_unified" do
it "converts normal emoji to unified codepoint" do
EmojiData.char_to_unified("👾").should eq('1F47E')
Expand Down

0 comments on commit 5ee23a8

Please sign in to comment.