class Ncase::Words

Implements efficient conversion of a string into a multitude of case styles.

By default will guess the separator in the string:

  1. If the string contains spaces '\x20', any sequence of whitespace is a separator.

  2. If the string contains hyphens '-' or underscores '_', whichever is more frequent is a separator.

  3. A case-switch is a separator.

@example Convert a string into PascalCase

require "ncase"

w = Ncase::Words.new("this is a test string")
p w.pascal_case  # => "ThisIsATestString"

Constants

CASE_SEP_REGEXP
HYPHEN_SEP_REGEXP
SPACE_SEP_REGEXP
UNDERSCORE_SEP_REGEXP

Public Class Methods

new(s, separator: nil) click to toggle source

@param s [String] the string to convert @param separator [Regexp] the pattern to split the string into words.

If +nil+, it will guess the separator as described in {Words}.

@see String#split

# File lib/ncase/words.rb, line 26
def initialize(s, separator: nil)
  ss = s.strip
  separator ||= guess_separator(ss)
  @words = ss.split(separator)
end

Public Instance Methods

camel_case() click to toggle source

@return [String] the camelCase representation of the string

# File lib/ncase/words.rb, line 33
def camel_case
  first_word = @words.first
  if first_word
    @words.drop(1)
      .map {|s| s.capitalize}
      .unshift(first_word.downcase)
      .join
  else
    ""
  end
end
inver_title_case() click to toggle source

@return [String] the +tITLE cASE+ representation of the string

# File lib/ncase/words.rb, line 86
def inver_title_case
  title_case.swapcase
end
kebab_case() click to toggle source

@return [String] the kebab-case representation of the string

# File lib/ncase/words.rb, line 51
def kebab_case
  @words.map {|s| s.downcase}.join("-")
end
lower_case() click to toggle source

@return [String] the +lower case+ representation of the string

# File lib/ncase/words.rb, line 61
def lower_case
  @words.map {|s| s.downcase}.join(" ")
end
pascal_case() click to toggle source

@return [String] the PascalCase representation of the string

# File lib/ncase/words.rb, line 46
def pascal_case
  @words.map {|s| s.capitalize}.join
end
random_case() click to toggle source

@return [String] a +rAnDOm CaSe+ representation of the string

# File lib/ncase/words.rb, line 91
def random_case
  @words.join(" ")
    .chars
    .map {|c| if rand(2) == 0 then c.downcase else c.upcase end}
    .join
end
snake_case() click to toggle source

@return [String] the snake_case representation of the string

# File lib/ncase/words.rb, line 71
def snake_case
  @words.map {|s| s.downcase}.join("_")
end
title_case() click to toggle source

@return [String] the +Title Case+ representation of the string

# File lib/ncase/words.rb, line 81
def title_case
  @words.map {|s| s.capitalize}.join(" ")
end
upper_case() click to toggle source

@return [String] the +UPPER CASE+ representation of the string

# File lib/ncase/words.rb, line 66
def upper_case
  @words.map {|s| s.upcase}.join(" ")
end
upper_kebab_case() click to toggle source

@return [String] the KEBAB-CASE representation of the string

# File lib/ncase/words.rb, line 56
def upper_kebab_case
  @words.map {|s| s.upcase}.join("-")
end
upper_snake_case() click to toggle source

@return [String] the SNAKE_CASE representation of the string

# File lib/ncase/words.rb, line 76
def upper_snake_case
  @words.map {|s| s.upcase}.join("_")
end

Private Instance Methods

guess_separator(s) click to toggle source

@return [Regexp] the most likely separator for the string

# File lib/ncase/words.rb, line 109
def guess_separator(s)
  if s.include?(" ")
    SPACE_SEP_REGEXP
  else
    num_both = s.count("-_")
    if num_both > 0
      num_hyphens = s.count("-")
      if num_hyphens * 2 >= num_both
        HYPHEN_SEP_REGEXP
      else
        UNDERSCORE_SEP_REGEXP
      end
    else
      CASE_SEP_REGEXP
    end
  end
end