Module: Kennel::StringUtils

Defined in:
lib/kennel/string_utils.rb

Class Method Summary collapse

Class Method Details

.parameterize(string) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/kennel/string_utils.rb', line 20

def parameterize(string)
  string
    .downcase
    .gsub(/[^a-z0-9\-_]+/, "-") # remove unsupported
    .gsub(/-{2,}/, "-") # remove duplicates
    .gsub(/^-|-$/, "") # remove leading/trailing
end

.snake_case(string) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/kennel/string_utils.rb', line 5

def snake_case(string)
  string
    .gsub(/::/, "_") # Foo::Bar -> foo_bar
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') # FOOBar -> foo_bar
    .gsub(/([a-z\d])([A-Z])/, '\1_\2') # fooBar -> foo_bar
    .tr("-", "_") # foo-bar -> foo_bar
    .downcase
end

.title_case(string) ⇒ Object

for child projects, not used internally



15
16
17
# File 'lib/kennel/string_utils.rb', line 15

def title_case(string)
  string.split(/[\s_]/).map(&:capitalize) * " "
end

.truncate_lines(text, to:, warning:) ⇒ Object



28
29
30
31
32
# File 'lib/kennel/string_utils.rb', line 28

def truncate_lines(text, to:, warning:)
  lines = text.split(/\n/, to + 1)
  lines[-1] = warning if lines.size > to
  lines.join("\n")
end