lib/string/cases.rb in bblib-0.4.1 vs lib/string/cases.rb in bblib-1.0.2
- old
+ new
@@ -1,10 +1,10 @@
module BBLib
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:]]+|\-|\_|\"|\'|\(|\)|\[|\]|\{|\}|\#/
+ regx = /\s+|\-|\_|(?<=\W|^)\"(?=\w|$)|(?<=\W|^)\'(?=\w|$)|\(|\)|\[|\]|\{|\}|\#/
spacing = str.scan(regx).to_a
words = str.split(regx).map do |word|
if ignoreables.include?(word.downcase)
word.downcase
elsif first_only
@@ -38,19 +38,19 @@
words[0].downcase! if style == :lower
words.join
end
def self.delimited_case(str, delimiter = '_')
- regx = /[[:space:]]+|[^[[:alnum:]]]+|\#{delimiter}+/
+ regx = /[[:space:]]+|\s+|[^\w\d]+|\#{delimiter}+/
str.split(regx).join(delimiter)
end
def self.snake_case(str)
- BBLib.delimited_case str, '_'
+ BBLib.delimited_case(str, '_')
end
def self.method_case(str)
- str.gsub(/(?<=[^^])([A-Z])/, '_\1').gsub(/\s+/, ' ').snake_case.downcase
+ str.gsub(/(?<=[^^])([A-Z])(?=[^A-Z\s])/, '_\1').gsub(/[\s\_]+/, '_').snake_case.downcase
end
def self.class_case(str)
str.gsub(/(?<=[^^])([A-Z])/, ' \1').gsub(/\s+/, ' ').title_case.gsub(/\s+|\_/, '')
end