Skip to content

Commit

Permalink
add checkers for valid case type and valid case string
Browse files Browse the repository at this point in the history
  • Loading branch information
magynhard committed Aug 14, 2020
1 parent b3d4091 commit aed1e4a
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 13 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'

Expand All @@ -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 ]
```


Expand Down
20 changes: 20 additions & 0 deletions lib/lucky_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#----------------------------------------------------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions lib/lucky_case/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
#----------------------------------------------------------------------------------------------------
Expand Down
78 changes: 67 additions & 11 deletions spec/lucky_case_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

#----------------------------------------------------------------------------------------------------
30 changes: 30 additions & 0 deletions spec/lucky_case_string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit aed1e4a

Please sign in to comment.