Class: Worldwide::BusinessNames

Inherits:
Object
  • Object
show all
Defined in:
lib/worldwide/business_names.rb

Class Method Summary collapse

Class Method Details

.abbreviated(name:, ideal_max_length: 3) ⇒ Object

Returns a script aware abbreviation, or nil when the name cannot be abbreviated.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/worldwide/business_names.rb', line 7

def abbreviated(name:, ideal_max_length: 3)
  name_stripped = name&.strip

  return if Util.blank?(name_stripped)
  return unless ideal_max_length.is_a?(Integer) && ideal_max_length.positive?

  scripts = Scripts.identify(text: name_stripped)
  return unless scripts.one?

  case scripts.first
  when :Latin
    latin_abbreviation(name_stripped, ideal_max_length)
  when :Hangul
    name_stripped.split(" ", 2).first.grapheme_clusters[0, ideal_max_length].join
  when :Thai
    # Thai does not use spaces between words.
    return if name_stripped.include?(" ")

    name_stripped.grapheme_clusters.first
  when :Han, :Katakana, :Hiragana
    return if name_stripped.include?(" ")

    name_stripped
  end
end