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,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6713d9529df421372812b0a1d21c647eb6e4cbc2928d9d544ccfa1b4bef5ad0b
4
+ data.tar.gz: 4400debd57858bbbbf006c2004ad6812c91baec26c1180a202f56bc5f5c9a8a5
5
+ SHA512:
6
+ metadata.gz: 4766701ba03173a2ae2e07e88f0ff7ebe7e14bedf14ec78a5d622258f4c2b2521b8cdc790380e6b376050696ad60d1a2a27c700f1ee5a76c0fe4cc6396d669b5
7
+ data.tar.gz: c8c9a8193876f82888c3163a4cc24469b396fc98e5c0002a09e2767d8f23b4ef036ec7cad87a284c68859a0d757bac5a85f31c63af3d11adfc4beea26c3433cc
@@ -0,0 +1,10 @@
1
+ # IDE
2
+ .idea/
3
+ .atom/
4
+ .vscode/
5
+
6
+ # Rspec
7
+ coverage/
8
+
9
+ # Gem builds
10
+ ruby_extended-*.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'unicode_utils'
6
+
7
+ group :test do
8
+ gem 'pry', require: false
9
+ gem 'rspec', require: false
10
+ gem 'codecov', require: false
11
+ gem 'simplecov-html', require: false
12
+ end
@@ -0,0 +1,45 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ codecov (0.2.12)
5
+ json
6
+ simplecov
7
+ coderay (1.1.3)
8
+ diff-lcs (1.4.4)
9
+ docile (1.3.2)
10
+ json (2.3.1)
11
+ method_source (1.0.0)
12
+ pry (0.13.1)
13
+ coderay (~> 1.1)
14
+ method_source (~> 1.0)
15
+ rspec (3.10.0)
16
+ rspec-core (~> 3.10.0)
17
+ rspec-expectations (~> 3.10.0)
18
+ rspec-mocks (~> 3.10.0)
19
+ rspec-core (3.10.0)
20
+ rspec-support (~> 3.10.0)
21
+ rspec-expectations (3.10.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.10.0)
24
+ rspec-mocks (3.10.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.10.0)
27
+ rspec-support (3.10.0)
28
+ simplecov (0.19.1)
29
+ docile (~> 1.1)
30
+ simplecov-html (~> 0.11)
31
+ simplecov-html (0.12.3)
32
+ unicode_utils (1.4.0)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ codecov
39
+ pry
40
+ rspec
41
+ simplecov-html
42
+ unicode_utils
43
+
44
+ BUNDLED WITH
45
+ 2.1.2
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="104" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="a"><rect width="104" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#a)"><path fill="#555" d="M0 0h61v20H0z"/><path fill="#4c1" d="M61 0h43v20H61z"/><path fill="url(#b)" d="M0 0h104v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110"> <text x="315" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">coverage</text><text x="315" y="140" transform="scale(.1)" textLength="510">coverage</text><text x="815" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="330">100%</text><text x="815" y="140" transform="scale(.1)" textLength="330">100%</text></g> </svg>
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="112" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="a"><rect width="112" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#a)"><path fill="#555" d="M0 0h47v20H0z"/><path fill="#4c1" d="M47 0h65v20H47z"/><path fill="url(#b)" d="M0 0h112v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110"> <text x="245" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="370">license</text><text x="245" y="140" transform="scale(.1)" textLength="370">license</text><text x="785" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="550">CC BY 4.0</text><text x="785" y="140" transform="scale(.1)" textLength="550">CC BY 4.0</text></g> </svg>
@@ -0,0 +1 @@
1
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="84" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="a"><rect width="84" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#a)"><path fill="#555" d="M0 0h37v20H0z"/><path fill="#007ec6" d="M37 0h47v20H37z"/><path fill="url(#b)" d="M0 0h84v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110"> <text x="195" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="270">Ruby</text><text x="195" y="140" transform="scale(.1)" textLength="270">Ruby</text><text x="595" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="370">1.9.3+</text><text x="595" y="140" transform="scale(.1)" textLength="370">1.9.3+</text></g> </svg>
@@ -0,0 +1,226 @@
1
+ # Ruby Extend - Array
2
+
3
+
4
+
5
+ ### `.sort_lv`
6
+ Sorts Array respecting latvian characters, like `Ā`, `Č`, etc..
7
+ ```ruby
8
+ %w[a b c ā z č].sort_lv # => ['a', 'ā', 'b', 'c', 'č', 'z']
9
+ ```
10
+ Method accepts block, so it can be used on hash arrays.
11
+ ```ruby
12
+ [
13
+ { n: 'a' },
14
+ { n: 'c' },
15
+ { n: 'ā' },
16
+ { n: 'č' },
17
+ { n: 'z' }
18
+ ].sort_lv { |x| x[:n] } # => [ { n: 'a' }, { n: 'ā' }, { n: 'c' }, { n: 'č' }, { n: 'z' } ]
19
+ ```
20
+
21
+
22
+ ### `.tabulate`
23
+ Split and transpose array horizontally or vertically.
24
+ ```ruby
25
+ %w[a b c d e f g].tabulate(2, split_by: :columns, direction: :horizontal)
26
+ # => [ [a,b], [c,d], [e,f], [g,nil] ]
27
+ #
28
+ # visualized example:
29
+ # [ a ] [ b ]
30
+ # [ c ] [ d ]
31
+ # [ e ] [ f ]
32
+ # [ g ] [nil]
33
+ ```
34
+ ```ruby
35
+ %w[a b c d e f g].tabulate(2, split_by: :rows, direction: :horizontal)
36
+ # => [ [a,b,c,d], [e,f,g,nil] ]
37
+ #
38
+ # visualized example:
39
+ # [ a ] [ b ] [ c ] [ d ]
40
+ # [ e ] [ f ] [ g ] [nil]
41
+ ```
42
+ ```ruby
43
+ %w[a b c d e f g].tabulate(2, split_by: :columns, direction: :vertical)
44
+ # => [ [a,b,c,d], [e,f,g,nil] ]
45
+ #
46
+ # visualized example:
47
+ # [ a ] [ e ]
48
+ # [ b ] [ f ]
49
+ # [ c ] [ g ]
50
+ # [ d ] [nil]
51
+ ```
52
+ ```ruby
53
+ %w[a b c d e f g].tabulate(2, split_by: :rows, direction: :vertical)
54
+ # => [ [a,c,e,g], [b,d,f,nil] ]
55
+ #
56
+ # visualized example:
57
+ # [ a ] [ c ] [ e ] [ g ]
58
+ # [ b ] [ d ] [ f ] [nil]
59
+ ```
60
+
61
+
62
+ ### `.sample_index`
63
+ Returns uniq array of hashes, based on one key.
64
+ ```ruby
65
+ [
66
+ {a: 1, b: '123'},
67
+ {a: 1, b: 'abc'},
68
+ {a: 2, b: 'cde'}
69
+ ].uniq_by_key(:a) # => [ {a: 1, b: '123'}, {a: 2, b: 'cde'} ]
70
+ ```
71
+ Returns original array if key not found
72
+ ```ruby
73
+ [
74
+ {a: 1, b: '123'},
75
+ {a: 1, b: 'abc'},
76
+ {a: 2, b: 'cde'}
77
+ ].uniq_by_key(:x) # => [ {a: 1, b: '123'}, {a: 1, b: 'abc'}, {a: 2, b: 'cde'} ]
78
+ ```
79
+
80
+
81
+ ### `.duplicates`
82
+ Returns duplicates from array
83
+ ```ruby
84
+ ['a', 'a', 2, 2, 2, 2, 3, 'b', nil, nil].duplicates # => ['a', 2, nil]
85
+ ```
86
+ Returns duplicates from array with count
87
+ ```ruby
88
+ ['a', 'a', 2, 2, 2, 2, 3, 'b', nil, nil].duplicates(full: true)
89
+ # => [
90
+ # {item: 'a', count: 2},
91
+ # {item: 2, count: 4},
92
+ # {item: nil, count: 2}
93
+ # ]
94
+ ```
95
+
96
+
97
+ ### `.sum` (overwrites & extends existing method)
98
+ ```ruby
99
+ [1, 2, 3].sum # => 6
100
+ [0.1, 0.2, 1].sum # => 1.3
101
+ %w[a b].sum # => 'ab'
102
+ ['a', 'b', 2, 1.5, nil].sum # => ab21.5
103
+ ```
104
+ If only numbers in array need to be summed:
105
+ ```ruby
106
+ ['a', 1, 1.5, nil].sum(only_numbers: true) # => 2.5
107
+ ```
108
+ Possible to sum by hash key:
109
+ ```ruby
110
+ [ {a: 1}, {a: 'a'}, {a: 3} ].sum(key: :a, only_numbers: true) # => 4
111
+ [ {a: 1}, {a: 'a'}, {b: 3} ].sum(key: :a, only_numbers: true) # => 1
112
+ ```
113
+ If the key is not found in any of the array hash items, will return `nil`
114
+ ```ruby
115
+ [ {a: 1}, {a: 'a'}, {a: 3} ].sum(key: :b) # => nil
116
+ ```
117
+
118
+
119
+ ### `.to_tree`
120
+ Converts hash array with parent_id to embed array with children.
121
+ ```ruby
122
+ [
123
+ {id: 1, parent_id: nil, any_other_key: 'a'},
124
+ {id: 2, parent_id: 1, any_other_key: 'b'},
125
+ {id: 3, parent_id: 2, any_other_key: 'c'},
126
+ ].to_tree
127
+ ```
128
+ to
129
+ ```json
130
+ [
131
+ {
132
+ "id": 1,
133
+ "parent_id": null,
134
+ "any_other_key": "a",
135
+ "children": [
136
+ {
137
+ "id": 2,
138
+ "parent_id": 1,
139
+ "any_other_key": "b",
140
+ "children": [
141
+ {
142
+ "id": 3,
143
+ "parent_id": 2,
144
+ "any_other_key": "c"
145
+ }
146
+ ]
147
+ }
148
+ ]
149
+ }
150
+ ]
151
+ ```
152
+ It's also possible to convert it to hash
153
+ ```ruby
154
+ [
155
+ {id: 1, parent_id: nil, any_other_key: 'a'},
156
+ {id: 2, parent_id: 1, any_other_key: 'b'},
157
+ {id: 3, parent_id: 2, any_other_key: 'c'},
158
+ ].to_tree(to_hash: true)
159
+ ```
160
+ ```json
161
+ {
162
+ "1": {
163
+ "id": 1,
164
+ "any_other_key": "a",
165
+ "children": {
166
+ "2": {
167
+ "id": 2,
168
+ "any_other_key": "b",
169
+ "children": {
170
+ "3": {
171
+ "id": 3,
172
+ "any_other_key": "c"
173
+ }
174
+ }
175
+ }
176
+ }
177
+ }
178
+ }
179
+ ```
180
+ All available options
181
+ <table border="1">
182
+ <thead>
183
+ <tr>
184
+ <td>Option</td>
185
+ <td>Description</td>
186
+ <td>Default</td>
187
+ </tr>
188
+ </thead>
189
+ <tbody>
190
+ <tr>
191
+ <td>id_key</td>
192
+ <td>Specify the id key</td>
193
+ <td>id</td>
194
+ </tr>
195
+ <tr>
196
+ <td>parent_id_key</td>
197
+ <td>Specify parent_id key</td>
198
+ <td>parent_id</td>
199
+ </tr>
200
+ <tr>
201
+ <td>children_key</td>
202
+ <td>Specify children key</td>
203
+ <td>children</td>
204
+ </tr>
205
+ <tr>
206
+ <td>include_parent_id</td>
207
+ <td>Include parent_id</td>
208
+ <td>false</td>
209
+ </tr>
210
+ <tr>
211
+ <td>include_empty_children</td>
212
+ <td>includes empty children key if no children present</td>
213
+ <td>false</td>
214
+ </tr>
215
+ <tr>
216
+ <td>to_hash</td>
217
+ <td>Convert to hash instead of array</td>
218
+ <td>false</td>
219
+ </tr>
220
+ <tr>
221
+ <td>include_id</td>
222
+ <td>Include id key when converting to hash</td>
223
+ <td>true</td>
224
+ </tr>
225
+ </tbody>
226
+ </table>
@@ -0,0 +1,22 @@
1
+ # Ruby Extend - Hash
2
+
3
+
4
+
5
+ ### `.deep_find`
6
+ Returns key value in any hash level and return full path to it
7
+ ```ruby
8
+ {
9
+ a: {
10
+ d: 'foo value 1',
11
+ b: [
12
+ {c: 1},
13
+ {d: 'foo value 2'}
14
+ ]
15
+ }
16
+ }.deep_find(:d)
17
+ # =>
18
+ # [
19
+ # {value: 'foo value 1', path: [:a, :d]},
20
+ # {value: 'foo value 2', path: [:a, b, 1, :d]}
21
+ # ]
22
+ ```
@@ -0,0 +1,39 @@
1
+ # Ruby Extend - Number (Integer, Float, BigDecimal, Complex, Rational)
2
+
3
+
4
+
5
+ ### `.no_zeros`
6
+ Converts any number to string, that has no zeroes
7
+ ```ruby
8
+ (1.0).no_zero # => '1'
9
+ (1.10).no_zero # => '1.1'
10
+ ```
11
+
12
+
13
+ ### `.to_money`
14
+ Converts any number to string formatted in money format
15
+ ```ruby
16
+ (1.0).to_money # => '1.00'
17
+ ```
18
+
19
+
20
+ ### `.percent_of`
21
+ Get percent value from number
22
+ ```ruby
23
+ (10).percent_of(100) # => 10
24
+ ```
25
+ Possible to get decimal value as well
26
+ ```ruby
27
+ (10).percent_of(111, decimal: true) # => 11.100000000000001
28
+ ```
29
+
30
+
31
+ ### `.get_percent_from`
32
+ Get percent from two numbers
33
+ ```ruby
34
+ (2).get_percent_from(6) # => 33
35
+ ```
36
+ Possible to get decimal value as well
37
+ ```ruby
38
+ (2).get_percent_from(6, decimal: true) # => 33.33333333333333
39
+ ```
@@ -0,0 +1,17 @@
1
+ # Ruby Extend - Object (Array & Hash)
2
+
3
+
4
+
5
+ ### `.dig`
6
+ Navigate hash or array without getting error, in case item is not found.
7
+ ```ruby
8
+ {a: {b: 'value'}}.dig(:a, :b) # => 'value'
9
+ [{a: [{b: 'value'}]}].dig(0, :a, 0, :b) # => 'value'
10
+ ```
11
+ Note: the difference from the dig method, included from Ruby 2.3, is that
12
+ in this version it's possible to use array as value, which is not possible
13
+ to do in original dig method.
14
+ ```ruby
15
+ {a: {b: 'value'}}.dig([:a, :b]) # => 'value'
16
+ [{a: [{b: 'value'}]}].dig([0, :a, 0, :b]) # => 'value'
17
+ ```
@@ -0,0 +1,7 @@
1
+ # Ruby Extend Documentation
2
+
3
+ * [Array](/documentation/array.md)
4
+ * [Hash](/documentation/hash.md)
5
+ * [Object](/documentation/object.md) (Array & Hash)
6
+ * [Number](/documentation/object.md) (Integer, Float, BigDecimal, Complex, Rational)
7
+ * [String](/documentation/string.md)
@@ -0,0 +1,156 @@
1
+ # Ruby Extend - String
2
+
3
+
4
+
5
+ ### `.similarity_percent`
6
+ Returns percentage on how similar are two strings
7
+ ```ruby
8
+ 'ab'.similarity_percent('a') # => 50.0
9
+ 'ab'.similarity_percent('ax') # => 50.0
10
+ 'abdefghijk'.similarity_percent('abdefghij0') # => 90.0
11
+
12
+ a = 'The quick brown fox jumps over the lazy dog'
13
+ b = 'The quick brown fox jumps over the lazy cat'
14
+ a.similarity_percent(b) # => 93.02325581395348
15
+ ```
16
+
17
+
18
+ ### `.index_of`
19
+ Return array of indexes found in string
20
+ ```ruby
21
+ 'abc123abc'.index_of('abc') # => [0, 6]
22
+ ```
23
+
24
+
25
+ ### `.strip_newline`
26
+ Remove new line characters
27
+ ```ruby
28
+ "\n\r".strip_newline # => ''
29
+ ```
30
+
31
+
32
+ ### `.strip_whitespace`
33
+ Remove multiple different whitespace characters
34
+ ```ruby
35
+ ' '.strip_whitespace # => ''
36
+ ```
37
+ Whitespace characters that are removed:
38
+ <table border="1">
39
+ <thead>
40
+ <tr>
41
+ <td>Name</td>
42
+ <td>Code</td>
43
+ </td>
44
+ </thead>
45
+ <tbody>
46
+ <tr>
47
+ <td>SPACE</td>
48
+ <td>(U+0020)</td>
49
+ <tr>
50
+ <tr>
51
+ <td>NO-BREAK SPACE</td>
52
+ <td>(U+00A0)</td>
53
+ <tr>
54
+ <tr>
55
+ <td>OGHAM SPACE MARK</td>
56
+ <td>(U+1680)</td>
57
+ <tr>
58
+ <tr>
59
+ <td>MONGOLIAN VOWEL SEPARATOR</td>
60
+ <td>(U+180E)</td>
61
+ <tr>
62
+ <tr>
63
+ <td>EN QUAD</td>
64
+ <td>(U+2000)</td>
65
+ <tr>
66
+ <tr>
67
+ <td>EN SPACE (nut)</td>
68
+ <td>(U+2002)</td>
69
+ <tr>
70
+ <tr>
71
+ <td>EM SPACE (mutton)</td>
72
+ <td>(U+2003)</td>
73
+ <tr>
74
+ <tr>
75
+ <td>THREE-PER-EM SPACE (thick space)</td>
76
+ <td>(U+2004)</td>
77
+ <tr>
78
+ <tr>
79
+ <td>FOUR-PER-EM SPACE (mid space)</td>
80
+ <td>(U+2005)</td>
81
+ <tr>
82
+ <tr>
83
+ <td>SIX-PER-EM SPACE</td>
84
+ <td>(U+2006)</td>
85
+ <tr>
86
+ <tr>
87
+ <td>FIGURE SPACE</td>
88
+ <td>(U+2007)</td>
89
+ <tr>
90
+ <tr>
91
+ <td>PUNCTUATION SPACE</td>
92
+ <td>(U+2008)</td>
93
+ <tr>
94
+ <tr>
95
+ <td>THIN SPACE</td>
96
+ <td>(U+2009)</td>
97
+ <tr>
98
+ <tr>
99
+ <td>HAIR SPACE</td>
100
+ <td>(U+200A)</td>
101
+ <tr>
102
+ <tr>
103
+ <td>ZERO WIDTH SPACE</td>
104
+ <td>(U+200B)</td>
105
+ <tr>
106
+ <tr>
107
+ <td>NARROW NO-BREAK SPACE</td>
108
+ <td>(U+200F)</td>
109
+ <tr>
110
+ <tr>
111
+ <td>MEDIUM MATHEMATICAL SPACE</td>
112
+ <td>(U+205F)</td>
113
+ <tr>
114
+ <tr>
115
+ <td>IDEOGRAPHIC SPACE</td>
116
+ <td>(U+3000)</td>
117
+ <tr>
118
+ <tr>
119
+ <td>ZERO WIDTH NO-BREAK SPACE</td>
120
+ <td>(U+FEFF)</td>
121
+ <tr>
122
+ </tbody>
123
+ </table>
124
+
125
+ Possible to also remove visual space characters, by
126
+ passing `visible` option.
127
+ ```ruby
128
+ '␣'.strip_whitespace(visible: true) # => ''
129
+ ```
130
+ Visual characters removed
131
+ <table>
132
+ <thead>
133
+ <tr>
134
+ <td>Name</td>
135
+ <td>Code</td>
136
+ <td>Character</td>
137
+ </tr>
138
+ </thead>
139
+ <tbody>
140
+ <tr>
141
+ <td>OPEN BOX</td>
142
+ <td>U+2423</td>
143
+ <td>␣</td>
144
+ </tr>
145
+ <tr>
146
+ <td>BLANK SYMBOL</td>
147
+ <td>U+2422</td>
148
+ <td>␢</td>
149
+ </tr>
150
+ <tr>
151
+ <td>SYMBOL FOR SPACE</td>
152
+ <td>U+2420</td>
153
+ <td>␠</td>
154
+ </tr>
155
+ </tbody>
156
+ </table>