Module: Strings::Case
- Defined in:
- lib/strings/case.rb,
lib/strings/case/version.rb,
lib/strings/case/extensions.rb
Defined Under Namespace
Modules: Extensions, NullCase Classes: Error
Constant Summary collapse
- DIGITS =
("0".."9").freeze
- UP_LETTERS =
("A".."Z").freeze
- DOWN_LETTERS =
("a".."z").freeze
- DELIMITERS =
[" ", "\n", "\t", "_", ".", "-", "#", "?", "!"].freeze
- NONALPHANUMERIC =
(32..127).map(&:chr) - (DIGITS.to_a + DOWN_LETTERS.to_a + UP_LETTERS.to_a + DELIMITERS)
- UPCASE =
/(?<!\p{Lu})\p{Lu}$/.freeze
- LOWERCASE =
/\p{Lu}(?=\p{Ll})/.freeze
- VERSION =
"0.2.0"
Class Method Summary collapse
-
.camelcase(string, acronyms: [], separator: "") ⇒ Object
Convert string to camel case: * start with a lowercase character * every subsequent word has its first character uppercased * all words are compounded together.
-
.constantcase ⇒ Object
Converts string to a constant.
-
.constcase(string, separator: "_") ⇒ Object
Converts string to a constant.
-
.dashcase ⇒ String
Converts string to lower case words linked by hyphenes.
-
.headercase(string, acronyms: [], separator: "-") ⇒ Object
Convert string to a HTTP Header.
-
.kebabcase(string, acronyms: [], separator: "-") ⇒ String
Converts string to lower case words linked by hyphenes.
-
.lower_camelcase ⇒ Object
Convert string to camel case: * start with a lowercase character * every subsequent word has its first character uppercased * all words are compounded together.
-
.pascalcase(string, acronyms: [], separator: "") ⇒ Object
Convert string to pascal case: * every word has its first character uppercased * all words are compounded together.
-
.pathcase(string, acronyms: [], separator: "/") ⇒ Object
Convert string into a file path.
-
.sentencecase(string, acronyms: [], separator: " ") ⇒ Object
Convert string int a sentence.
-
.snakecase(string, acronyms: [], separator: "_") ⇒ Object
Convert string into a snake_case.
-
.split_into_words(string, sep: nil) ⇒ Array[String]
private
Split string into words.
-
.titlecase(string, acronyms: [], separator: " ") ⇒ Object
Convert string into a title case.
-
.underscore ⇒ Object
Convert string into a snake_case.
-
.upper_camelcase ⇒ Object
Convert string to pascal case: * every word has its first character uppercased * all words are compounded together.
Class Method Details
.camelcase(string, acronyms: [], separator: "") ⇒ Object
Convert string to camel case:
-
start with a lowercase character
-
every subsequent word has its first character uppercased
-
all words are compounded together
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/strings/case.rb', line 43 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 |
.constantcase ⇒ Object
Converts string to a constant
78 79 80 |
# File 'lib/strings/case.rb', line 78 def constcase(string, separator: "_") parsecase(string, sep: separator, casing: :upcase) end |
.constcase(string, separator: "_") ⇒ Object
Converts string to a constant
73 74 75 |
# File 'lib/strings/case.rb', line 73 def constcase(string, separator: "_") parsecase(string, sep: separator, casing: :upcase) end |
.dashcase ⇒ String
Converts string to lower case words linked by hyphenes
121 122 123 |
# File 'lib/strings/case.rb', line 121 def kebabcase(string, acronyms: [], separator: "-") parsecase(string, acronyms: acronyms, sep: separator) end |
.headercase(string, acronyms: [], separator: "-") ⇒ Object
Convert string to a HTTP Header
94 95 96 |
# File 'lib/strings/case.rb', line 94 def headercase(string, acronyms: [], separator: "-") parsecase(string, acronyms: acronyms, sep: separator, casing: :capitalize) end |
.kebabcase(string, acronyms: [], separator: "-") ⇒ String
Converts string to lower case words linked by hyphenes
116 117 118 |
# File 'lib/strings/case.rb', line 116 def kebabcase(string, acronyms: [], separator: "-") parsecase(string, acronyms: acronyms, sep: separator) end |
.lower_camelcase ⇒ Object
Convert string to camel case:
-
start with a lowercase character
-
every subsequent word has its first character uppercased
-
all words are compounded together
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/strings/case.rb', line 57 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 |
.pascalcase(string, acronyms: [], separator: "") ⇒ Object
Convert string to pascal case:
-
every word has its first character uppercased
-
all words are compounded together
139 140 141 |
# File 'lib/strings/case.rb', line 139 def pascalcase(string, acronyms: [], separator: "") parsecase(string, acronyms: acronyms, sep: separator, casing: :capitalize) end |
.pathcase(string, acronyms: [], separator: "/") ⇒ Object
Convert string into a file path.
By default uses ‘/` as a path separator.
164 165 166 |
# File 'lib/strings/case.rb', line 164 def pathcase(string, acronyms: [], separator: "/") parsecase(string, acronyms: acronyms, sep: separator) end |
.sentencecase(string, acronyms: [], separator: " ") ⇒ Object
Convert string int a sentence
182 183 184 185 186 187 188 |
# File 'lib/strings/case.rb', line 182 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: "_") ⇒ Object
Convert string into a snake_case
208 209 210 |
# File 'lib/strings/case.rb', line 208 def snakecase(string, acronyms: [], separator: "_") parsecase(string, acronyms: acronyms, sep: separator) end |
.split_into_words(string, sep: nil) ⇒ Array[String]
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Split string into words
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/strings/case.rb', line 258 def split_into_words(string, sep: nil) words = [] word = [] last = string.length - 1 string.each_char.with_index do |char, i| combine = word[-1].to_s + char if combine =~ UPCASE if word.size <= 1 # don't allow single letter words word << char else words << word.join word = [char] end elsif combine =~ LOWERCASE letter = word.pop if word.size <= 1 # don't allow single letter words word << letter << char else words << word.join word = [letter, char] end elsif DELIMITERS.include?(char) words << word.join unless word.empty? if i.zero? && char == sep words << "" else word = [] end elsif NONALPHANUMERIC.include?(char) # noop else word << char end if last == i word = [""] if char == sep words << word.join unless word.empty? end end words end |
.titlecase(string, acronyms: [], separator: " ") ⇒ Object
Convert string into a title case
229 230 231 |
# File 'lib/strings/case.rb', line 229 def titlecase(string, acronyms: [], separator: " ") parsecase(string, acronyms: acronyms, sep: separator, casing: :capitalize) end |
.underscore ⇒ Object
Convert string into a snake_case
213 214 215 |
# File 'lib/strings/case.rb', line 213 def snakecase(string, acronyms: [], separator: "_") parsecase(string, acronyms: acronyms, sep: separator) end |
.upper_camelcase ⇒ Object
Convert string to pascal case:
-
every word has its first character uppercased
-
all words are compounded together
144 145 146 |
# File 'lib/strings/case.rb', line 144 def pascalcase(string, acronyms: [], separator: "") parsecase(string, acronyms: acronyms, sep: separator, casing: :capitalize) end |