Skip to content

Commit

Permalink
Freeze and dedup strings
Browse files Browse the repository at this point in the history
  • Loading branch information
jhawthorn committed May 15, 2019
1 parent 22b920f commit 69009d4
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions lib/emoji.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,23 @@ def parse_data_file
JSON.parse(file.read, symbolize_names: true)
end

if "".respond_to?(:-@)
# Ruby >= 2.3 this is equivalent to .freeze
# Ruby >= 2.5 this will freeze and dedup
dedup = lambda { |str| -str }
else
dedup = lambda { |str| str.freeze }
end

append_unicode = lambda do |emoji, raw|
unless TEXT_GLYPHS.include?(raw) || emoji.unicode_aliases.include?(raw)
emoji.add_unicode_alias(raw)
emoji.add_unicode_alias(dedup.call(raw))
end
end

data.each do |raw_emoji|
self.create(nil) do |emoji|
raw_emoji.fetch(:aliases).each { |name| emoji.add_alias(name) }
raw_emoji.fetch(:aliases).each { |name| emoji.add_alias(dedup.call(name)) }
if raw = raw_emoji[:emoji]
append_unicode.call(emoji, raw)
start_pos = 0
Expand All @@ -104,12 +112,12 @@ def parse_data_file
append_unicode.call(emoji, "#{raw}#{VARIATION_SELECTOR_16}")
end
end
raw_emoji.fetch(:tags).each { |tag| emoji.add_tag(tag) }
raw_emoji.fetch(:tags).each { |tag| emoji.add_tag(dedup.call(tag)) }

emoji.category = raw_emoji[:category]
emoji.description = raw_emoji[:description]
emoji.unicode_version = raw_emoji[:unicode_version]
emoji.ios_version = raw_emoji[:ios_version]
emoji.category = dedup.call(raw_emoji[:category])
emoji.description = dedup.call(raw_emoji[:description])
emoji.unicode_version = dedup.call(raw_emoji[:unicode_version])
emoji.ios_version = dedup.call(raw_emoji[:ios_version])
end
end
end
Expand Down

0 comments on commit 69009d4

Please sign in to comment.