Use the same emoji data on the frontend and backend (#4284)
* Use the same emoji data on the frontend and backend * Move emoji.json to repository, add tests This way you don't need to install node dependencies if you only want to run Ruby code
This commit is contained in:
parent
c1bc5e14eb
commit
a390abdefb
5 changed files with 66 additions and 6 deletions
40
app/lib/emoji.rb
Normal file
40
app/lib/emoji.rb
Normal file
|
@ -0,0 +1,40 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'singleton'
|
||||
|
||||
class Emoji
|
||||
include Singleton
|
||||
|
||||
def initialize
|
||||
data = Oj.load(File.open(File.join(Rails.root, 'lib', 'assets', 'emoji.json')))
|
||||
|
||||
@map = {}
|
||||
|
||||
data.each do |_, emoji|
|
||||
keys = [emoji['shortname']] + emoji['aliases']
|
||||
unicode = codepoint_to_unicode(emoji['unicode'])
|
||||
|
||||
keys.each do |key|
|
||||
@map[key] = unicode
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def unicode(shortcode)
|
||||
@map[shortcode]
|
||||
end
|
||||
|
||||
def names
|
||||
@map.keys
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def codepoint_to_unicode(codepoint)
|
||||
if codepoint.include?('-')
|
||||
codepoint.split('-').map(&:hex).pack('U')
|
||||
else
|
||||
[codepoint.hex].pack('U')
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue