Class: Rack::Padlock::StringUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/padlock/string_util.rb

Instance Method Summary collapse

Instance Method Details

#elide(string, max) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/rack/padlock/string_util.rb', line 4

def elide(string, max)
  length = string.length
  return string unless length > max
  return string if max <= 0
  amount_to_preserve_on_the_left = (max/2.0).ceil
  amount_to_preserve_on_the_right = max - amount_to_preserve_on_the_left
  left = string[0..(amount_to_preserve_on_the_left-1)]
  right = string[-amount_to_preserve_on_the_right..-1]
  "#{left}...#{right}"
end