闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
693 B

  1. # frozen_string_literal: true
  2. require 'singleton'
  3. class Emoji
  4. include Singleton
  5. def initialize
  6. data = Oj.load(File.open(File.join(Rails.root, 'lib', 'assets', 'emoji.json')))
  7. @map = {}
  8. data.each do |_, emoji|
  9. keys = [emoji['shortname']] + emoji['aliases']
  10. unicode = codepoint_to_unicode(emoji['unicode'])
  11. keys.each do |key|
  12. @map[key] = unicode
  13. end
  14. end
  15. end
  16. def unicode(shortcode)
  17. @map[shortcode]
  18. end
  19. def names
  20. @map.keys
  21. end
  22. private
  23. def codepoint_to_unicode(codepoint)
  24. if codepoint.include?('-')
  25. codepoint.split('-').map(&:hex).pack('U*')
  26. else
  27. [codepoint.hex].pack('U')
  28. end
  29. end
  30. end