ruby_extended 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bigdecimal'
4
+ require_relative '../lib/ruby_extended.rb'
5
+
6
+ RSpec.describe Hash do
7
+
8
+
9
+ describe 'Hash.deep_find' do
10
+ input_hash = {
11
+ a: 1,
12
+ b: {c: {d: 2}},
13
+ e: [{f: 3, g: {h: 4}}]
14
+ }
15
+
16
+ it 'Should handle nil value' do
17
+ expect({a: nil}.deep_find(:b)).to eql([])
18
+ end
19
+
20
+ it 'Should find in all levels' do
21
+ expect(input_hash.deep_find(:a)).to eql([{:path => [:a], :value => 1}])
22
+ expect(input_hash.deep_find(:b)).to eql([{:path => [:b], :value => {c: {d: 2}}}])
23
+ expect(input_hash.deep_find(:c)).to eql([{:path => [:b, :c], :value => {d: 2}}])
24
+ expect(input_hash.deep_find(:d)).to eql([{:path => [:b, :c, :d], :value => 2}])
25
+ expect(input_hash.deep_find(:e)).to eql([{:path => [:e], :value => [{f: 3, g: {h: 4}}]}])
26
+ end
27
+
28
+ it 'Should find in arrays' do
29
+ expect(input_hash.deep_find(:f)).to eql([{:path => [:e, 0, :f], :value => 3}])
30
+ expect(input_hash.deep_find(:g)).to eql([{:path => [:e, 0, :g], :value => {h: 4}}])
31
+ expect(input_hash.deep_find(:h)).to eql([{:path => [:e, 0, :g, :h], :value => 4}])
32
+ end
33
+
34
+ end
35
+
36
+
37
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bigdecimal'
4
+ require_relative '../lib/ruby_extended.rb'
5
+
6
+ RSpec.describe Numeric do
7
+
8
+
9
+ describe 'Numeric.no_zeros' do
10
+
11
+ it 'Should convert BigDecimal to no-zero string' do
12
+ expect(BigDecimal('1.0').no_zeros).to eql('1')
13
+ expect(BigDecimal('1.10').no_zeros).to eql('1.1')
14
+ end
15
+
16
+ it 'Should convert Float return to no-zero string' do
17
+ expect(Float('1.0').no_zeros).to eql('1')
18
+ expect(Float('1.10').no_zeros).to eql('1.1')
19
+ end
20
+
21
+ it 'Should convert Integer return to no-zero string' do
22
+ expect(Integer('1').no_zeros).to eql('1')
23
+ expect(Integer('1').no_zeros).to eql('1')
24
+ end
25
+
26
+ it 'Should convert Complex return to no-zero string' do
27
+ expect(Complex('1.0').no_zeros).to eql('1')
28
+ expect(Complex('1.10').no_zeros).to eql('1.1')
29
+ end
30
+
31
+ it 'Should convert Rational return to no-zero string' do
32
+ expect(Rational('1.0').no_zeros).to eql('1')
33
+ expect(Rational('1.10').no_zeros).to eql('1.1')
34
+ end
35
+
36
+ end
37
+
38
+
39
+ describe 'Numeric.to_money' do
40
+
41
+ it 'Should convert BigDecimal to string formatted like money' do
42
+ expect(BigDecimal('1.0').to_money).to eql('1.00')
43
+ end
44
+
45
+ it 'Should convert Float return to string formatted like money' do
46
+ expect(Float('1.0').to_money).to eql('1.00')
47
+ end
48
+
49
+ it 'Should convert Integer return to string formatted like money' do
50
+ expect(Integer('1').to_money).to eql('1.00')
51
+ end
52
+
53
+ it 'Should convert Complex return to string formatted like money' do
54
+ expect(Complex('1.0').to_money).to eql('1.00')
55
+ end
56
+
57
+ it 'Should convert Rational return to string formatted like money' do
58
+ expect(Rational('1.0').to_money).to eql('1.00')
59
+ end
60
+
61
+ end
62
+
63
+
64
+ describe 'Numeric.percent_of' do
65
+
66
+ it 'Should return percentage from BigDecimal value' do
67
+ expect(BigDecimal('1.0').percent_of(BigDecimal('100'))).to eql(1)
68
+ end
69
+
70
+ it 'Should return percentage from Float value' do
71
+ expect(Float('1.0').percent_of(Float('100'))).to eql(1)
72
+ end
73
+
74
+ it 'Should return percentage from Integer value' do
75
+ expect(Integer('1').percent_of(Integer('100'))).to eql(1)
76
+ end
77
+
78
+ it 'Should return percentage from Complex value' do
79
+ expect(Complex('1.0').percent_of(Complex('100'))).to eql(1)
80
+ end
81
+
82
+ it 'Should return percentage from Rational value' do
83
+ expect(Rational('1.0').percent_of(Rational('100'))).to eql(1)
84
+ end
85
+
86
+ end
87
+
88
+
89
+ describe 'Numeric.get_percent_from' do
90
+
91
+ it 'Should return percent from two numbers, using BigDecimal class' do
92
+ expect(BigDecimal('1.0').get_percent_from(BigDecimal('2'))).to eql(50)
93
+ end
94
+
95
+ it 'Should return percent from two numbers, using Float class' do
96
+ expect(Float('1.0').get_percent_from(Float('2'))).to eql(50)
97
+ end
98
+
99
+ it 'Should return percent from two numbers, using Integer class' do
100
+ expect(Integer('1').get_percent_from(Integer('2'))).to eql(50)
101
+ end
102
+
103
+ it 'Should return percent from two numbers, using Complex class' do
104
+ expect(Complex('1.0').get_percent_from(Complex('2'))).to eql(50)
105
+ end
106
+
107
+ it 'Should return percent from two numbers, using Rational class' do
108
+ expect(Rational('1.0').get_percent_from(Rational('2'))).to eql(50)
109
+ end
110
+
111
+ end
112
+
113
+
114
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bigdecimal'
4
+ require_relative '../lib/ruby_extended.rb'
5
+
6
+ RSpec.describe Object do
7
+
8
+
9
+ describe 'Object.dig' do
10
+ input_hash = {
11
+ a: 1,
12
+ b: {c: {d: 2}},
13
+ e: [{f: 3, g: {h: 4}}],
14
+ i: '123abc',
15
+ j: 123
16
+ }
17
+
18
+ input_array = [
19
+ {a: [{b: [1]}], b: [2], c: '123abc', d: 123 }
20
+ ]
21
+
22
+ it 'Should find in Hash' do
23
+ expect(input_hash.dig(:a)).to eql(1)
24
+ expect(input_hash.dig(:b, :c)).to eql({d: 2})
25
+ expect(input_hash.dig(:b, :c, :d)).to eql(2)
26
+ expect(input_hash.dig(:i)).to eql('123abc')
27
+ expect(input_hash.dig(:j)).to eql(123)
28
+ end
29
+
30
+ it 'Should find in Hash by passing array' do
31
+ expect(input_hash.dig([:a])).to eql(1)
32
+ expect(input_hash.dig([:b, :c])).to eql({d: 2})
33
+ expect(input_hash.dig([:b, :c, :d])).to eql(2)
34
+ end
35
+
36
+ it 'Should find in Array' do
37
+ expect(input_array.dig(0, :a, 0, :b, 0)).to eql(1)
38
+ expect(input_array.dig(0, :c)).to eql('123abc')
39
+ expect(input_array.dig(0, :d)).to eql(123)
40
+ end
41
+
42
+ it 'Should find in Array by passing array' do
43
+ expect(input_array.dig([0, :a, 0, :b, 0])).to eql(1)
44
+ end
45
+
46
+ end
47
+
48
+
49
+ end
@@ -0,0 +1,147 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bigdecimal'
4
+ require_relative '../lib/ruby_extended.rb'
5
+
6
+ RSpec.describe Array do
7
+
8
+
9
+ describe 'String.strip_whitespace' do
10
+
11
+ it "Expect to remove character SPACE (U+0020)" do
12
+ expect("\u0020".strip_whitespace).to eql('')
13
+ end
14
+
15
+ it "Expect to remove character NO-BREAK SPACE (U+00A0)" do
16
+ expect("\u00A0".strip_whitespace).to eql('')
17
+ end
18
+
19
+ it "Expect to remove character OGHAM SPACE MARK (U+1680)" do
20
+ expect("\u1680".strip_whitespace).to eql('')
21
+ end
22
+
23
+ it "Expect to remove character MONGOLIAN VOWEL SEPARATOR (U+180E)" do
24
+ expect("\u180E".strip_whitespace).to eql('')
25
+ end
26
+
27
+ it "Expect to remove character EN QUAD (U+2000)" do
28
+ expect("\u2000".strip_whitespace).to eql('')
29
+ end
30
+
31
+ it "Expect to remove character EN SPACE (nut) (U+2002)" do
32
+ expect("\u2002".strip_whitespace).to eql('')
33
+ end
34
+
35
+ it "Expect to remove character EM SPACE (mutton) (U+2003)" do
36
+ expect("\u2003".strip_whitespace).to eql('')
37
+ end
38
+
39
+ it "Expect to remove character THREE-PER-EM SPACE (thick space) (U+2004)" do
40
+ expect("\u2004".strip_whitespace).to eql('')
41
+ end
42
+
43
+ it "Expect to remove character FOUR-PER-EM SPACE (mid space) (U+2005)" do
44
+ expect("\u2005".strip_whitespace).to eql('')
45
+ end
46
+
47
+ it "Expect to remove character SIX-PER-EM SPACE (U+2006)" do
48
+ expect("\u2006".strip_whitespace).to eql('')
49
+ end
50
+
51
+ it "Expect to remove character FIGURE SPACE (U+2007)" do
52
+ expect("\u2007".strip_whitespace).to eql('')
53
+ end
54
+
55
+ it "Expect to remove character PUNCTUATION SPACE (U+2008)" do
56
+ expect("\u2008".strip_whitespace).to eql('')
57
+ end
58
+
59
+ it "Expect to remove character THIN SPACE (U+2009)" do
60
+ expect("\u2009".strip_whitespace).to eql('')
61
+ end
62
+
63
+ it "Expect to remove character HAIR SPACE (U+200A)" do
64
+ expect("\u200A".strip_whitespace).to eql('')
65
+ end
66
+
67
+ it "Expect to remove character ZERO WIDTH SPACE (U+200B)" do
68
+ expect("\u200B".strip_whitespace).to eql('')
69
+ end
70
+
71
+ it "Expect to remove character NARROW NO-BREAK SPACE (U+200F)" do
72
+ expect("\u200F".strip_whitespace).to eql('')
73
+ end
74
+
75
+ it "Expect to remove character MEDIUM MATHEMATICAL SPACE (U+205F)" do
76
+ expect("\u205F".strip_whitespace).to eql('')
77
+ end
78
+
79
+ it "Expect to remove character IDEOGRAPHIC SPACE (U+3000)" do
80
+ expect("\u3000".strip_whitespace).to eql('')
81
+ end
82
+
83
+ it "Expect to remove character ZERO WIDTH NO-BREAK SPACE (U+FEFF)" do
84
+ expect("\uFEFF".strip_whitespace).to eql('')
85
+ end
86
+
87
+ it "Expect to remove character OPEN BOX (U+2423)" do
88
+ expect("\u2423".strip_whitespace(visible: true)).to eql('')
89
+ end
90
+
91
+ it "Expect to remove character BLANK SYMBOL (U+2422)" do
92
+ expect("\u2422".strip_whitespace(visible: true)).to eql('')
93
+ end
94
+
95
+ it "Expect to remove character SYMBOL FOR SPACE (U+2420)" do
96
+ expect("\u2420".strip_whitespace(visible: true)).to eql('')
97
+ end
98
+
99
+ it 'Should not break string if no whitespace characters are found' do
100
+ expect('').to eql('')
101
+ expect('abc').to eql('abc')
102
+ end
103
+
104
+ end
105
+
106
+
107
+ describe 'String.strip_newline' do
108
+
109
+ it 'Should remove new line characters' do
110
+ expect(''.strip_newline).to eql('')
111
+ expect("\n".strip_newline).to eql('')
112
+ expect("\r".strip_newline).to eql('')
113
+ end
114
+
115
+ it 'Should not break string if no new line characters are found' do
116
+ expect('').to eql('')
117
+ expect('abc').to eql('abc')
118
+ end
119
+
120
+ end
121
+
122
+
123
+ describe 'String.index_of' do
124
+
125
+ it 'Should return array of indexes from string' do
126
+ expect('abc123abc'.index_of('abc')).to eql([0, 6])
127
+ end
128
+
129
+ it 'Should return empty array of no indexes found in string' do
130
+ expect('abc123abc'.index_of('x')).to eql([])
131
+ end
132
+
133
+ end
134
+
135
+
136
+ describe 'String.similarity_percentage' do
137
+
138
+ it 'Should return percent on how similar are two strings' do
139
+ expect('ab'.similarity_percent('a')).to eql(50.0)
140
+ expect('ab'.similarity_percent('ax')).to eql(50.0)
141
+ expect('abdefghijk'.similarity_percent('abdefghij0')).to eql(90.0)
142
+ end
143
+
144
+ end
145
+
146
+
147
+ end
@@ -0,0 +1,8 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'codecov'
5
+ SimpleCov.formatters = [
6
+ SimpleCov::Formatter::Codecov,
7
+ SimpleCov::Formatter::HTMLFormatter
8
+ ]
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_extended
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - EdCordata
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-11-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: unicode_utils
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ description: Extend Ruby classes with helpful methods
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".gitignore"
34
+ - ".rspec"
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - badges/coverage.svg
38
+ - badges/license.svg
39
+ - badges/ruby.svg
40
+ - documentation/array.md
41
+ - documentation/hash.md
42
+ - documentation/number.md
43
+ - documentation/object.md
44
+ - documentation/readme.md
45
+ - documentation/string.md
46
+ - lib/ruby_extended.rb
47
+ - lib/ruby_extended/array.rb
48
+ - lib/ruby_extended/hash.rb
49
+ - lib/ruby_extended/number.rb
50
+ - lib/ruby_extended/object.rb
51
+ - lib/ruby_extended/string.rb
52
+ - lib/ruby_extended/version.rb
53
+ - readme.md
54
+ - ruby_extended.gemspec
55
+ - spec/ruby_extend_array_spec.rb
56
+ - spec/ruby_extend_hash_spec.rb
57
+ - spec/ruby_extend_number_spec.rb
58
+ - spec/ruby_extend_object_spec.rb
59
+ - spec/ruby_extend_string_spec.rb
60
+ - spec/spec_helper.rb
61
+ homepage: https://github.com/EdCordata-Ruby-Gems/ruby_extended
62
+ licenses:
63
+ - CC BY 4.0
64
+ metadata:
65
+ documentation_uri: https://github.com/EdCordata-Ruby-Gems/ruby_extended/blob/master/documentation/readme.md
66
+ source_code_uri: https://github.com/EdCordata-Ruby-Gems/ruby_extended
67
+ bug_tracker_uri: https://github.com/EdCordata-Ruby-Gems/ruby_extended/issues
68
+ wiki_uri: https://github.com/EdCordata-Ruby-Gems/ruby_extended/blob/master/documentation/readme.md
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 1.9.3
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubygems_version: 3.1.2
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Extend Ruby classes with helpful methods
88
+ test_files: []