Class: Ncase::Words

Inherits:
Object
  • Object
show all
Defined in:
lib/ncase/words.rb

Overview

Implements efficient conversion of a string into multiple 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.

Examples:

Convert a string into PascalCase and snake_case

require "ncase"

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

Instance Method Summary collapse

Constructor Details

#initialize(str, separator: nil) ⇒ Words

Returns a new instance of Words.

Parameters:

  • str (String)

    the string to work with

  • separator (Regexp) (defaults to: nil)

    the pattern to split the string into words. If nil, it will guess the separator as described in Ncase::Words.

See Also:

  • String#split


25
26
27
28
29
# File 'lib/ncase/words.rb', line 25

def initialize(str, separator: nil)
  sstr = str.strip
  separator ||= guess_separator(sstr)
  @words = sstr.split(separator)
end

Instance Method Details

#camel_caseString

Returns the camelCase representation of the string.

Returns:

  • (String)

    the camelCase representation of the string



32
33
34
35
36
37
38
# File 'lib/ncase/words.rb', line 32

def camel_case
  return "" if @words.empty?

  @words.each(&:capitalize!)
  @words.first.downcase!
  @words.join
end

#inver_title_caseString

Returns the tITLE cASE representation of the string.

Returns:

  • (String)

    the tITLE cASE representation of the string



81
82
83
# File 'lib/ncase/words.rb', line 81

def inver_title_case
  @words.each(&:capitalize!).each(&:swapcase!).join(" ")
end

#kebab_caseString

Returns the kebab-case representation of the string.

Returns:

  • (String)

    the kebab-case representation of the string



46
47
48
# File 'lib/ncase/words.rb', line 46

def kebab_case
  @words.each(&:downcase!).join("-")
end

#lower_caseString

Returns the lower case representation of the string.

Returns:

  • (String)

    the lower case representation of the string



56
57
58
# File 'lib/ncase/words.rb', line 56

def lower_case
  @words.each(&:downcase!).join(" ")
end

#pascal_caseString

Returns the PascalCase representation of the string.

Returns:

  • (String)

    the PascalCase representation of the string



41
42
43
# File 'lib/ncase/words.rb', line 41

def pascal_case
  @words.each(&:capitalize!).join
end

#random_caseString

Returns a rAnDOm CaSe representation of the string.

Returns:

  • (String)

    a rAnDOm CaSe representation of the string



86
87
88
89
90
91
# File 'lib/ncase/words.rb', line 86

def random_case
  @words.join(" ")
        .chars
        .each { |c| rand(2).zero? ? c.downcase! : c.upcase! }
        .join
end

#snake_caseString

Returns the snake_case representation of the string.

Returns:

  • (String)

    the snake_case representation of the string



66
67
68
# File 'lib/ncase/words.rb', line 66

def snake_case
  @words.each(&:downcase!).join("_")
end

#title_caseString

Returns the Title Case representation of the string.

Returns:

  • (String)

    the Title Case representation of the string



76
77
78
# File 'lib/ncase/words.rb', line 76

def title_case
  @words.each(&:capitalize!).join(" ")
end

#upper_caseString

Returns the UPPER CASE representation of the string.

Returns:

  • (String)

    the UPPER CASE representation of the string



61
62
63
# File 'lib/ncase/words.rb', line 61

def upper_case
  @words.each(&:upcase!).join(" ")
end

#upper_kebab_caseString

Returns the KEBAB-CASE representation of the string.

Returns:

  • (String)

    the KEBAB-CASE representation of the string



51
52
53
# File 'lib/ncase/words.rb', line 51

def upper_kebab_case
  @words.each(&:upcase!).join("-")
end

#upper_snake_caseString

Returns the SNAKE_CASE representation of the string.

Returns:

  • (String)

    the SNAKE_CASE representation of the string



71
72
73
# File 'lib/ncase/words.rb', line 71

def upper_snake_case
  @words.each(&:upcase!).join("_")
end