From aed1e4a6afca165eec989e912d6596a90f1ad6a8 Mon Sep 17 00:00:00 2001 From: magynhard Date: Fri, 14 Aug 2020 14:45:03 +0200 Subject: [PATCH] add checkers for valid case type and valid case string --- README.md | 16 ++++++- lib/lucky_case.rb | 20 +++++++++ lib/lucky_case/string.rb | 7 +++ spec/lucky_case_spec.rb | 78 +++++++++++++++++++++++++++++----- spec/lucky_case_string_spec.rb | 30 +++++++++++++ 5 files changed, 138 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 522e2da..8c667e6 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ LuckyCase.constantize('SOME_CONSTANT') # => SomeConstant LuckyCase.constantize('some/path_example/folder') # => Some::PathExample::Folder LuckyCase.deconstantize(SomeConstant) # => 'some_constant' LuckyCase.deconstantize(Some::PathExample::Folder, case_type: :camel_case) # => 'some/pathExample/folder' -# identifier +# identifiers LuckyCase.case('this_can_only_be_snake_case') # => :snake_case LuckyCase.cases('validformultiple') # => [ :snake_case, :camel_case, :dash_case, :word_case ] # checkers @@ -75,13 +75,19 @@ LuckyCase.upper_case?('UPPER50984') # => true LuckyCase.lower_case?('lower_cheese') # => true LuckyCase.capital?('Some') # => true LuckyCase.capitalized?('some') # => false +LuckyCase.valid_case_type?(:snake_case) # => true +LuckyCase.valid_case_type?(:apple_case) # => false +LuckyCase.valid_case_string?('validString') # => true +LuckyCase.valid_case_string?('1nV4lid$tring') # => false ``` ### Monkey patch the string class -With monkey patching you can access the same methods (except deconstantize) of LuckyCase directly from strings. +With monkey patching you can access the same methods (except deconstantize, valid_case_type?) of LuckyCase directly from strings. Additionally they provide versions with exclamation mark for direct manipulation. +Because the method #case is so general and could lead to conflicts, it is called #letter_case here. + ```ruby require 'lucky_case/string' @@ -93,6 +99,12 @@ a # => 'ExampleString' # string variable manipulation a.snake_case! # => 'example_string' a # => 'example_string' +... +# identifiers +# got a other method name here because 'case' might be to common and cause conflicts +b = 'example' +b.letter_case # => :snake_case +b.letter_cases # => [ :snake_case, :camel_case, :dash_case, :word_case ] ``` diff --git a/lib/lucky_case.rb b/lib/lucky_case.rb index 3e82eda..2f48ed0 100644 --- a/lib/lucky_case.rb +++ b/lib/lucky_case.rb @@ -101,6 +101,26 @@ def self.convert_case(string, case_type, preserve_prefixed_underscores: true) 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 #---------------------------------------------------------------------------------------------------- diff --git a/lib/lucky_case/string.rb b/lib/lucky_case/string.rb index 643531f..f17a946 100644 --- a/lib/lucky_case/string.rb +++ b/lib/lucky_case/string.rb @@ -46,6 +46,13 @@ def convert_case!(case_type, preserve_prefixed_underscores: true) set_self_value self.convert_case(case_type, preserve_prefixed_underscores: preserve_prefixed_underscores) end + # Check if the string matches any of the available cases + # + # @return [Boolean] + def valid_case_string?() + LuckyCase.case(self) != nil + end + #---------------------------------------------------------------------------------------------------- # UPPER CASE #---------------------------------------------------------------------------------------------------- diff --git a/spec/lucky_case_spec.rb b/spec/lucky_case_spec.rb index d5c46a5..538a96a 100644 --- a/spec/lucky_case_spec.rb +++ b/spec/lucky_case_spec.rb @@ -274,7 +274,7 @@ expect(result).to eql(true) end it 'can check invalid snake case' do - all_cases.reject { |k,v| k == :snake_case }.each do |k,v| + all_cases.reject { |k, v| k == :snake_case }.each do |k, v| result = LuckyCase.snake_case?(v) expect(result).to eql(false), "Failed with '#{k}'" end @@ -284,7 +284,7 @@ expect(result).to eql(true) end it 'can check invalid upper snake case' do - all_cases.reject { |k,v| k == :upper_snake_case }.each do |k,v| + all_cases.reject { |k, v| k == :upper_snake_case }.each do |k, v| result = LuckyCase.upper_snake_case?(v) expect(result).to eql(false), "Failed with '#{k}'" end @@ -294,7 +294,7 @@ expect(result).to eql(true) end it 'can check invalid pascal case' do - all_cases.reject { |k,v| k == :pascal_case }.each do |k,v| + all_cases.reject { |k, v| k == :pascal_case }.each do |k, v| result = LuckyCase.pascal_case?(v) expect(result).to eql(false), "Failed with '#{k}'" end @@ -304,7 +304,7 @@ expect(result).to eql(true) end it 'can check invalid camel case' do - all_cases.reject { |k,v| k == :camel_case }.each do |k,v| + all_cases.reject { |k, v| k == :camel_case }.each do |k, v| result = LuckyCase.camel_case?(v) expect(result).to eql(false), "Failed with '#{k}'" end @@ -314,7 +314,7 @@ expect(result).to eql(true) end it 'can check invalid dash case' do - all_cases.reject { |k,v| k == :dash_case }.each do |k,v| + all_cases.reject { |k, v| k == :dash_case }.each do |k, v| result = LuckyCase.dash_case?(v) expect(result).to eql(false), "Failed with '#{k}'" end @@ -324,7 +324,7 @@ expect(result).to eql(true) end it 'can check invalid upper dash case' do - all_cases.reject { |k,v| k == :upper_dash_case }.each do |k,v| + all_cases.reject { |k, v| k == :upper_dash_case }.each do |k, v| result = LuckyCase.upper_dash_case?(v) expect(result).to eql(false), "Failed with '#{k}'" end @@ -334,7 +334,7 @@ expect(result).to eql(true) end it 'can check invalid train case' do - all_cases.reject { |k,v| k == :train_case }.each do |k,v| + all_cases.reject { |k, v| k == :train_case }.each do |k, v| result = LuckyCase.train_case?(v) expect(result).to eql(false), "Failed with '#{k}'" end @@ -344,7 +344,7 @@ expect(result).to eql(true) end it 'can check invalid word case' do - all_cases.reject { |k,v| k == :word_case }.each do |k,v| + all_cases.reject { |k, v| k == :word_case }.each do |k, v| result = LuckyCase.word_case?(v) expect(result).to eql(false), "Failed with '#{k}'" end @@ -354,7 +354,7 @@ expect(result).to eql(true) end it 'can check invalid upper word case' do - all_cases.reject { |k,v| k == :upper_word_case }.each do |k,v| + all_cases.reject { |k, v| k == :upper_word_case }.each do |k, v| result = LuckyCase.upper_word_case?(v) expect(result).to eql(false), "Failed with '#{k}'" end @@ -364,7 +364,7 @@ expect(result).to eql(true) end it 'can check invalid capital word case' do - all_cases.reject { |k,v| k == :capital_word_case }.each do |k,v| + all_cases.reject { |k, v| k == :capital_word_case }.each do |k, v| result = LuckyCase.capital_word_case?(v) expect(result).to eql(false), "Failed with '#{k}'" end @@ -374,7 +374,7 @@ expect(result).to eql(true) end it 'can check invalid sentence case' do - all_cases.reject { |k,v| k == :sentence_case }.each do |k,v| + all_cases.reject { |k, v| k == :sentence_case }.each do |k, v| result = LuckyCase.sentence_case?(v) expect(result).to eql(false), "Failed with '#{k}'" end @@ -1327,3 +1327,59 @@ end #---------------------------------------------------------------------------------------------------- + +RSpec.describe LuckyCase, '#valid_case_type?' do + context 'check valid case types: ' do + it "recognizes valid case type 'snake_case'" do + expect(LuckyCase.valid_case_type?(:snake_case)).to eql(true) + end + it "recognizes valid case type 'upper_dash_case'" do + expect(LuckyCase.valid_case_type?(:upper_dash_case)).to eql(true) + end + it "recognizes all defined case types" do + LuckyCase::CASES.keys.each do |c| + expect(LuckyCase.valid_case_type?(c)).to eql(true), "Unrecognized case type '#{c}'" + end + end + end + context 'check invalid case types: ' do + it "does not recognize invalid case type 'apple_case'" do + expect(LuckyCase.valid_case_type?(:apple_case)).to eql(false) + end + it "does not recognize invalid case type 'banana_case'" do + expect(LuckyCase.valid_case_type?(:banana_case)).to eql(false) + end + end +end + +#---------------------------------------------------------------------------------------------------- + +RSpec.describe LuckyCase, '#valid_case_string?' do + context 'check valid case strings: ' do + it "recognizes valid case string 'snake_case'" do + expect(LuckyCase.valid_case_string?('snake_case')).to eql(true) + end + it "recognizes valid case string 'UPPER-DASH-CASE'" do + expect(LuckyCase.valid_case_string?('UPPER-DASH-CASE')).to eql(true) + end + it "recognizes valid case string 'word case string'" do + expect(LuckyCase.valid_case_string?('word case string')).to eql(true) + end + it "recognizes valid case string 'some Mixed-case_string'" do + expect(LuckyCase.valid_case_string?('some Mixed-case_string')).to eql(true) + end + end + context 'check invalid case strings: ' do + it "does not recognize invalid case string '4pple Cais'" do + expect(LuckyCase.valid_case_string?('4pple Cais')).to eql(false) + end + it "does not recognize invalid case string '$pecial'" do + expect(LuckyCase.valid_case_string?('$pecial')).to eql(false) + end + it "does not recognize invalid case string ')(§/$=)?'" do + expect(LuckyCase.valid_case_string?(')(§/$=)?')).to eql(false) + end + end +end + +#---------------------------------------------------------------------------------------------------- diff --git a/spec/lucky_case_string_spec.rb b/spec/lucky_case_string_spec.rb index f7cac15..5f74034 100644 --- a/spec/lucky_case_string_spec.rb +++ b/spec/lucky_case_string_spec.rb @@ -85,3 +85,33 @@ module TestCaseModule::SubModule end end end end + +#---------------------------------------------------------------------------------------------------- + +RSpec.describe String, '#valid_case_string?' do + context 'check valid case strings: ' do + it "recognizes valid case string 'snake_case'" do + expect('snake_case'.valid_case_string?).to eql(true) + end + it "recognizes valid case string 'UPPER-DASH-CASE'" do + expect('UPPER-DASH-CASE'.valid_case_string?).to eql(true) + end + it "recognizes valid case string 'word case string'" do + expect('word case string'.valid_case_string?).to eql(true) + end + it "recognizes valid case string 'some Mixed-case_string'" do + expect('some Mixed-case_string'.valid_case_string?).to eql(true) + end + end + context 'check invalid case strings: ' do + it "does not recognize invalid case string '4pple Cais'" do + expect('4pple Cais'.valid_case_string?).to eql(false) + end + it "does not recognize invalid case string '$pecial'" do + expect('$pecial'.valid_case_string?).to eql(false) + end + it "does not recognize invalid case string ')(§/$=)?'" do + expect(')(§/$=)?'.valid_case_string?).to eql(false) + end + end +end \ No newline at end of file