Module: Cyrillic::UaPassport

Defined in:
lib/cyrillic/ua_passport.rb

Constant Summary collapse

CHARACTER_TABLE =
k.zip(v).to_h.freeze

Class Method Summary collapse

Class Method Details

.transliterate(str = "") ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cyrillic/ua_passport.rb', line 11

def transliterate(str = "")
  return "" if str.nil? || str.empty?

  str = str.to_s.gsub('Зг', 'Zgh').gsub('зг', 'zgh')

  result = +""
  str.each_char.with_index do |char, index|
    prev_char = index.positive? ? str[index - 1] : nil
    is_word_start = prev_char.nil? || prev_char == ' '

    case char
    when 'Є'
      result << (is_word_start ? 'Ye' : 'Ie')
    when 'є'
      result << (is_word_start ? 'ye' : 'ie')
    when 'Ї'
      result << (is_word_start ? 'Yi' : 'I')
    when 'ї'
      result << (is_word_start ? 'yi' : 'i')
    when 'Й'
      result << (is_word_start ? 'Y' : 'I')
    when 'й'
      result << (is_word_start ? 'y' : 'i')
    when 'Ю'
      result << (is_word_start ? 'Yu' : 'Iu')
    when 'ю'
      result << (is_word_start ? 'yu' : 'iu')
    when 'Я'
      result << (is_word_start ? 'Ya' : 'Ia')
    when 'я'
      result << (is_word_start ? 'ya' : 'ia')
    when 'Ь', 'ь', "'"
      # Ignored in transliteration
    else
      result << (CHARACTER_TABLE[char] || char)
    end
  end
  result
end