lib/emoji-regex/maker.rb in emoji-regex-0.1.0 vs lib/emoji-regex/maker.rb in emoji-regex-0.1.1

- old
+ new

@@ -1,48 +1,48 @@ module EmojiRegex class Maker def initialize @input_file_path = Fetcher.new.saved_path - @prev = -1 - @prev_hex = '' - @left = '' - @ranges = [] end + 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 + def ranges - ranges = - File.open(@input_file_path).map do |line| - hex = line.to_i(16) + prev = -1 + prev_hex = '' + left = '' - if [@prev, @prev + 1].include?(hex) - @prev, @prev_hex = [hex, line.chomp] && next - else - [@left, @prev_hex].tap do - @left = line.chomp - @prev = hex - @prev_hex = @left - end - end + 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 { |ary| ary[0].empty? || ary[1].empty? } - end - - def regex - regex = '[' + - ranges.map do |range| - left = range[0] - right = range[1] - next if left == '' || right == '' || !left[/\s/].nil? || !right[/\s/].nil? - - if left == right - "\\u{#{left}}" - else - "\\u{#{left}}-\\u{#{right}}" - end - end.join + ']' - Regexp.new(regex) + .reject { |a, b| a.empty? || b.empty? } end end end