lib/string/cases.rb in bblib-0.3.0 vs lib/string/cases.rb in bblib-0.4.1
- old
+ new
@@ -1,29 +1,26 @@
module BBLib
-
- def self.title_case str, first_only: true
- ignoreables = ['a', 'an', 'the', 'on', 'upon', 'and', 'but', 'or', 'in', 'with', 'to']
+ def self.title_case(str, first_only: true)
+ str = str.to_s unless str.is_a?(String)
+ ignoreables = %w(a an the on upon and but or in with to)
regx = /[[:space:]]+|\-|\_|\"|\'|\(|\)|\[|\]|\{|\}|\#/
spacing = str.scan(regx).to_a
words = str.split(regx).map do |word|
if ignoreables.include?(word.downcase)
word.downcase
+ elsif first_only
+ word.to_s.slice(0,1).to_s.upcase + word.to_s[1..-1].to_s
else
- if first_only
- word[0] = word[0].upcase
- word
- else
- word.capitalize
- end
+ word.capitalize
end
end
# Always cap the first word
- words.first.capitalize
+ words[0] = words.first.to_s.slice(0,1).to_s.upcase + words.first.to_s[1..-1].to_s
words.interleave(spacing).join
end
- def self.start_case str, first_only: false
+ def self.start_case(str, first_only: false)
regx = /[[:space:]]+|\-|\_|\"|\'|\(|\)|\[|\]|\{|\}|\#/
spacing = str.scan(regx).to_a
words = str.split(regx).map do |word|
if first_only
word[0] = word[0].upcase
@@ -33,64 +30,75 @@
end
end
words.interleave(spacing).join
end
- def self.camel_case str, style = :lower
+ def self.camel_case(str, style = :lower)
regx = /[[:space:]]+|[^[[:alnum:]]]+/
- words = str.split(regx).map do |word|
- word.capitalize
- end
+ words = str.split(regx).map(&:capitalize)
words[0].downcase! if style == :lower
words.join
end
- def self.delimited_case str, delimiter = '_'
+ def self.delimited_case(str, delimiter = '_')
regx = /[[:space:]]+|[^[[:alnum:]]]+|\#{delimiter}+/
- words = str.split(regx).join(delimiter)
+ str.split(regx).join(delimiter)
end
- def self.snake_case str
+ def self.snake_case(str)
BBLib.delimited_case str, '_'
end
- def self.spinal_case str
+ def self.method_case(str)
+ str.gsub(/(?<=[^^])([A-Z])/, '_\1').gsub(/\s+/, ' ').snake_case.downcase
+ end
+
+ def self.class_case(str)
+ str.gsub(/(?<=[^^])([A-Z])/, ' \1').gsub(/\s+/, ' ').title_case.gsub(/\s+|\_/, '')
+ end
+
+ def self.spinal_case(str)
BBLib.delimited_case str, '-'
end
- def self.train_case str
+ def self.train_case(str)
BBLib.spinal_case(BBLib.start_case(str))
end
-
end
class String
-
- def title_case first_only: false
- BBLib.title_case self, first_only:first_only
+ def title_case(first_only: false)
+ BBLib.title_case self, first_only: first_only
end
- def start_case first_only: false
- BBLib.start_case self, first_only:first_only
+ def start_case(first_only: false)
+ BBLib.start_case self, first_only: first_only
end
- def camel_case style = :lower
+ def camel_case(style = :lower)
BBLib.camel_case self, style
end
- def delimited_case delimiter = '_'
+ def delimited_case(delimiter = '_')
BBLib.delimited_case self, delimiter
end
def snake_case
BBLib.snake_case self
end
+ def method_case
+ BBLib.method_case(self)
+ end
+
+ def class_case
+ BBLib.class_case(self)
+ end
+
def spinal_case
BBLib.spinal_case self
end
def train_case
BBLib.train_case self
end
-
end