lucky_case 0.2.2 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8a436c523f2a63b7411b894d146a89725c153dfd3970f3f22d49be49dbd35882
4
- data.tar.gz: 3b3faaac0ec66f6f47e82b3a802cdd2ce71b666a70ae01f6fea5c4e344303b37
3
+ metadata.gz: a02e8e5b6c87d69698ea74ce773d0db80054d0f95df611cf6787c8b64ca2ef9a
4
+ data.tar.gz: 65618b4882eafd8ef600cd798e9bad39a50b0bf3094ff140c6c6e17542fbe038
5
5
  SHA512:
6
- metadata.gz: 146fee7851d89d9e1dacba0dac0012cf223285fb94e2d723270fe0111af0c4b366714bb8ffdc2836387b48a1046421cf45bdaca5c3822409f800b602554a1afd
7
- data.tar.gz: a5c74f03bb733a25a3a246bdebdc27c04c1ee187bc973356e9893f678c50d4ef72c5fb149633319e0bc5649f133502b22aa224b6c64947363edcd9c0da8ae4b6
6
+ metadata.gz: 929b76eb4c553e6e1e404d33f2b60d6e8506f376fe3524ace0178e93d2b93da786e779d9b6b24d57faa4ad631edb43ae95319a9a74691d6779b5027dbba6db3f
7
+ data.tar.gz: 69120e28465009a311bdce99ffc49b071250f02cf5be846ef0a6256a7464e14f74b4a01609adcd4782e8bdf13980e3d71a4d4c8d64f91eea0138881644475881
data/README.md CHANGED
@@ -25,7 +25,7 @@ Useful when working with conventions, where class names, method names and file n
25
25
 
26
26
  You can either use the static LuckyCase class with its method or optionally monkey patch the String class.
27
27
 
28
- ### Use the static class only
28
+ ### Approach 1: Using the static class
29
29
  ```ruby
30
30
  require 'lucky_case'
31
31
 
@@ -42,8 +42,10 @@ LuckyCase.upper_word_case('PascalToUpperWord') # => 'PASCAL TO UPPER WOR
42
42
  LuckyCase.capital_word_case('snake_to_capital_word') # => 'Snake To Capital Word'
43
43
  LuckyCase.sentence_case('snake_to_sentence_case') # => 'Snake to sentence case'
44
44
  LuckyCase.mixed_case('example_snake_string') # => 'Example-snake_STRING'
45
+
45
46
  # converter by type
46
47
  LuckyCase.convert_case('some_snake', :pascal_case) # => 'SomeSnake'
48
+
47
49
  # transformers
48
50
  LuckyCase.lower_case('Some_FuckingShit') # => 'some_fuckingshit'
49
51
  LuckyCase.upper_case('Some_FuckingShit') # => 'SOME_FUCKINGSHIT'
@@ -53,11 +55,13 @@ LuckyCase.capitalize('example') # => 'Example'
53
55
  LuckyCase.constantize('some_constant') # => SomeConstant
54
56
  LuckyCase.constantize('SOME_CONSTANT') # => SomeConstant
55
57
  LuckyCase.constantize('some/path_example/folder') # => Some::PathExample::Folder
56
- LuckyCase.deconstantize(SomeConstant) # => 'some_constant'
58
+ LuckyCase.deconstantize(SomeConstant) # => 'some_constant' // default case_type: :snake_case
57
59
  LuckyCase.deconstantize(Some::PathExample::Folder, case_type: :camel_case) # => 'some/pathExample/folder'
60
+
58
61
  # identifiers
59
62
  LuckyCase.case('this_can_only_be_snake_case') # => :snake_case
60
63
  LuckyCase.cases('validformultiple') # => [ :snake_case, :camel_case, :dash_case, :word_case ]
64
+
61
65
  # checkers
62
66
  LuckyCase.snake_case?('valid_snake_case') # => true
63
67
  LuckyCase.upper_snake_case?('UPPER_SNAKE') # => true
@@ -81,7 +85,7 @@ LuckyCase.valid_case_string?('validString') # => true
81
85
  LuckyCase.valid_case_string?('1nV4lid$tring') # => false
82
86
  ```
83
87
 
84
- ### Monkey patch the string class
88
+ ### Approach 2: Monkey patch the string class
85
89
 
86
90
  With monkey patching you can access the same methods (except deconstantize, valid_case_type?) of LuckyCase directly from strings.
87
91
  Additionally they provide versions with exclamation mark for direct manipulation.
@@ -96,10 +100,12 @@ a = 'ExampleString'
96
100
  a.pascal_case? # => true
97
101
  a.snake_case # => 'example_string'
98
102
  a # => 'ExampleString'
103
+
99
104
  # string variable manipulation
100
105
  a.snake_case! # => 'example_string'
101
106
  a # => 'example_string'
102
107
  ...
108
+
103
109
  # identifiers
104
110
  # got a other method name here because 'case' might be to common and cause conflicts
105
111
  b = 'example'
@@ -77,9 +77,9 @@ module LuckyCase
77
77
  end
78
78
  if matched_cases.empty?
79
79
  nil
80
- # reject mixed case if there are other matches
81
- # because it would always be included if one other case matches
82
80
  elsif matched_cases.size > 1
81
+ # reject :mixed_case if there are other matches
82
+ # because it would always be included if one other case matches
83
83
  matched_cases.reject { |e| e == :mixed_case }
84
84
  else
85
85
  matched_cases
@@ -115,12 +115,12 @@ module LuckyCase
115
115
 
116
116
  # Check if the string matches any of the available cases
117
117
  #
118
- # @param [String] case_type
118
+ # @param [String] string
119
119
  # @return [Boolean]
120
120
  def self.valid_case_string?(string)
121
121
  self.case(string) != nil
122
122
  end
123
-
123
+
124
124
  #----------------------------------------------------------------------------------------------------
125
125
  # UPPER CASE
126
126
  #----------------------------------------------------------------------------------------------------
@@ -616,7 +616,9 @@ module LuckyCase
616
616
  #----------------------------------------------------------------------------------------------------
617
617
 
618
618
  # Convert the given string from any case
619
- # into mixed case
619
+ # into mixed case.
620
+ #
621
+ # The new string is ensured to be different from the input.
620
622
  #
621
623
  # @example conversion
622
624
  # 'this-isAnExample_string' => 'This-Is_anExample-string'
@@ -626,9 +628,14 @@ module LuckyCase
626
628
  # @return [String]
627
629
  def self.mixed_case(string, preserve_prefixed_underscores: true)
628
630
  a = split_case_string string
629
- converted = ''
630
- a.each do |part|
631
- converted += self.send random_case, part
631
+ converted = nil
632
+ loop do
633
+ converted = ''
634
+ a.each do |part|
635
+ converted += self.convert_case part, CASES.keys.sample, preserve_prefixed_underscores: preserve_prefixed_underscores
636
+ end
637
+ converted = self.convert_case converted, CASES.keys.sample, preserve_prefixed_underscores: preserve_prefixed_underscores
638
+ break if converted != string && underscores_at_start(string) + converted != string
632
639
  end
633
640
  if preserve_prefixed_underscores
634
641
  underscores_at_start(string) + converted
@@ -641,8 +648,13 @@ module LuckyCase
641
648
  #
642
649
  # @param [String] string to check
643
650
  # @return [Boolean]
644
- def self.mixed_case?(string)
645
- _case_match? string, :mixed_case
651
+ def self.mixed_case?(string, allow_prefixed_underscores: true)
652
+ s = if allow_prefixed_underscores
653
+ cut_underscores_at_start string
654
+ else
655
+ string
656
+ end
657
+ _case_match? s, :mixed_case
646
658
  end
647
659
 
648
660
  #----------------------------------------------------------------------------------------------------
@@ -750,7 +762,7 @@ module LuckyCase
750
762
  #----------------------------------------------------------------------------------------------------
751
763
  # HELPERS
752
764
  #----------------------------------------------------------------------------------------------------
753
-
765
+
754
766
  # Return string without underscores at the start
755
767
  #
756
768
  # @param [String] string
@@ -802,10 +814,10 @@ module LuckyCase
802
814
  # Check if the given case matches the string
803
815
  #
804
816
  # @param [String] string
805
- # @param [Symbol,String] case_to_match
817
+ # @param [Symbol,String] case_type
806
818
  # @return [Boolean]
807
- def self._case_match?(string, case_to_match)
808
- !!(string =~ CASES[case_to_match.to_sym])
819
+ def self._case_match?(string, case_type)
820
+ !!(string =~ CASES[case_type.to_sym])
809
821
  end
810
822
 
811
823
  #----------------------------------------------------------------------------------------------------
@@ -21,6 +21,11 @@ class String
21
21
  #
22
22
  # @param [Boolean] allow_prefixed_underscores
23
23
  # @return [Symbol,nil] symbol of type, nil if no match
24
+ def letter_case(allow_prefixed_underscores: true)
25
+ LuckyCase.case self, allow_prefixed_underscores: allow_prefixed_underscores
26
+ end
27
+
28
+ # easter egg version of #letter_case
24
29
  def lucky_case(allow_prefixed_underscores: true)
25
30
  LuckyCase.case self, allow_prefixed_underscores: allow_prefixed_underscores
26
31
  end
@@ -29,6 +34,11 @@ class String
29
34
  #
30
35
  # @param [Boolean] allow_prefixed_underscores
31
36
  # @return [Array<Symbol>,nil] symbols of types, nil if no one matches
37
+ def letter_cases(allow_prefixed_underscores: true)
38
+ LuckyCase.cases self, allow_prefixed_underscores: allow_prefixed_underscores
39
+ end
40
+
41
+ # easter egg version of #letter_cases
32
42
  def lucky_cases(allow_prefixed_underscores: true)
33
43
  LuckyCase.cases self, allow_prefixed_underscores: allow_prefixed_underscores
34
44
  end
@@ -1,3 +1,3 @@
1
1
  module LuckyCase
2
- VERSION = '0.2.2'.freeze
2
+ VERSION = '1.0.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lucky_case
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthäus J. N. Beyrle
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-14 00:00:00.000000000 Z
11
+ date: 2020-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler