Class: EmojiRegex::Maker

Inherits:
Object
  • Object
show all
Defined in:
lib/emoji-regex/maker.rb

Instance Method Summary collapse

Constructor Details

#initializeMaker

Returns a new instance of Maker.



3
4
5
# File 'lib/emoji-regex/maker.rb', line 3

def initialize
  @input_file_path = Fetcher.new.saved_path
end

Instance Method Details

#rangesObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/emoji-regex/maker.rb', line 24

def ranges
  prev = -1
  prev_hex = ''
  left = ''

  ranges = File.open(@input_file_path).map do |line|
    hex = line.to_i(16)
    if hex == prev || hex == prev + 1
      prev = hex
      prev_hex = line.chomp
      next
    end
    range = [left, prev_hex]
    left = line.chomp
    prev = hex
    prev_hex = left
    range
  end

  ranges
    .compact
    .reject { |a, b| a.empty? || b.empty? }
end

#regexObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/emoji-regex/maker.rb', line 7

def regex
  regex = ranges.map do |e|
    l = e[0]
    r = e[1]
    next if l == '' || r == '' || !l[/\s/].nil? || !r[/\s/].nil?
    if l == r
      "\\u{#{l}}"
    else
      "\\u{#{l}}-\\u{#{r}}"
    end
  end
  regex.unshift '['
  regex.push ']'

  Regexp.new(regex.join)
end