module Strings::Case

Constants

DELIMITERS
DELIMS
DIGITS
DOWN_LETTERS
LOWERCASE
NONALPHANUMERIC
NONALPHAS
UPPERCASE
UP_LETTERS
VERSION

Public Class Methods

camelcase(string, acronyms: [], separator: "") click to toggle source

Convert string to camel case:

  • start with a lowercase character

  • every subsequent word has its first character uppercased

  • all words are compounded together

@example

camelcase("foo bar baz") # => "fooBarBaz"

@param [String] string

the string to camelcase

@param [Array] acronyms

the acronyms to use to prevent modifications

@param [String] separator

the separator for linking words, by default none

@api public

# File lib/strings/case.rb, line 47
def camelcase(string, acronyms: [], separator: "")
  res = parsecase(string, acronyms: acronyms, sep: separator, casing: :capitalize)

  return res if res.to_s.empty?

  acronyms_regex = /^(#{acronyms.join("|")})/
  if !acronyms.empty? && (res =~ acronyms_regex)
    res
  else
    res[0].downcase + res[1..-1]
  end
end
Also aliased as: lower_camelcase
constantcase(string, separator: "_")
Alias for: constcase
constcase(string, separator: "_") click to toggle source

Converts string to a constant

@example

constantcase("foo bar baz") # => "FOO_BAR_BAZ"

@param [String] string

the string to turn into constant

@param [Array] acronyms

the acronyms to use to prevent modifications

@param [String] separator

the words separator, by default "_"

@api public

# File lib/strings/case.rb, line 77
def constcase(string, separator: "_")
  parsecase(string, sep: separator, casing: :upcase)
end
Also aliased as: constantcase
dashcase(string, acronyms: [], separator: "-")
Alias for: kebabcase
headercase(string, acronyms: [], separator: "-") click to toggle source

Convert string to a HTTP Header

@example

headercase("foo bar baz") # = "Foo-Bar-Baz"

@param [String] string

the string to turn into header

@param [Array] acronyms

the acronyms to use to prevent modifications

@param [String] separator

the words separator, by default "-"

@api public

# File lib/strings/case.rb, line 98
def headercase(string, acronyms: [], separator: "-")
  parsecase(string, acronyms: acronyms, sep: separator, casing: :capitalize)
end
kebabcase(string, acronyms: [], separator: "-") click to toggle source

Converts string to lower case words linked by hyphenes

@example

kebabcase("fooBarBaz") # => "foo-bar-baz"

kebabcase("__FOO_BAR__") # => "foo-bar"

@param [String] string

the string to convert to dashed string

@param [Array] acronyms

the acronyms to use to prevent modifications

@param [String] separator

the separator for linking words, by default hyphen

@return [String]

@api public

# File lib/strings/case.rb, line 120
def kebabcase(string, acronyms: [], separator: "-")
  parsecase(string, acronyms: acronyms, sep: separator)
end
Also aliased as: dashcase
lower_camelcase(string, acronyms: [], separator: "")
Alias for: camelcase
pascalcase(string, acronyms: [], separator: "") click to toggle source

Convert string to pascal case:

  • every word has its first character uppercased

  • all words are compounded together

@example

pascalcase("foo bar baz") # => "FooBarBaz"

@param [String] string

the string to convert to camel case with capital letter

@param [Array] acronyms

the acronyms to use to prevent modifications

@param [String] separator

the separator for linking words, by default none

@api public

# File lib/strings/case.rb, line 143
def pascalcase(string, acronyms: [], separator: "")
  parsecase(string, acronyms: acronyms, sep: separator, casing: :capitalize)
end
Also aliased as: upper_camelcase
pathcase(string, acronyms: [], separator: "/") click to toggle source

Convert string into a file path.

By default uses `/` as a path separator.

@example

pathcase("foo bar baz") # => "foo/bar/baz"

pathcase("FooBarBaz") # => "foo/bar/baz"

@param [String] string

the string to convert to file path

@param [Array] acronyms

the acronyms to use to prevent modifications

@param [String] separator

the separator for linking words, by default `/`

@api public

# File lib/strings/case.rb, line 168
def pathcase(string, acronyms: [], separator: "/")
  parsecase(string, acronyms: acronyms, sep: separator)
end
sentencecase(string, acronyms: [], separator: " ") click to toggle source

Convert string int a sentence

@example

sentencecase("foo bar baz") # => "Foo bar baz"

@param [String] string

the string to convert to sentence

@param [Array] acronyms

the acronyms to use to prevent modifications

@param [String] separator

the separator for linking words, by default a space

@api public

# File lib/strings/case.rb, line 186
def sentencecase(string, acronyms: [], separator: " ")
  res = parsecase(string, acronyms: acronyms, sep: separator, casing: :downcase)

  return res if res.to_s.empty?

  res[0].upcase + res[1..-1]
end
snakecase(string, acronyms: [], separator: "_") click to toggle source

Convert string into a snake_case

@example

snakecase("foo bar baz") # => "foo_bar_baz"

snakecase("ЗдравствуйтеПривет") # => "здравствуйте_привет"

snakecase("HTTPResponse") # => "http_response"

@param [String] string

the string to convert to snake case

@param [Array] acronyms

the acronyms to use to prevent modifications

@param [String] separator

the separator for linking words, by default `_`

@api public

# File lib/strings/case.rb, line 212
def snakecase(string, acronyms: [], separator: "_")
  parsecase(string, acronyms: acronyms, sep: separator)
end
Also aliased as: underscore
split_into_words(string, sep: nil) click to toggle source

Split string into words

@return [Array]

the split words

@api private

# File lib/strings/case.rb, line 262
def split_into_words(string, sep: nil)
  words = []
  word = []
  scanner = StringScanner.new(string)

  while !scanner.eos?
    if scanner.match?(UPPERCASE)
      char = scanner.getch
      if word.size <= 1 # don't allow single letter words
        word << char
      else
        word << char
        words << word.join
        word = []
      end
    elsif scanner.match?(LOWERCASE)
      char = scanner.getch
      if word.size <= 1 # don't allow single letter words
        word << char
      else
        words << word.join
        word = [char]
      end
    elsif scanner.match?(DELIMS)
      char = scanner.getch
      words << word.join unless word.empty?
      if scanner.pos == 1 && char == sep
        words << ""
      elsif scanner.eos? && char == sep
        word = [""]
      else
        word = []
      end
    elsif scanner.skip(NONALPHAS)
      # noop
    else
      word << scanner.getch
    end
  end

  words << word.join unless word.empty?

  words
end
titlecase(string, acronyms: [], separator: " ") click to toggle source

Convert string into a title case

@example

titlecase("foo bar baz") # => "Foo Bar Baz"

@param [String] string

the string to convert to title case

@param [Array] acronyms

the acronyms to use to prevent modifications

@param [String] separator

the separator for linking words, by default a space

@api public

# File lib/strings/case.rb, line 233
def titlecase(string, acronyms: [], separator: " ")
  parsecase(string, acronyms: acronyms, sep: separator, casing: :capitalize)
end
underscore(string, acronyms: [], separator: "_")
Alias for: snakecase
upper_camelcase(string, acronyms: [], separator: "")
Alias for: pascalcase

Private Class Methods

parsecase(string, acronyms: [], sep: "_", casing: :downcase) click to toggle source

Parse string and transform to desired case

@api private

# File lib/strings/case.rb, line 241
def parsecase(string, acronyms: [], sep: "_", casing: :downcase)
  return if string.nil?

  words = split_into_words(string, sep: sep)

  no_case = ->(w) { acronyms.include?(w) ? w.extend(NullCase) : w }

  words
    .map(&no_case)
    .map(&casing)
    .join(sep)
end

Private Instance Methods

camelcase(string, acronyms: [], separator: "") click to toggle source

Convert string to camel case:

  • start with a lowercase character

  • every subsequent word has its first character uppercased

  • all words are compounded together

@example

camelcase("foo bar baz") # => "fooBarBaz"

@param [String] string

the string to camelcase

@param [Array] acronyms

the acronyms to use to prevent modifications

@param [String] separator

the separator for linking words, by default none

@api public

# File lib/strings/case.rb, line 47
def camelcase(string, acronyms: [], separator: "")
  res = parsecase(string, acronyms: acronyms, sep: separator, casing: :capitalize)

  return res if res.to_s.empty?

  acronyms_regex = /^(#{acronyms.join("|")})/
  if !acronyms.empty? && (res =~ acronyms_regex)
    res
  else
    res[0].downcase + res[1..-1]
  end
end
Also aliased as: lower_camelcase
constantcase(string, separator: "_")
Alias for: constcase
constcase(string, separator: "_") click to toggle source

Converts string to a constant

@example

constantcase("foo bar baz") # => "FOO_BAR_BAZ"

@param [String] string

the string to turn into constant

@param [Array] acronyms

the acronyms to use to prevent modifications

@param [String] separator

the words separator, by default "_"

@api public

# File lib/strings/case.rb, line 77
def constcase(string, separator: "_")
  parsecase(string, sep: separator, casing: :upcase)
end
Also aliased as: constantcase
dashcase(string, acronyms: [], separator: "-")
Alias for: kebabcase
headercase(string, acronyms: [], separator: "-") click to toggle source

Convert string to a HTTP Header

@example

headercase("foo bar baz") # = "Foo-Bar-Baz"

@param [String] string

the string to turn into header

@param [Array] acronyms

the acronyms to use to prevent modifications

@param [String] separator

the words separator, by default "-"

@api public

# File lib/strings/case.rb, line 98
def headercase(string, acronyms: [], separator: "-")
  parsecase(string, acronyms: acronyms, sep: separator, casing: :capitalize)
end
kebabcase(string, acronyms: [], separator: "-") click to toggle source

Converts string to lower case words linked by hyphenes

@example

kebabcase("fooBarBaz") # => "foo-bar-baz"

kebabcase("__FOO_BAR__") # => "foo-bar"

@param [String] string

the string to convert to dashed string

@param [Array] acronyms

the acronyms to use to prevent modifications

@param [String] separator

the separator for linking words, by default hyphen

@return [String]

@api public

# File lib/strings/case.rb, line 120
def kebabcase(string, acronyms: [], separator: "-")
  parsecase(string, acronyms: acronyms, sep: separator)
end
Also aliased as: dashcase
lower_camelcase(string, acronyms: [], separator: "")
Alias for: camelcase
parsecase(string, acronyms: [], sep: "_", casing: :downcase) click to toggle source

Parse string and transform to desired case

@api private

# File lib/strings/case.rb, line 241
def parsecase(string, acronyms: [], sep: "_", casing: :downcase)
  return if string.nil?

  words = split_into_words(string, sep: sep)

  no_case = ->(w) { acronyms.include?(w) ? w.extend(NullCase) : w }

  words
    .map(&no_case)
    .map(&casing)
    .join(sep)
end
pascalcase(string, acronyms: [], separator: "") click to toggle source

Convert string to pascal case:

  • every word has its first character uppercased

  • all words are compounded together

@example

pascalcase("foo bar baz") # => "FooBarBaz"

@param [String] string

the string to convert to camel case with capital letter

@param [Array] acronyms

the acronyms to use to prevent modifications

@param [String] separator

the separator for linking words, by default none

@api public

# File lib/strings/case.rb, line 143
def pascalcase(string, acronyms: [], separator: "")
  parsecase(string, acronyms: acronyms, sep: separator, casing: :capitalize)
end
Also aliased as: upper_camelcase
pathcase(string, acronyms: [], separator: "/") click to toggle source

Convert string into a file path.

By default uses `/` as a path separator.

@example

pathcase("foo bar baz") # => "foo/bar/baz"

pathcase("FooBarBaz") # => "foo/bar/baz"

@param [String] string

the string to convert to file path

@param [Array] acronyms

the acronyms to use to prevent modifications

@param [String] separator

the separator for linking words, by default `/`

@api public

# File lib/strings/case.rb, line 168
def pathcase(string, acronyms: [], separator: "/")
  parsecase(string, acronyms: acronyms, sep: separator)
end
sentencecase(string, acronyms: [], separator: " ") click to toggle source

Convert string int a sentence

@example

sentencecase("foo bar baz") # => "Foo bar baz"

@param [String] string

the string to convert to sentence

@param [Array] acronyms

the acronyms to use to prevent modifications

@param [String] separator

the separator for linking words, by default a space

@api public

# File lib/strings/case.rb, line 186
def sentencecase(string, acronyms: [], separator: " ")
  res = parsecase(string, acronyms: acronyms, sep: separator, casing: :downcase)

  return res if res.to_s.empty?

  res[0].upcase + res[1..-1]
end
snakecase(string, acronyms: [], separator: "_") click to toggle source

Convert string into a snake_case

@example

snakecase("foo bar baz") # => "foo_bar_baz"

snakecase("ЗдравствуйтеПривет") # => "здравствуйте_привет"

snakecase("HTTPResponse") # => "http_response"

@param [String] string

the string to convert to snake case

@param [Array] acronyms

the acronyms to use to prevent modifications

@param [String] separator

the separator for linking words, by default `_`

@api public

# File lib/strings/case.rb, line 212
def snakecase(string, acronyms: [], separator: "_")
  parsecase(string, acronyms: acronyms, sep: separator)
end
Also aliased as: underscore
split_into_words(string, sep: nil) click to toggle source

Split string into words

@return [Array]

the split words

@api private

# File lib/strings/case.rb, line 262
def split_into_words(string, sep: nil)
  words = []
  word = []
  scanner = StringScanner.new(string)

  while !scanner.eos?
    if scanner.match?(UPPERCASE)
      char = scanner.getch
      if word.size <= 1 # don't allow single letter words
        word << char
      else
        word << char
        words << word.join
        word = []
      end
    elsif scanner.match?(LOWERCASE)
      char = scanner.getch
      if word.size <= 1 # don't allow single letter words
        word << char
      else
        words << word.join
        word = [char]
      end
    elsif scanner.match?(DELIMS)
      char = scanner.getch
      words << word.join unless word.empty?
      if scanner.pos == 1 && char == sep
        words << ""
      elsif scanner.eos? && char == sep
        word = [""]
      else
        word = []
      end
    elsif scanner.skip(NONALPHAS)
      # noop
    else
      word << scanner.getch
    end
  end

  words << word.join unless word.empty?

  words
end
titlecase(string, acronyms: [], separator: " ") click to toggle source

Convert string into a title case

@example

titlecase("foo bar baz") # => "Foo Bar Baz"

@param [String] string

the string to convert to title case

@param [Array] acronyms

the acronyms to use to prevent modifications

@param [String] separator

the separator for linking words, by default a space

@api public

# File lib/strings/case.rb, line 233
def titlecase(string, acronyms: [], separator: " ")
  parsecase(string, acronyms: acronyms, sep: separator, casing: :capitalize)
end
underscore(string, acronyms: [], separator: "_")
Alias for: snakecase
upper_camelcase(string, acronyms: [], separator: "")
Alias for: pascalcase