Class: Gitlab::Danger::EmojiChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/danger/emoji_checker.rb

Constant Summary collapse

DIGESTS =
File.expand_path('../../../fixtures/emojis/digests.json', __dir__)
ALIASES =
File.expand_path('../../../fixtures/emojis/aliases.json', __dir__)
LIKELY_EMOJI =

A regex that indicates a piece of text might include an Emoji. The regex alone is not enough, as we'd match `:foo:bar:baz`. Instead, we use this regex to save us from having to check for all possible emoji names when we know one definitely is not included.

/:[\+a-z0-9_\-]+:/.freeze
UNICODE_EMOJI_REGEX =
%r{(
  [\u{1F300}-\u{1F5FF}] |
  [\u{1F1E6}-\u{1F1FF}] |
  [\u{2700}-\u{27BF}] |
  [\u{1F900}-\u{1F9FF}] |
  [\u{1F600}-\u{1F64F}] |
  [\u{1F680}-\u{1F6FF}] |
  [\u{2600}-\u{26FF}]
)}x.freeze

Instance Method Summary collapse

Constructor Details

#initializeEmojiChecker

Returns a new instance of EmojiChecker.


27
28
29
30
31
32
# File 'lib/gitlab/danger/emoji_checker.rb', line 27

def initialize
  names = JSON.parse(File.read(DIGESTS)).keys +
    JSON.parse(File.read(ALIASES)).keys

  @emoji = names.map { |name| ":#{name}:" }
end

Instance Method Details

#includes_text_emoji?(text) ⇒ Boolean

Returns:

  • (Boolean)

34
35
36
37
38
# File 'lib/gitlab/danger/emoji_checker.rb', line 34

def includes_text_emoji?(text)
  return false unless text.match?(LIKELY_EMOJI)

  @emoji.any? { |emoji| text.include?(emoji) }
end

#includes_unicode_emoji?(text) ⇒ Boolean

Returns:

  • (Boolean)

40
41
42
# File 'lib/gitlab/danger/emoji_checker.rb', line 40

def includes_unicode_emoji?(text)
  text.match?(UNICODE_EMOJI_REGEX)
end