lib/lucky_case.rb in lucky_case-0.2.1 vs lib/lucky_case.rb in lucky_case-0.2.2

- old
+ new

@@ -20,11 +20,11 @@ train_case: /^([[:upper:]][[:lower:]0-9]*\-|[0-9]+\-)*([[:upper:]][[:lower:]0-9]*)$/, word_case: /^[[:lower:]]{1}[[:lower:] 0-9]+$/, upper_word_case: /^[[:upper:]]{1}[[:upper:] 0-9]+$/, capital_word_case: /^([[:upper:]][[:lower:]0-9]*\ |[0-9]+\ )*([[:upper:]][[:lower:]0-9]*)$/, sentence_case: /^[[:upper:]]{1}[[:lower:] 0-9]+$/, - mixed_case: /^[[:upper:][:lower:]][[:upper:][:lower:]_\-0-9]*$/, + mixed_case: /^[[:upper:][:lower:]][[:upper:][:lower:]_\-0-9 ]*$/, } FORMATS = { capital: /^[[:upper:]]{1}.*$/, upper_case: /^[^[:lower:]]+$/, @@ -99,15 +99,35 @@ end error_message = "Invalid case type '#{case_type}'. Valid types are: #{CASES.keys.join(', ')}" raise InvalidCaseError.new error_message end + # Check if given case type is a valid case type + # + # @param [Symbol, String] case_type + # @return [Boolean] + def self.valid_case_type?(case_type) + if CASES.keys.include? case_type.to_sym + true + else + false + end + end + + # Check if the string matches any of the available cases + # + # @param [String] case_type + # @return [Boolean] + def self.valid_case_string?(string) + self.case(string) != nil + end + #---------------------------------------------------------------------------------------------------- # UPPER CASE #---------------------------------------------------------------------------------------------------- - # Converts all characters inside the string + # Convert all characters inside the string # into upper case # # @example conversion # 'this-isAnExample_string' => 'THIS-ISANEXAMPLE_STRING' # @@ -115,11 +135,11 @@ # @return [String] def self.upper_case(string) string.upcase end - # Checks if all characters inside the string are upper case + # Check if all characters inside the string are upper case # # @param [String] string to check # @return [Boolean] def self.upper_case?(string) string == upper_case(string) @@ -127,11 +147,11 @@ #---------------------------------------------------------------------------------------------------- # LOWER CASE #---------------------------------------------------------------------------------------------------- - # Converts all characters inside the string + # Convert all characters inside the string # into lower case # # @example conversion # 'this-isAnExample_string' => 'this-isanexample_string' # @@ -139,11 +159,11 @@ # @return [String] def self.lower_case(string) string.downcase end - # Checks if all characters inside the string are lower case + # Check if all characters inside the string are lower case # # @param [String] string to check # @return [Boolean] def self.lower_case?(string) string == lower_case(string) @@ -151,11 +171,11 @@ #---------------------------------------------------------------------------------------------------- # SNAKE CASE #---------------------------------------------------------------------------------------------------- - # Converts the given string from any case + # Convert the given string from any case # into snake case # # @example conversion # 'this-isAnExample_string' => 'this_is_an_example_string' # @@ -170,11 +190,11 @@ else converted end end - # Checks if the string is snake case + # Check if the string is snake case # # @param [String] string to check # @param [Boolean] allow_prefixed_underscores # @return [Boolean] def self.snake_case?(string, allow_prefixed_underscores: true) @@ -184,11 +204,11 @@ string end _case_match? s, :snake_case end - # Converts the given string from any case + # Convert the given string from any case # into upper snake case # # @example conversion # 'this-isAnExample_string' => 'THIS_IS_AN_EXAMPLE_STRING' # @@ -203,11 +223,11 @@ else converted end end - # Checks if the string is upper snake case + # Check if the string is upper snake case # # @param [String] string to check # @param [Boolean] allow_prefixed_underscores # @return [Boolean] def self.upper_snake_case?(string, allow_prefixed_underscores: true) @@ -221,11 +241,11 @@ #---------------------------------------------------------------------------------------------------- # PASCAL CASE #---------------------------------------------------------------------------------------------------- - # Converts the given string from any case + # Convert the given string from any case # into pascal case # # @example conversion # 'this-isAnExample_string' => 'ThisIsAnExampleString' # @@ -240,11 +260,11 @@ else converted end end - # Checks if the string is upper pascal case + # Check if the string is upper pascal case # # @param [String] string to check # @param [Boolean] allow_prefixed_underscores # @return [Boolean] def self.pascal_case?(string, allow_prefixed_underscores: true) @@ -258,11 +278,11 @@ #---------------------------------------------------------------------------------------------------- # CAMEL CASE #---------------------------------------------------------------------------------------------------- - # Converts the given string from any case + # Convert the given string from any case # into camel case # # @example conversion # 'this-isAnExample_string' => 'thisIsAnExampleString' # @@ -277,11 +297,11 @@ else converted end end - # Checks if the string is camel case + # Check if the string is camel case # # @param [String] string to check # @param [Boolean] allow_prefixed_underscores # @return [Boolean] def self.camel_case?(string, allow_prefixed_underscores: true) @@ -295,11 +315,11 @@ #---------------------------------------------------------------------------------------------------- # DASH CASE #---------------------------------------------------------------------------------------------------- - # Converts the given string from any case + # Convert the given string from any case # into dash case # # @example conversion # 'this-isAnExample_string' => 'this-is-an-example-string' # @@ -314,11 +334,11 @@ else converted end end - # Checks if the string is dash case + # Check if the string is dash case # # @param [String] string to check # @param [Boolean] allow_prefixed_underscores # @return [Boolean] def self.dash_case?(string, allow_prefixed_underscores: true) @@ -328,11 +348,11 @@ string end _case_match? s, :dash_case end - # Converts the given string from any case + # Convert the given string from any case # into upper dash case # # @example conversion # 'this-isAnExample_string' => 'THIS-IS-AN-EXAMPLE-STRING' # @@ -342,11 +362,11 @@ def self.upper_dash_case(string, preserve_prefixed_underscores: true) s = dash_case string, preserve_prefixed_underscores: preserve_prefixed_underscores upper_case s end - # Checks if the string is upper dash case + # Check if the string is upper dash case # # @param [String] string to check # @param [Boolean] allow_prefixed_underscores # @return [Boolean] def self.upper_dash_case?(string, allow_prefixed_underscores: true) @@ -360,11 +380,11 @@ #---------------------------------------------------------------------------------------------------- # TRAIN CASE #---------------------------------------------------------------------------------------------------- - # Converts the given string from any case + # Convert the given string from any case # into train case # # @example conversion # 'this-isAnExample_string' => 'This-Is-An-Example-String' # @@ -379,11 +399,11 @@ else converted end end - # Checks if the string is train case + # Check if the string is train case # # @param [String] string to check # @param [Boolean] allow_prefixed_underscores # @return [Boolean] def self.train_case?(string, allow_prefixed_underscores: true) @@ -397,11 +417,11 @@ #---------------------------------------------------------------------------------------------------- # WORD CASE #---------------------------------------------------------------------------------------------------- - # Converts the given string from any case + # Convert the given string from any case # into word case # # @example conversion # 'this-isAnExample_string' => 'this is an example string' # @@ -416,11 +436,11 @@ else converted end end - # Checks if the string is word case + # Check if the string is word case # # @param [String] string to check # @param [Boolean] allow_prefixed_underscores # @return [Boolean] def self.word_case?(string, allow_prefixed_underscores: true) @@ -430,11 +450,11 @@ string end _case_match? s, :word_case end - # Converts the given string from any case + # Convert the given string from any case # into upper word case # # @example conversion # 'this-isAnExample_string' => 'THIS IS AN EXAMPLE STRING' # @@ -449,11 +469,11 @@ else converted end end - # Checks if the string is upper word case + # Check if the string is upper word case # # @param [String] string to check # @param [Boolean] allow_prefixed_underscores # @return [Boolean] def self.upper_word_case?(string, allow_prefixed_underscores: true) @@ -463,11 +483,11 @@ string end _case_match? s, :upper_word_case end - # Converts the given string from any case + # Convert the given string from any case # into capital word case # # @example conversion # 'this-isAnExample_string' => 'This Is An Example String' # @@ -482,11 +502,11 @@ else converted end end - # Checks if the string is capital word case + # Check if the string is capital word case # # @param [String] string to check # @param [Boolean] allow_prefixed_underscores # @return [Boolean] def self.capital_word_case?(string, allow_prefixed_underscores: true) @@ -500,11 +520,11 @@ #---------------------------------------------------------------------------------------------------- # SENTENCE CASE #---------------------------------------------------------------------------------------------------- - # Converts the given string from any case + # Convert the given string from any case # into sentence case # # @example conversion # 'this-isAnExample_string' => 'This is an example string' # @@ -519,11 +539,11 @@ else converted end end - # Checks if the string is sentence case + # Check if the string is sentence case # # @param [String] string to check # @param [Boolean] allow_prefixed_underscores # @return [Boolean] def self.sentence_case?(string, allow_prefixed_underscores: true) @@ -537,11 +557,11 @@ #---------------------------------------------------------------------------------------------------- # CAPITALIZE #---------------------------------------------------------------------------------------------------- - # Converts the first character to capital + # Convert the first character to capital # # @param [String] string to convert # @param [Boolean] skip_prefixed_underscores # @return [String] def self.capital(string, skip_prefixed_underscores: false) @@ -557,20 +577,20 @@ else s end end - # Converts the first character to capital + # Convert the first character to capital # # @param [String] string to convert # @param [Boolean] skip_prefixed_underscores # @return [String] def self.capitalize(string, skip_prefixed_underscores: false) capital string, skip_prefixed_underscores: skip_prefixed_underscores end - # Checks if the strings first character is a capital letter + # Check if the strings first character is a capital letter # # @param [String] string to check # @param [Boolean] skip_prefixed_underscores # @return [Boolean] def self.capital?(string, skip_prefixed_underscores: false) @@ -580,11 +600,11 @@ string end _case_match? s, :capital end - # Checks if the strings first character is a capital letter + # Check if the strings first character is a capital letter # # @param [String] string to check # @param [Boolean] skip_prefixed_underscores # @return [Boolean] def self.capitalized?(string, skip_prefixed_underscores: false) @@ -593,11 +613,11 @@ #---------------------------------------------------------------------------------------------------- # MIXED CASE #---------------------------------------------------------------------------------------------------- - # Converts the given string from any case + # Convert the given string from any case # into mixed case # # @example conversion # 'this-isAnExample_string' => 'This-Is_anExample-string' # @@ -615,11 +635,11 @@ else converted end end - # Checks if the string is a valid mixed case (without special characters!) + # 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 @@ -670,11 +690,11 @@ #---------------------------------------------------------------------------------------------------- # CONSTANTIZE #---------------------------------------------------------------------------------------------------- - # Converts the string from any case + # Convert the string from any case # into pascal case and casts it into a constant # # @example conversion # 'this-isAnExample_string' => ThisIsAnExampleString # 'this/is_an/example_path' => This::IsAn::ExamplePath @@ -728,10 +748,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