Skip to content

Commit

Permalink
Merge 1aa8dc2 into 3f1885b
Browse files Browse the repository at this point in the history
  • Loading branch information
benedikt committed Apr 1, 2015
2 parents 3f1885b + 1aa8dc2 commit 1bba9a6
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 7 deletions.
8 changes: 8 additions & 0 deletions lib/emoji_data.rb
Expand Up @@ -20,6 +20,7 @@ module EmojiData
EMOJI_CHARS.each do |ec|
EMOJICHAR_UNIFIED_MAP[ec.unified] = ec
ec.variations.each { |variant| EMOJICHAR_UNIFIED_MAP[variant] = ec }
ec.skin_variations.each { |skin_variant| EMOJICHAR_UNIFIED_MAP[skin_variant.unified] = skin_variant }
end

# precomputed hashmap for fast precached lookups in .from_short_name
Expand Down Expand Up @@ -55,6 +56,13 @@ def self.all_with_variants
EMOJI_CHARS.select(&:variant?)
end

# Returns a list of all `EmojiChar` that have at least one skin variant encoding.
#
# @return [Array<EmojiChar>] a list of all `EmojiChar` with skin variant encoding.
def self.all_with_skin_variants
EMOJI_CHARS.select(&:skin_variant?)
end

# Returns a list of all known Emoji characters rendered as UTF-8 strings.
#
# By default, the default rendering options for this library will be used.
Expand Down
18 changes: 18 additions & 0 deletions lib/emoji_data/emoji_char.rb
Expand Up @@ -34,16 +34,24 @@ def initialize(emoji_hash)
# array for instance value, and let it get overriden in main
# deserialization loop if variable is present.
@variations = []
@skin_variations = []

# trick for declaring instance variables while iterating over a hash
# http://stackoverflow.com/questions/1615190/
emoji_hash.each do |k,v|
if v.kind_of?(Hash)
v = v.map do |_, variation|
EmojiChar.new(variation)
end
end
instance_variable_set("@#{k}",v)
eigenclass = class<<self; self; end
eigenclass.class_eval { attr_reader k }
end
end

attr_reader :skin_variations

# Renders an `EmojiChar` to its string glyph representation, suitable for
# printing to screen.
#
Expand Down Expand Up @@ -75,6 +83,9 @@ def chars
@variations.each do |variation|
results << EmojiChar::unified_to_char(variation)
end
@skin_variations.each do |skin_variation|
results += skin_variation.chars
end
@chars ||= results
end

Expand All @@ -92,6 +103,13 @@ def variant?
@variations.length > 0
end

# Does the `EmojiChar` have an alternate skin variant encoding?
#
# @return [Boolean]
def skin_variant?
@skin_variations.length > 0
end

# Returns the most likely variant-encoding codepoint ID for an `EmojiChar`.
#
# For now we only know of one possible variant encoding for certain
Expand Down
8 changes: 8 additions & 0 deletions spec/emoji_char_spec.rb
Expand Up @@ -30,6 +30,7 @@
@usflag = EmojiChar.new({'unified' => '1F1FA-1F1F8'})
@hourglass = EmojiChar.new({'unified' => '231B', 'variations' => ['231B-FE0F']})
@cloud = EmojiChar.new({'unified' => '2601', 'variations' => ['2601-FE0F']})
@ear = EmojiChar.new({'unified' => '1F442', 'skin_variations' => { '1F442-1F3FF' => { 'unified' => '1F442-1F3FF' }}})
end

describe "#to_s" do
Expand Down Expand Up @@ -84,6 +85,13 @@
end
end

describe "#skin_variant?" do
it "should indicate when a character has a skin variant encoding" do
@ear.skin_variant?.should be_true
@usflag.variant?.should be_false
end
end

describe "#variant" do
it "should return the most likely variant encoding ID representation for the char" do
@hourglass.variant.should eq('231B-FE0F')
Expand Down
20 changes: 15 additions & 5 deletions spec/emoji_data_spec.rb
Expand Up @@ -4,7 +4,7 @@
describe EmojiData do
describe ".all" do
it "should return an array of all 845 known emoji chars" do
EmojiData.all.count.should eq(845)
EmojiData.all.count.should eq(900)
end
it "should return all EmojiChar objects" do
EmojiData.all.all? {|char| char.class == EmojiData::EmojiChar}.should be_true
Expand All @@ -13,13 +13,19 @@

describe ".all_doublebyte" do
it "should return an array of all 21 known emoji chars with doublebyte encoding" do
EmojiData.all_doublebyte.count.should eq(21)
EmojiData.all_doublebyte.count.should eq(71)
end
end

describe ".all_with_variants" do
it "should return an array of all 107 known emoji chars with variant encodings" do
EmojiData.all_with_variants.count.should eq(107)
EmojiData.all_with_variants.count.should eq(116)
end
end

describe ".all_with_skin_variants" do
it "should return an array of all 107 known emoji chars with variant encodings" do
EmojiData.all_with_skin_variants.count.should eq(57)
end
end

Expand All @@ -45,14 +51,14 @@
describe ".codepoints" do
it "should return an array of all known codepoints in dashed string representation" do
EmojiData.codepoints.all? {|cp| cp.class == String}.should be_true
EmojiData.codepoints.all? {|cp| cp.match(/^[0-9A-F\-]{4,11}$/)}.should be_true
EmojiData.codepoints.all? {|cp| cp.match(/^[0-9A-F\-]{4,42}$/)}.should be_true
end
it "should include variants in list when options {include_variants: true}" do
results = EmojiData.codepoints({include_variants: true})
numChars = EmojiData.all.count
numVariants = EmojiData.all_with_variants.count
results.count.should eq(numChars + numVariants)
results.all? {|cp| cp.match(/^[0-9A-F\-]{4,16}$/)}.should be_true
results.all? {|cp| cp.match(/^[0-9A-F\-]{4,42}$/)}.should be_true
end
end

Expand Down Expand Up @@ -109,6 +115,10 @@
results.should_not be_nil
results.name.should eq('HEAVY BLACK HEART')
end
it "should find skin variants as well" do
results = EmojiData.from_unified('1f442-1f3ff')
results.should_not be_nil
end
end

describe ".find_by_unified - DEPRECATED" do
Expand Down
2 changes: 1 addition & 1 deletion vendor/emoji-data/README.md
Expand Up @@ -4,4 +4,4 @@ Manually vendored from iamcal/emoji-data
single file.)

Most recent vendoring from revision:
iamcal/emoji-data@6cb685cd1e
iamcal/emoji-data@0f0cf4e
2 changes: 1 addition & 1 deletion vendor/emoji-data/emoji.json

Large diffs are not rendered by default.

0 comments on commit 1bba9a6

Please sign in to comment.