Class: Ncase::Words
- Inherits:
-
Object
- Object
- Ncase::Words
- 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:
-
If the string contains spaces ‘
\x20
’, any sequence of whitespace is a separator. -
If the string contains hyphens ‘
-
’ or underscores ‘_
’, whichever is more frequent is a separator. -
A case-switch is a separator.
Instance Method Summary collapse
-
#camel_case ⇒ String
The
camelCase
representation of the string. -
#initialize(str, separator: nil) ⇒ Words
constructor
A new instance of Words.
-
#inver_title_case ⇒ String
The tITLE cASE representation of the string.
-
#kebab_case ⇒ String
The
kebab-case
representation of the string. -
#lower_case ⇒ String
The lower case representation of the string.
-
#pascal_case ⇒ String
The
PascalCase
representation of the string. -
#random_case ⇒ String
A rAnDOm CaSe representation of the string.
-
#snake_case ⇒ String
The
snake_case
representation of the string. -
#title_case ⇒ String
The Title Case representation of the string.
-
#upper_case ⇒ String
The UPPER CASE representation of the string.
-
#upper_kebab_case ⇒ String
The
KEBAB-CASE
representation of the string. -
#upper_snake_case ⇒ String
The
SNAKE_CASE
representation of the string.
Constructor Details
#initialize(str, separator: nil) ⇒ Words
Returns a new instance of Words.
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_case ⇒ String
Returns 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_case ⇒ String
Returns 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_case ⇒ String
Returns 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_case ⇒ String
Returns 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_case ⇒ String
Returns 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_case ⇒ String
Returns 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_case ⇒ String
Returns 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_case ⇒ String
Returns 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_case ⇒ String
Returns 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_case ⇒ String
Returns 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_case ⇒ String
Returns 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 |