Module: Flatito::Utils

Included in:
Base
Defined in:
lib/flatito/utils.rb

Instance Method Summary collapse

Instance Method Details

#truncate(string, max: 50, match_position: nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/flatito/utils.rb', line 5

def truncate(string, max: 50, match_position: nil)
  return string if string.length <= max

  if match_position && match_position > max
    start = [match_position - (max / 2), 0].max
    ending = start + max
    result = string[start...ending]
    result = "...#{result}" if start.positive?
    result = "#{result}..." if ending < string.length
    result
  else
    "#{string[0...max]}..."
  end
end