corefines 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e3779e9f17fb899aa96118417ea1426efcdb53d9
4
- data.tar.gz: d72b1df9289ae8b439d698af8ae66b469d5aa492
3
+ metadata.gz: 6ff01826fd1321effe7725bd8de106831ae6400c
4
+ data.tar.gz: 6441dd63f2e8896861f092fce7c3001258aefdb1
5
5
  SHA512:
6
- metadata.gz: ca6c1f9221db16740578b134c66820695db1e3ac332e80803c3ff204da2cb77c7ae2374ab43ba0f327c434a3d7399b1dd0b821e45e407a8631e77a86ff4488f8
7
- data.tar.gz: e3f0db1ad0602e6e24434d24f17b0a6eeaf0a0173264208aa0e47968d649b178227a9a0cde320a0304b22b65a5cc5637f0e7fc4f2649d79cf0d88da9ac19aee4
6
+ metadata.gz: 4e3b8f57a51c0c947e9ae175481676b4b2c9423f687533a8d549069d39a3d35cd66b4830fff0430c83f821b15ac312b61db3e99d2de9271bae5dda2f181a0132
7
+ data.tar.gz: e5d5affb24e366e957d40894bfaec32bd1cd1f59693816fd8777e2cddcf19f5981080a2d5abe0ab6a1e07398d0ad98fe7c7f3aa9ac3d82b39ef3eb9416875db4
data/CHANGELOG.adoc CHANGED
@@ -3,6 +3,11 @@
3
3
  :doc-base-url: http://www.rubydoc.info/github/jirutka/corefines/Corefines
4
4
  :issue-uri: {repo-uri}/issues
5
5
 
6
+ == 1.6.0 (2015-05-16)
7
+
8
+ * Add new refinement {doc-base-url}/String/Camelcase[String#camelcase].
9
+ * Add new refinement {doc-base-url}/String/SnakeCase[String#snake_case].
10
+
6
11
 
7
12
  == 1.5.0 (2015-05-03)
8
13
 
data/README.adoc CHANGED
@@ -41,12 +41,12 @@ TODO
41
41
  Add this line to your application’s Gemfile:
42
42
 
43
43
  [source]
44
- gem 'corefines', '~> 1.5'
44
+ gem 'corefines', '~> 1.6'
45
45
 
46
46
  or to your gemspec:
47
47
 
48
48
  [source]
49
- s.add_runtime_dependency 'corefines', '~> 1.5'
49
+ s.add_runtime_dependency 'corefines', '~> 1.6'
50
50
 
51
51
  and then execute:
52
52
 
@@ -162,6 +162,7 @@ Not ideal indeed, but probably the best of what we can achieve.
162
162
  ** {doc-base-url}/Object/Try[#try]
163
163
  ** {doc-base-url}/Object/Try[#try!]
164
164
  * {doc-base-url}/String[String]
165
+ ** {doc-base-url}/String/Camelcase[#camelcase]
165
166
  ** {doc-base-url}/String/Color[#color]
166
167
  ** {doc-base-url}/String/Concat[#concat!]
167
168
  ** {doc-base-url}/String/Decolor[#decolor]
@@ -170,6 +171,7 @@ Not ideal indeed, but probably the best of what we can achieve.
170
171
  ** {doc-base-url}/String/Indent[#indent]
171
172
  ** {doc-base-url}/String/RelativePathFrom[#relative_path_from]
172
173
  ** {doc-base-url}/String/Remove[#remove]
174
+ ** {doc-base-url}/String/SnakeCase[#snake_case]
173
175
  ** {doc-base-url}/String/ToB[#to_b]
174
176
  ** {doc-base-url}/String/ToRe[#to_re]
175
177
  ** {doc-base-url}/String/Unindent[#unindent] (alias `#strip_heredoc`)
@@ -7,6 +7,61 @@ module Corefines
7
7
  ESCAPE_SEQUENCE = /\033\[([0-9]+);([0-9]+);([0-9]+)m(.+?)\033\[0m|([^\033]+)/m
8
8
  private_constant :ESCAPE_SEQUENCE
9
9
 
10
+ ##
11
+ # @!method camelcase
12
+ # Returns a copy of the _str_ converted to camelcase. When no +:upper+ or
13
+ # +:lower+ is specified, then it leaves the first character of a word
14
+ # (after camelization) unchanged.
15
+ #
16
+ # @example
17
+ # "camel case".camelcase # => "camelCase"
18
+ # "Camel case".camelcase # => "CamelCase"
19
+ # "camel_case__4you!".camelcase # => "camelCase4you!"
20
+ # "camel_case__4_you!".camelcase # => "camelCase4You!"
21
+ #
22
+ # @overload camelcase(*separators)
23
+ # @example
24
+ # "camel-case yay!".camelcase('-') # => "camelCase yay!"
25
+ # "camel::ca-se-y".camelcase(':', '-') # => "camelCaSeY"
26
+ # "camel42case".camelcase(/[0-9]+/) # => "camelCase"
27
+ #
28
+ # @overload camelcase(first_letter, *separators)
29
+ # @example
30
+ # "camel case".camelcase(:upper) # => "CamelCase"
31
+ # "camel-case yay!".camelcase(:upper, '-') # => "CamelCase Yay!"
32
+ #
33
+ # @param first_letter [:upper, :lower] desired case of the first
34
+ # character of a word - +:upper+ to be upcased, or +:lower+ to
35
+ # be downcased.
36
+ #
37
+ # @param *separators [String, Regexp] the patterns used to determine
38
+ # where capitalization should occur. Defaults to <tt>/_+/</tt> and
39
+ # <tt>\s+</tt>.
40
+ # @return [String] a copy of the _str_ converted to camelcase.
41
+ #
42
+ module Camelcase
43
+ refine ::String do
44
+ def camelcase(*separators)
45
+ first_letter = separators.shift if ::Symbol === separators.first
46
+ separators = [/_+/, /\s+/] if separators.empty?
47
+
48
+ self.dup.tap do |str|
49
+ separators.compact.each do |s|
50
+ s = "(?:#{::Regexp.escape(s)})+" unless s.is_a? ::Regexp
51
+ str.gsub!(/#{s}([a-z\d])/i) { $1.upcase }
52
+ end
53
+
54
+ case first_letter
55
+ when :upper
56
+ str.gsub!(/(\A|\s)([a-z])/) { $1 + $2.upcase }
57
+ when :lower
58
+ str.gsub!(/(\A|\s)([A-Z])/) { $1 + $2.downcase }
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+
10
65
  ##
11
66
  # @!method color
12
67
  # @example
@@ -286,6 +341,40 @@ module Corefines
286
341
  end
287
342
  end
288
343
 
344
+ ##
345
+ # @!method snake_case
346
+ # @example
347
+ # "snakeCase".snake_case # => "snake_case"
348
+ # "SNAkeCASe".snake_case # => "sn_ake_ca_se"
349
+ # "Snake2Case".snake_case # => "snake2_case"
350
+ # "snake2case".snake_case # => "snake2case"
351
+ # "snake-Ca-se".snake_case # => "snake_ca_se"
352
+ # "snake ca se".snake_case # => "snake__ca_se"
353
+ # "__snake-case__".snake_case # => "__snake_case__"
354
+ #
355
+ # @return [String] a copy of the _str_ converted to snake_case.
356
+ #
357
+ # @!method snakecase
358
+ # Alias for {#snake_case}.
359
+ #
360
+ # @return (see #snake_case)
361
+ #
362
+ module SnakeCase
363
+ refine ::String do
364
+ def snake_case
365
+ self.dup.tap do |s|
366
+ s.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
367
+ s.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
368
+ s.tr!('-', '_')
369
+ s.gsub!(/\s/, '_')
370
+ s.downcase!
371
+ end
372
+ end
373
+
374
+ alias_method :snakecase, :snake_case
375
+ end
376
+ end
377
+
289
378
  ##
290
379
  # @!method to_b
291
380
  # Interprets common affirmative string meanings as +true+, otherwise
@@ -421,6 +510,7 @@ module Corefines
421
510
  alias_method :force_utf8!, :force_utf8
422
511
  alias_method :indent!, :indent
423
512
  alias_method :remove!, :remove
513
+ alias_method :snakecase, :snake_case
424
514
  end
425
515
  end
426
516
  end
@@ -1,3 +1,3 @@
1
1
  module Corefines
2
- VERSION = '1.5.0'
2
+ VERSION = '1.6.0'
3
3
  end
@@ -0,0 +1,69 @@
1
+ describe String do
2
+ using Corefines::String::camelcase
3
+
4
+ describe '#camelcase' do
5
+
6
+ it "doesn't modify the original string" do
7
+ str = 'camel_case'
8
+ origo = str.dup
9
+ expect( str.camelcase ).to eq "camelCase"
10
+ expect( str ).to eq origo
11
+ end
12
+
13
+ context "default" do
14
+
15
+ it "capitalizes at underscore(s)" do
16
+ expect( 'camel__case_4you!'.camelcase ).to eq 'camelCase4you!'
17
+ end
18
+
19
+ it "capitalizes at white space(s)" do
20
+ expect( 'camel case 4 you!'.camelcase ).to eq 'camelCase4You!'
21
+ expect( "camel\tcase\t\t4you!".camelcase ).to eq 'camelCase4you!'
22
+ end
23
+ end
24
+
25
+ context "with string separator" do
26
+
27
+ it "capitalizes at the separator" do
28
+ expect( 'camel/case'.camelcase('/') ).to eq 'camelCase'
29
+ end
30
+
31
+ it "capitalizes at all of the given separators" do
32
+ expect( 'camel/case;allons-y!'.camelcase('/', ';', '-') ).to eq 'camelCaseAllonsY!'
33
+ end
34
+
35
+ it "capitalizes at multiple occurrences of the separator" do
36
+ expect( 'camel--case-here'.camelcase('-') ).to eq 'camelCaseHere'
37
+ expect( '-Camel--case-here-'.camelcase('-') ).to eq 'CamelCaseHere-'
38
+ end
39
+
40
+ it "capitalizes at occurrences of the multichar separator" do
41
+ expect( 'camel->case'.camelcase('->') ).to eq 'camelCase'
42
+ end
43
+ end
44
+
45
+ context "with regexp separator" do
46
+ it "capitalizes at matches of the regexp" do
47
+ expect( 'camel22case'.camelcase(/[0-9]+/) ).to eq 'camelCase'
48
+ end
49
+ end
50
+
51
+ context "with :lower" do
52
+ it "capitalizes and converts the first letter to lower case" do
53
+ expect( 'CAmeL Case'.camelcase(:lower) ).to eq 'cAmeLCase'
54
+ end
55
+ end
56
+
57
+ context "with :upper" do
58
+ it "capitalizes at default separators and converts the first letter to upper case" do
59
+ expect( 'camel_case yay'.camelcase(:upper) ).to eq 'CamelCaseYay'
60
+ end
61
+
62
+ context "and separator" do
63
+ it "capitalizes at the separator and converts the first letter to upper case" do
64
+ expect( 'camel-case'.camelcase(:upper, '-') ).to eq 'CamelCase'
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,40 @@
1
+ describe String do
2
+ using Corefines::String[:snake_case, :snakecase]
3
+
4
+ describe '#snake_case' do
5
+
6
+ {
7
+ 'snakeCase' => 'snake_case',
8
+ 'SnakeCase' => 'snake_case',
9
+ 'SNAkeCASe' => 'sn_ake_ca_se',
10
+ 'Snake2Case' => 'snake2_case',
11
+ 'snake2case' => 'snake2case'
12
+ }
13
+ .each do |str, expected|
14
+ it "converts CamelCase to snake_case: #{str} => #{expected}" do
15
+ expect( str.snake_case ).to eq expected
16
+ end
17
+ end
18
+
19
+ it "converts white spaces to underscores" do
20
+ ["Sssnake Case Yay!", "Sssnake\tCase\t\tYay!"].each do |str|
21
+ expect( str.snake_case ).to eq 'sssnake_case__yay!'
22
+ end
23
+ end
24
+
25
+ it "converts hyphens to underscores" do
26
+ expect( 'Sss-nake--case!'.snake_case ).to eq 'sss_nake__case!'
27
+ end
28
+
29
+ it "doesn't squeeze underscores" do
30
+ expect( '__snake__case__'.snake_case ).to eq '__snake__case__'
31
+ end
32
+
33
+ it "doesn't modify the original string" do
34
+ str = 'SnakeCase'
35
+ origo = str.dup
36
+ expect( str.snake_case ).to eq 'snake_case'
37
+ expect( str ).to eq origo
38
+ end
39
+ end
40
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: corefines
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Jirutka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-03 00:00:00.000000000 Z
11
+ date: 2015-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -141,6 +141,7 @@ files:
141
141
  - spec/object/then_spec.rb
142
142
  - spec/object/try_spec.rb
143
143
  - spec/spec_helper.rb
144
+ - spec/string/camelcase_spec.rb
144
145
  - spec/string/color_spec.rb
145
146
  - spec/string/concat_spec.rb
146
147
  - spec/string/decolor_spec.rb
@@ -148,6 +149,7 @@ files:
148
149
  - spec/string/indent_spec.rb
149
150
  - spec/string/relative_path_from_spec.rb
150
151
  - spec/string/remove_spec.rb
152
+ - spec/string/snake_case_spec.rb
151
153
  - spec/string/to_b_spec.rb
152
154
  - spec/string/to_re_spec.rb
153
155
  - spec/string/unindent_spec.rb