lib/lucky_case.rb in lucky_case-0.2.4 vs lib/lucky_case.rb in lucky_case-1.0.0

- old
+ new

@@ -118,11 +118,11 @@ # @param [String] string # @return [Boolean] def self.valid_case_string?(string) self.case(string) != nil end - + #---------------------------------------------------------------------------------------------------- # UPPER CASE #---------------------------------------------------------------------------------------------------- # Convert all characters inside the string @@ -614,23 +614,30 @@ #---------------------------------------------------------------------------------------------------- # MIXED CASE #---------------------------------------------------------------------------------------------------- # Convert the given string from any case - # into mixed case + # into mixed case. # + # The new string is ensured to be different from the input. + # # @example conversion # 'this-isAnExample_string' => 'This-Is_anExample-string' # # @param [String] string to convert # @param [Boolean] preserve_prefixed_underscores # @return [String] def self.mixed_case(string, preserve_prefixed_underscores: true) a = split_case_string string - converted = '' - a.each do |part| - converted += self.send random_case, part + converted = nil + loop do + converted = '' + a.each do |part| + converted += self.send CASES.keys.sample, part + end + converted = self.send CASES.keys.sample, converted + break if converted != string && underscores_at_start(string) + converted != string end if preserve_prefixed_underscores underscores_at_start(string) + converted else converted @@ -639,12 +646,17 @@ # Check if the string is a valid mixed case (without special characters!) # # @param [String] string to check # @return [Boolean] - def self.mixed_case?(string) - _case_match? string, :mixed_case + def self.mixed_case?(string, allow_prefixed_underscores: true) + s = if allow_prefixed_underscores + cut_underscores_at_start string + else + string + end + _case_match? s, :mixed_case end #---------------------------------------------------------------------------------------------------- # SWAP CASE #---------------------------------------------------------------------------------------------------- @@ -748,10 +760,10 @@ end #---------------------------------------------------------------------------------------------------- # HELPERS #---------------------------------------------------------------------------------------------------- - + # Return string without underscores at the start # # @param [String] string # @return [String] string without prefixed underscores def self.cut_underscores_at_start(string) \ No newline at end of file