lite-ruby 1.0.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.
@@ -0,0 +1,282 @@
1
+ # Hash
2
+
3
+ `assert_valid_keys!`
4
+ ------
5
+ Raises an error if key is not included in a list of keys.
6
+
7
+ ```ruby
8
+ {}.assert_valid_keys(:foo) #=> {}
9
+ { foo: 'bar' }.assert_valid_keys(:foo) #=> { foo: 'bar' }
10
+ { baz: 'boz' }.assert_valid_keys(:foo, :boo) #=> raises ArgumentError: 'Invalid key: ":baz". Allowed keys are: ":foo", ":boo"'
11
+ ```
12
+
13
+ `assert_all_valid_keys!`
14
+ ------
15
+ Raises like an error like `assert_valid_values!` but also on empty.
16
+
17
+ ```ruby
18
+ {}.assert_all_valid_keys!(:foo) #=> raises ArgumentError: 'An empty hash is not allowed'
19
+ ```
20
+
21
+ `assert_valid_values!`
22
+ ------
23
+ Raises an error if value is not included in a list of values.
24
+
25
+ ```ruby
26
+ {}.assert_valid_values(:foo) #=> {}
27
+ { foo: 'bar' }.assert_valid_values('bar') #=> { foo: 'bar' }
28
+ { baz: 'boz' }.assert_valid_values(:foo, :boo) #=> raises ArgumentError: 'Invalid value: "boz". Allowed values are: ":foo", ":boo"'
29
+ ```
30
+
31
+ `assert_all_valid_values!`
32
+ ------
33
+ Raises like an error like `assert_valid_values!` but also on empty.
34
+
35
+ ```ruby
36
+ {}.assert_all_valid_values!(:foo) #=> raises ArgumentError: 'An empty hash is not allowed'
37
+ ```
38
+
39
+ `bury`
40
+ ------
41
+ Updates a deeply nested value.
42
+
43
+ ```ruby
44
+ { foo: { baz: 'boo' } }.bury(:foo, :moo) #=> { foo: :moo }
45
+ { foo: { baz: 'boo' } }.bury(:foo, :baz, :moo) #=> { foo: { baz: :moo } }
46
+ { foo: { baz: 'boo' } }.bury(:foo) #=> raises ArgumentError: '2 or more arguments required'
47
+ ```
48
+
49
+ `collect_keys`
50
+ ------
51
+ Returns an array with all keys converted using the block operation.
52
+
53
+ ```ruby
54
+ { foo: 'bar', 'baz' => :boo }.collect_keys #=> [:foo, 'baz']
55
+ { foo: 'bar', 'baz' => :boo }.collect_keys { |k| k.to_s.upcase } #=> ['FOO', BAZ']
56
+ ```
57
+
58
+ `collect_values`
59
+ ------
60
+ Returns an array with all values converted using the block operation.
61
+
62
+ ```ruby
63
+ { foo: 'bar', baz: :boo }.collect_values #=> ['bar', :boo]
64
+ { foo: 'bar', baz: :boo }.collect_values { |k| k.to_s.upcase } #=> ['BAR', BOO']
65
+ ```
66
+
67
+ `deep_merge(!)`
68
+ ------
69
+ Returns a new hash with self and other_hash merged recursively.
70
+
71
+ ```ruby
72
+ h1 = { a: true, b: { c: [1, 2, 3] } }
73
+ h2 = { a: false, b: { x: [3, 4, 5] } }
74
+
75
+ h1.deep_merge(h2) #=> { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
76
+ ```
77
+
78
+ `demote(!)`
79
+ ------
80
+ Moves a key value pair to the tail of the hash.
81
+
82
+ ```ruby
83
+ { a: 0, b: 1, c: 2 }.demote(:b) #=> { a: 0, c: 2, b: 1 }
84
+ { a: 0, b: 1, c: 2 }.demote!(:d) #=> { a: 0, b: 1, c: 2 }
85
+ ```
86
+
87
+ `denillify(!)`
88
+ ------
89
+ Converts `nil` into a given value.
90
+
91
+ ```ruby
92
+ { abc: nil, xyz: 1 }.denillify #=> { abc: 0, xyz: 1 }
93
+ { abc: nil, xyz: 1 }.denillify!(9) #=> { abc: 9, xyz: 1 }
94
+ ```
95
+
96
+ `except(!)`
97
+ ------
98
+ Returns a hash that includes everything but the given keys.
99
+
100
+ ```ruby
101
+ {}.except(:foo) #=> {}
102
+ { foo: 'foo', baz: 'baz', bar: 'bar' }.except(:foo) #=> { baz: 'baz', bar: 'bar' }
103
+ { :foo => 'foo', :baz => 'baz', :bar => 'bar' }.except(:baz, :bar) #=> { :foo => 'foo' }
104
+ ```
105
+
106
+ `extract!`
107
+ ------
108
+ Removes and returns the key value pairs matching the given keys.
109
+
110
+ ```ruby
111
+ {}.extract!(:foo) #=> {}
112
+ { foo: 'foo', baz: 'baz', bar: 'bar' }.extract!(:foo) #=> { foo: 'foo' }
113
+
114
+ ```
115
+
116
+ `hmap(!)`
117
+ ------
118
+ Returns a hash that is transformed in place.
119
+
120
+ ```ruby
121
+ { a: 1, b: 2, c: 3 }.hmap { |k, v| { k => v + 3 } } #=> { a: 4, b: 5, c: 6 }
122
+ ```
123
+
124
+ `nillify(!)`
125
+ ------
126
+ Transforms all blank values to `nil`.
127
+
128
+ ```ruby
129
+ { a: 1, b: 'test', c: nil, d: false, e: '', f: ' ' }.nillify #=> {a: 1, b: 'test', c: nil, d: nil, e: nil, f: nil}
130
+ ```
131
+
132
+ `only_fill(!)`
133
+ ------
134
+ Returns only key value pairs matching certain keys and any missing one.
135
+
136
+ ```ruby
137
+ {}.only_fill(:foo) #=> { foo: nil }
138
+ { :foo => 1, baz: 2 }.only_fill(:foo, :bar, placeholder: 0) #=> { foo: 1, bar: 0 }
139
+ ```
140
+
141
+ `pair?`
142
+ ------
143
+ Returns if the hash has a key with a matching value.
144
+
145
+ ```ruby
146
+ { a: 0 }.pair?(:a, 0) #=> true
147
+ { a: 0 }.pair?(:a, 2) #=> false
148
+ ```
149
+
150
+ `promote(!)`
151
+ ------
152
+ Moves a key value pair to the head of the hash.
153
+
154
+ ```ruby
155
+ { a: 0, b: 1, c: 2 }.promote(:b) #=> { b: 1, a: 0, c: 2 }
156
+ { a: 0, b: 1, c: 2 }.promote!(:d) #=> { a: 0, b: 1, c: 2 }
157
+ ```
158
+
159
+ `rename_keys(!)`
160
+ ------
161
+ Rename the keys of a hash.
162
+
163
+ ```ruby
164
+ { foo: 'foo', baz: 'baz' }.rename_keys(foo: :bar) #=> { bar: 'foo', baz: 'baz' }
165
+ { foo: 'foo', 'baz' => 'baz' }.rename_keys(foo: :bar, 'baz' => 'tick') #=> { bar: 'foo', tick: 'baz' }
166
+ ```
167
+
168
+ `reverse_merge(!)`
169
+ ------
170
+ Merges one hash into other hash (merge but in reverse).
171
+
172
+ ```ruby
173
+ {}.reverse_merge!(foo: 'bar') #=> { foo: 'bar' }
174
+ { foo: 'bar' }.reverse_merge!(baz: 'boo', boo: 'bam') #=> { foo: 'bar', baz: 'boo', boo: 'bam' }
175
+ ```
176
+
177
+ `sample(!)`
178
+ ------
179
+ Returns a random key-value pair.
180
+
181
+ ```ruby
182
+ h = { a: 1, b: 2, c: 3, d: 4 }
183
+
184
+ h.sample #=> [:c, 3]
185
+ h.sample! #=> [:a, 1]
186
+ ```
187
+
188
+ `sample_key(!)`
189
+ ------
190
+ Returns a random key.
191
+
192
+ ```ruby
193
+ h = { a: 1, b: 2, c: 3, d: 4 }
194
+
195
+ h.sample_key #=> :b
196
+ h.sample_key! #=> :d
197
+ ```
198
+
199
+ `sample_value(!)`
200
+ ------
201
+ Returns a random value.
202
+
203
+ ```ruby
204
+ h = { a: 1, b: 2, c: 3, d: 4 }
205
+
206
+ h.sample_value #=> 1
207
+ h.sample_value! #=> 3
208
+ ```
209
+
210
+ `shuffle(!)`
211
+ ------
212
+ Returns a hash with values arranged in new random order.
213
+
214
+ ```ruby
215
+ h = { a: 1, b: 2, c: 3, d: 4 }
216
+
217
+ h.shuffle #=> { b: 2, c: 3, a: 1, d: 4 }
218
+ h.shuffle! #=> { d: 4, b: 2, c: 3, a: 1 }
219
+ ```
220
+
221
+ `slice(!)` aka `only(!)`
222
+ ------
223
+ Returns a hash that includes only the given keys.
224
+
225
+ ```ruby
226
+ h = { a: 1, b: 2, c: 3, d: 4 }
227
+
228
+ h.slice(:a, :b) #=> { a: 1, b: 2 }
229
+ h.slice!(:a, :b) #=> { a: 1, b: 2 }
230
+ ```
231
+
232
+ `stringify_keys(!)`
233
+ ------
234
+ Converts the hash keys to strings.
235
+
236
+ ```ruby
237
+ { foo: 'foo', 'bar' => 'bar' }.stringify_keys #=> { 'foo' => 'foo', 'baz' => 'baz' }
238
+ ```
239
+
240
+ `strip(!)`
241
+ ------
242
+ Returns a hash with non `nil`, `false`, or blank values.
243
+
244
+ ```ruby
245
+ {}.strip #=> {}
246
+ { foo: nil, baz: false, boo: '', faz: ' ' }.strip #=> {}
247
+ { foo: 'bar', baz: false, boo: nil, boz: '', faz: ' ' }.strip #=> { foo: 'bar' }
248
+ ```
249
+
250
+ `symbolize_keys(!)`
251
+ ------
252
+ Converts the hash keys to symbols.
253
+
254
+ ```ruby
255
+ { foo: 'foo', 'bar' => 'bar' }.symbolize_keys #=> { foo: 'foo', baz: 'baz' }
256
+ ```
257
+
258
+ `symbolize_and_underscore_keys(!)`
259
+ ------
260
+ Symbolize and underscore hash keys.
261
+
262
+ ```ruby
263
+ { 'firstName' => 'foo', 'last Name' => 'test' }.symbolize_and_underscore_keys #=> { first_name: 'foo', last_name: 'test' }
264
+ ```
265
+
266
+ `to_object` aka `to_o`
267
+ ------
268
+ Converts an object to have an object like API.
269
+
270
+ ```ruby
271
+ { foo: { bar: true } }.to_object.foo.bar #=> true
272
+ ```
273
+
274
+ `vacant?`
275
+ ------
276
+ Returns if the value of a key is blank?.
277
+
278
+ ```ruby
279
+ {}.vacant?(:foo) #=> true
280
+ { foo: ' ' }.vacant?(:foo) #=> true
281
+ { foo: 'bar' }.vacant?(:foo) #=> false
282
+ ```
@@ -0,0 +1,43 @@
1
+ # Integer
2
+
3
+ `factorial`
4
+ ------
5
+ Calculates the factorial of an integer.
6
+
7
+ ```ruby
8
+ 4.factorial #=> 24
9
+ ```
10
+
11
+ `factors`
12
+ ------
13
+ Calculates all the factors of an integer.
14
+
15
+ ```ruby
16
+ 24.factors #=> [1, 24, 2, 12, 3, 8, 4, 6]
17
+ ```
18
+
19
+ `of`
20
+ ------
21
+ Like `times` but returns a collection of the yield results.
22
+
23
+ ```ruby
24
+ 3.of { |i| i + 1 } #=> ['1', '2', '3']
25
+ ```
26
+
27
+ `roman_numeral`
28
+ ------
29
+ Converts an integer to a roman_numeral numeral.
30
+
31
+ ```ruby
32
+ 0.roman_numeral #=> ''
33
+ 49.roman_numeral #=> 'XLIX'
34
+ -49.roman_numeral #=> '-XLIX'
35
+ ```
36
+
37
+ `to_time` aka `to_t`
38
+ ------
39
+ Returns a Time object for the given Integer.
40
+
41
+ ```ruby
42
+ 3.to_time #=> '1969-12-31 19:00:03.000000000 -0500'
43
+ ```
@@ -0,0 +1,31 @@
1
+ # Kernel
2
+
3
+ `caller_name`
4
+ ------
5
+ Returns the name of the method calling it.
6
+
7
+ ```ruby
8
+ def sample_key
9
+ caller_name(0)
10
+ end
11
+
12
+ sample_key #=> 'sample_key'
13
+ ```
14
+
15
+ `safe_eval`
16
+ ------
17
+ Try's to evaluate or returns self.
18
+
19
+ ```ruby
20
+ '[1,2,3]'.safe_eval #=> [1,2,3]
21
+ '[#1,2,3]'.safe_eval #=> '[#1,2,3]'
22
+ ```
23
+
24
+ `try_eval`
25
+ ------
26
+ Try's to evaluate or returns nil.
27
+
28
+ ```ruby
29
+ '[1,2,3]'.try_eval #=> [1,2,3]
30
+ '[#1,2,3]'.try_eval #=> nil
31
+ ```
@@ -0,0 +1,302 @@
1
+ # Numeric
2
+
3
+ `add`
4
+ ------
5
+ Returns the sum of two numbers.
6
+
7
+ ```ruby
8
+ 4.add(2) #=> 6
9
+ ```
10
+
11
+ `clamp`
12
+ ------
13
+ Returns the closest number outside of the lower and upper bound.
14
+
15
+ ```ruby
16
+ 1.clamp(3, 6) # => 3
17
+ 5.clamp(3..6) # => 5
18
+ 8.clamp(3, 6) # => 6
19
+ ```
20
+
21
+ `decrement`
22
+ ------
23
+ Returns the n decremented number.
24
+
25
+ ```ruby
26
+ 1.decrement #=> 0
27
+ 1.decrement(0.5) #=> 0.5
28
+ ```
29
+
30
+ `distance`
31
+ ------
32
+ Returns the absolute difference between numbers.
33
+
34
+ ```ruby
35
+ 5.distance(3) #=> 2
36
+ 3.distance(5) #=> 2
37
+ ```
38
+
39
+ `divide`
40
+ ------
41
+ Returns the division of two numbers.
42
+
43
+ ```ruby
44
+ 4.divide(2) #=> 2
45
+ 0.divide(2) #=> 0
46
+ 4.divide(0) #=> 0
47
+ ```
48
+
49
+ `equal_to?` aka `eq?`
50
+ ------
51
+ Returns if matching equality using `==`.
52
+
53
+ ```ruby
54
+ 3.equal_to?(2) #=> false
55
+ 3.equal_to?(3) #=> true
56
+ ```
57
+
58
+ `fraction`
59
+ ------
60
+ Returns the numbers after '.' of a float.
61
+
62
+ ```ruby
63
+ 1.0.fraction #=> 0.0
64
+ 12.2456.fraction #=> 0.2456
65
+ -12.2456.fraction #=> 0.2456
66
+ ```
67
+
68
+ `fraction?`
69
+ ------
70
+ Returns if its a fraction.
71
+
72
+ ```ruby
73
+ 1.0.fraction? #=> false
74
+ 12.2456.fraction? #=> true
75
+ ```
76
+
77
+ `greater_than?` aka `gt?`
78
+ ------
79
+ Returns if self is greater than n.
80
+
81
+ ```ruby
82
+ 3.greater_than?(2) #=> true
83
+ 3.greater_than?(3) #=> false
84
+ 3.greater_than?(4) #=> false
85
+ ```
86
+
87
+ `greater_than_or_equal_to?` aka `gteq?`
88
+ ------
89
+ Returns if self is greater than or equal to n.
90
+
91
+ ```ruby
92
+ 3.greater_than_or_equal_to?(2) #=> true
93
+ 3.greater_than_or_equal_to?(3) #=> true
94
+ 3.greater_than_or_equal_to?(4) #=> false
95
+ ```
96
+
97
+ `increment`
98
+ ------
99
+ Returns the n incremented number.
100
+
101
+ ```ruby
102
+ 1.increment #=> 2
103
+ 1.increment(0.5) #=> 1.5
104
+ ```
105
+
106
+ `inside?`
107
+ ------
108
+ Returns if n is greater than start and less than finish.
109
+ Similar to between but does not return true if equal to self.
110
+
111
+ ```ruby
112
+ 3.inside?(1, 5) #=> true
113
+ 3.inside?(3, 5) #=> false
114
+ ```
115
+
116
+ `less_than?` aka `lt?`
117
+ ------
118
+ Returns if self is less than n.
119
+
120
+ ```ruby
121
+ 3.less_than?(2) #=> false
122
+ 3.less_than?(3) #=> false
123
+ 3.less_than?(4) #=> true
124
+ ```
125
+
126
+ `less_than_or_equal_to?` aka `lteq?`
127
+ ------
128
+ Returns if self is less than or equal to n.
129
+
130
+ ```ruby
131
+ 3.less_than_or_equal_to?(2) #=> false
132
+ 3.less_than_or_equal_to?(3) #=> true
133
+ 3.less_than_or_equal_to?(4) #=> true
134
+ ```
135
+
136
+ `multiply`
137
+ ------
138
+ Returns the multiplication of two numbers.
139
+
140
+ ```ruby
141
+ 4.multiply(2) #=> 8
142
+ ```
143
+
144
+ `multiple_of?`
145
+ ------
146
+ Returns if a number can be evenly divided by n.
147
+
148
+ ```ruby
149
+ 9.multiple_of?(3) #=> true
150
+ 7.multiple_of?(3) #=> false
151
+ ```
152
+
153
+ `not_equal_to?` aka `not_eq?` aka `inequal_to?` aka `ineq?`
154
+ ------
155
+ Returns if not matching equality using `!=`.
156
+
157
+ ```ruby
158
+ 3.not_equal_to?(2) #=> true
159
+ 3.not_equal_to?(3) #=> false
160
+ ```
161
+
162
+ `ordinal`
163
+ ------
164
+ Returns the suffix that should be added to a number to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
165
+
166
+ ```ruby
167
+ '1'.ordinal #=> 'th'
168
+ '2'.ordinal #=> 'nd'
169
+ '3'.ordinal #=> 'rd'
170
+ '11'.ordinal #=> 'th'
171
+ ```
172
+
173
+ `ordinalize`
174
+ ------
175
+ transforms a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
176
+
177
+ ```ruby
178
+ '1'.ordinalize #=> '1th'
179
+ '2'.ordinalize #=> '2nd'
180
+ '3'.ordinalize #=> '3rd'
181
+ '11'.ordinalize #=> '4th'
182
+ ```
183
+
184
+ `outside?`
185
+ ------
186
+ Returns if n is less than start or greater than finish.
187
+
188
+ ```ruby
189
+ 3.outside?(4, 5) #=> true
190
+ 3.outside?(3, 5) #=> false
191
+ ```
192
+
193
+ `pad`
194
+ ------
195
+ Returns a string representation of the number padded with pad_num to a specified length.
196
+
197
+ ```ruby
198
+ 3.pad #=> '003'
199
+ 3.pad(pad_number: 1) #=> '113'
200
+ 3.pad(precision: 4) #=> '0003'
201
+ ```
202
+
203
+ `pad_precision`
204
+ ------
205
+ Returns a string of padded after the '.' to n amount.
206
+
207
+ Option | Type | Default
208
+ --- | --- | ---
209
+ pad_number | integer | 0
210
+ precision | integer | 2
211
+ separator | string | '...'
212
+
213
+ ```ruby
214
+ 3.pad_precision #=> '3.00'
215
+ 3.5.pad_precision #=> '3.50'
216
+ 3.pad_precision(pad_number: 1) #=> '3.11'
217
+ ```
218
+
219
+ `percentage_of`
220
+ ------
221
+ Returns the percentage of a number in relation to another number.
222
+
223
+ ```ruby
224
+ 0.percentage_of(4) #=> 0
225
+ 2.percentage_of(0) #=> 0
226
+ 2.percentage_of(4) #=> 50.0
227
+ ```
228
+
229
+ `power`
230
+ ------
231
+ Returns the nth power of a number.
232
+
233
+ ```ruby
234
+ 4.power(2) #=> 16
235
+ ```
236
+
237
+ `root`
238
+ ------
239
+ Returns the nth root of a number.
240
+
241
+ ```ruby
242
+ 4.root(2) #=> 2
243
+ ```
244
+
245
+ `subtract`
246
+ ------
247
+ Returns the difference of two numbers.
248
+
249
+ ```ruby
250
+ 4.subtract(2) #=> 2
251
+ ```
252
+
253
+ `to_currency`
254
+ ------
255
+ Converts a number to currency string.
256
+
257
+ Option | Type | Default
258
+ --- | --- | ---
259
+ precision | integer | 2
260
+ unit | string | '$'
261
+
262
+ ```ruby
263
+ 3.to_currency #=> '$3.00'
264
+ 3.1.to_currency #=> '$3.10'
265
+ 3.11.to_currency #=> '$3.11'
266
+ 3.11111.to_currency #=> '$3.11'
267
+ 3.to_currency(unit: '@') #=> '@3.00'
268
+ ```
269
+
270
+ `to_nearest value`
271
+ ------
272
+ return the value in values that is nearest to the number.
273
+
274
+ ```ruby
275
+ 5.to_nearest_value([1, 3, 6, 9]) #=> 6
276
+ 3.5.to_nearest_value([3.0, 3.3, 3.6, 3.9]) #=> 3.6
277
+ ```
278
+
279
+ `to_percentage`
280
+ ------
281
+ Converts a number to percentage string.
282
+
283
+ Option | Type | Default
284
+ --- | --- | ---
285
+ precision | integer | 2
286
+ unit | string | '%'
287
+
288
+ ```ruby
289
+ 3.to_percentage #=> '3.00%'
290
+ 3.1.to_percentage #=> '3.10%'
291
+ 3.11.to_percentage #=> '3.11%'
292
+ 3.11111.to_percentage #=> '3.11%'
293
+ 3.to_percentage(unit: '@') #=> '3.00@'
294
+ ```
295
+
296
+ `within?`
297
+ ------
298
+ Returns if another number is approximately equal within a given epsilon
299
+
300
+ ```ruby
301
+ 10.006.within?(10, 0.1) #=> true
302
+ ```