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

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

Examples:

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

Parameters:

  • string (String)

    the string to camelcase

  • acronyms (Array[String]) (defaults to: [])

    the acronyms to use to prevent modifications

  • separator (String) (defaults to: "")

    the separator for linking words, by default none



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

.constantcaseObject

Converts string to a constant

Examples:

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

Parameters:

  • string (String)

    the string to turn into constant

  • acronyms (Array[String])

    the acronyms to use to prevent modifications

  • separator (String)

    the words separator, by default “_”



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

Examples:

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

Parameters:

  • string (String)

    the string to turn into constant

  • acronyms (Array[String])

    the acronyms to use to prevent modifications

  • separator (String) (defaults to: "_")

    the words separator, by default “_”



73
74
75
# File 'lib/strings/case.rb', line 73

def constcase(string, separator: "_")
  parsecase(string, sep: separator, casing: :upcase)
end

.dashcaseString

Converts string to lower case words linked by hyphenes

Examples:

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

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

Parameters:

  • string (String)

    the string to convert to dashed string

  • acronyms (Array[String])

    the acronyms to use to prevent modifications

  • separator (String)

    the separator for linking words, by default hyphen

Returns:

  • (String)


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

Examples:

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

Parameters:

  • string (String)

    the string to turn into header

  • acronyms (Array[String]) (defaults to: [])

    the acronyms to use to prevent modifications

  • separator (String) (defaults to: "-")

    the words separator, by default “-”



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

Examples:

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

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

Parameters:

  • string (String)

    the string to convert to dashed string

  • acronyms (Array[String]) (defaults to: [])

    the acronyms to use to prevent modifications

  • separator (String) (defaults to: "-")

    the separator for linking words, by default hyphen

Returns:

  • (String)


116
117
118
# File 'lib/strings/case.rb', line 116

def kebabcase(string, acronyms: [], separator: "-")
  parsecase(string, acronyms: acronyms, sep: separator)
end

.lower_camelcaseObject

Convert string to camel case:

  • start with a lowercase character

  • every subsequent word has its first character uppercased

  • all words are compounded together

Examples:

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

Parameters:

  • string (String)

    the string to camelcase

  • acronyms (Array[String])

    the acronyms to use to prevent modifications

  • separator (String)

    the separator for linking words, by default none



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

Examples:

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

Parameters:

  • string (String)

    the string to convert to camel case with capital letter

  • acronyms (Array[String]) (defaults to: [])

    the acronyms to use to prevent modifications

  • separator (String) (defaults to: "")

    the separator for linking words, by default none



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.

Examples:

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

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

Parameters:

  • string (String)

    the string to convert to file path

  • acronyms (Array[String]) (defaults to: [])

    the acronyms to use to prevent modifications

  • separator (String) (defaults to: "/")

    the separator for linking words, by default ‘/`



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

Examples:

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

Parameters:

  • string (String)

    the string to convert to sentence

  • acronyms (Array[String]) (defaults to: [])

    the acronyms to use to prevent modifications

  • separator (String) (defaults to: " ")

    the separator for linking words, by default a space



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

Examples:

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

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

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

Parameters:

  • string (String)

    the string to convert to snake case

  • acronyms (Array[String]) (defaults to: [])

    the acronyms to use to prevent modifications

  • separator (String) (defaults to: "_")

    the separator for linking words, by default ‘_`



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

Returns:

  • (Array[String])

    the split 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

Examples:

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

Parameters:

  • string (String)

    the string to convert to title case

  • acronyms (Array[String]) (defaults to: [])

    the acronyms to use to prevent modifications

  • separator (String) (defaults to: " ")

    the separator for linking words, by default a space



229
230
231
# File 'lib/strings/case.rb', line 229

def titlecase(string, acronyms: [], separator: " ")
  parsecase(string, acronyms: acronyms, sep: separator, casing: :capitalize)
end

.underscoreObject

Convert string into a snake_case

Examples:

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

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

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

Parameters:

  • string (String)

    the string to convert to snake case

  • acronyms (Array[String])

    the acronyms to use to prevent modifications

  • separator (String)

    the separator for linking words, by default ‘_`



213
214
215
# File 'lib/strings/case.rb', line 213

def snakecase(string, acronyms: [], separator: "_")
  parsecase(string, acronyms: acronyms, sep: separator)
end

.upper_camelcaseObject

Convert string to pascal case:

  • every word has its first character uppercased

  • all words are compounded together

Examples:

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

Parameters:

  • string (String)

    the string to convert to camel case with capital letter

  • acronyms (Array[String])

    the acronyms to use to prevent modifications

  • separator (String)

    the separator for linking words, by default none



144
145
146
# File 'lib/strings/case.rb', line 144

def pascalcase(string, acronyms: [], separator: "")
  parsecase(string, acronyms: acronyms, sep: separator, casing: :capitalize)
end