about summary refs log tree commit diff
path: root/app/lib
diff options
context:
space:
mode:
authorbeatrix-bitrot <beatrix.bitrot@gmail.com>2017-07-22 01:16:53 +0000
committerbeatrix-bitrot <beatrix.bitrot@gmail.com>2017-07-22 01:16:53 +0000
commit984d2d4cb626dd3a4da707ecf1e8ad45b476d8fb (patch)
tree9a7ba46c448cca4a4a245224bf421941a33c263d /app/lib
parent0244019ca17288802a144c84b7e0f319f1685695 (diff)
parent8d6c3cd48ae4f96752ff6b698bc7244d97aa9a27 (diff)
Merge that good fresh upstream shit
Diffstat (limited to 'app/lib')
-rw-r--r--app/lib/emoji.rb40
1 files changed, 40 insertions, 0 deletions
diff --git a/app/lib/emoji.rb b/app/lib/emoji.rb
new file mode 100644
index 000000000..e444b6893
--- /dev/null
+++ b/app/lib/emoji.rb
@@ -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