lite-ruby 1.0.14 → 1.0.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,261 +1,263 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Numeric
3
+ if Lite::Ruby.configuration.monkey_patches.include?('numeric')
4
+ class Numeric
4
5
 
5
- def add(num)
6
- self + num
7
- end
8
-
9
- def at_least(lower)
10
- self >= lower ? self : lower
11
- end
6
+ def add(num)
7
+ self + num
8
+ end
12
9
 
13
- def at_most(upper)
14
- self <= upper ? self : upper
15
- end
10
+ def at_least(lower)
11
+ self >= lower ? self : lower
12
+ end
16
13
 
17
- # rubocop:disable Metrics/MethodLength
18
- def clamp(minimum, maximum = nil)
19
- if minimum.is_a?(Range)
20
- maximum = minimum.max
21
- minimum = minimum.min
14
+ def at_most(upper)
15
+ self <= upper ? self : upper
22
16
  end
23
17
 
24
- if minimum > self
25
- minimum
26
- elsif maximum < self
27
- maximum
28
- else
29
- self
18
+ # rubocop:disable Metrics/MethodLength
19
+ def clamp(minimum, maximum = nil)
20
+ if minimum.is_a?(Range)
21
+ maximum = minimum.max
22
+ minimum = minimum.min
23
+ end
24
+
25
+ if minimum > self
26
+ minimum
27
+ elsif maximum < self
28
+ maximum
29
+ else
30
+ self
31
+ end
30
32
  end
31
- end
32
- # rubocop:enable Metrics/MethodLength
33
+ # rubocop:enable Metrics/MethodLength
33
34
 
34
- def close?(number, epsilon = 0.01)
35
- return self == number if epsilon.zero?
35
+ def close?(number, epsilon = 0.01)
36
+ return self == number if epsilon.zero?
36
37
 
37
- a = to_f
38
- b = number.to_f
38
+ a = to_f
39
+ b = number.to_f
39
40
 
40
- if a.zero? || b.zero?
41
- (a - b).abs < epsilon
42
- else
43
- (a / b - 1).abs < epsilon
41
+ if a.zero? || b.zero?
42
+ (a - b).abs < epsilon
43
+ else
44
+ (a / b - 1).abs < epsilon
45
+ end
44
46
  end
45
- end
46
47
 
47
- def decrement(amount = 1.0)
48
- self - amount
49
- end
48
+ def decrement(amount = 1.0)
49
+ self - amount
50
+ end
50
51
 
51
- def delimit(options = {})
52
- delimiter = options[:delimiter] || ','
53
- separator = options[:separator] || '.'
52
+ def delimit(options = {})
53
+ delimiter = options[:delimiter] || ','
54
+ separator = options[:separator] || '.'
54
55
 
55
- digits, decimals = to_s.split('.')
56
- digits = digits.reverse.chars.each_slice(3).map(&:join).join(delimiter).reverse
56
+ digits, decimals = to_s.split('.')
57
+ digits = digits.reverse.chars.each_slice(3).map(&:join).join(delimiter).reverse
57
58
 
58
- return digits unless decimals
59
+ return digits unless decimals
59
60
 
60
- [digits, decimals].join(separator)
61
- end
61
+ [digits, decimals].join(separator)
62
+ end
62
63
 
63
- def delta(num)
64
- (self - num).abs
65
- end
64
+ def delta(num)
65
+ (self - num).abs
66
+ end
66
67
 
67
- def distance(num)
68
- self - num
69
- end
68
+ def distance(num)
69
+ self - num
70
+ end
70
71
 
71
- def divide(num)
72
- return num if num.zero?
72
+ def divide(num)
73
+ return num if num.zero?
73
74
 
74
- self / num
75
- end
75
+ self / num
76
+ end
76
77
 
77
- def equal_to?(num)
78
- self == num
79
- end
78
+ def equal_to?(num)
79
+ self == num
80
+ end
80
81
 
81
- alias eq? equal_to?
82
+ alias eq? equal_to?
82
83
 
83
- def fraction
84
- (self - truncate).abs
85
- end
84
+ def fraction
85
+ (self - truncate).abs
86
+ end
86
87
 
87
- def fraction?
88
- fraction != 0.0
89
- end
88
+ def fraction?
89
+ fraction != 0.0
90
+ end
90
91
 
91
- def greater_than?(num)
92
- num < self
93
- end
92
+ def greater_than?(num)
93
+ num < self
94
+ end
94
95
 
95
- alias gt? greater_than?
96
+ alias gt? greater_than?
96
97
 
97
- def greater_than_or_equal_to?(num)
98
- num <= self
99
- end
98
+ def greater_than_or_equal_to?(num)
99
+ num <= self
100
+ end
100
101
 
101
- alias gteq? greater_than_or_equal_to?
102
+ alias gteq? greater_than_or_equal_to?
102
103
 
103
- def increment(amount = 1.0)
104
- self + amount
105
- end
104
+ def increment(amount = 1.0)
105
+ self + amount
106
+ end
106
107
 
107
- def inside?(start, finish)
108
- (start < self) && (finish > self)
109
- end
108
+ def inside?(start, finish)
109
+ (start < self) && (finish > self)
110
+ end
110
111
 
111
- def less_than?(num)
112
- num > self
113
- end
112
+ def less_than?(num)
113
+ num > self
114
+ end
114
115
 
115
- alias lt? less_than?
116
+ alias lt? less_than?
116
117
 
117
- def less_than_or_equal_to?(num)
118
- num >= self
119
- end
118
+ def less_than_or_equal_to?(num)
119
+ num >= self
120
+ end
120
121
 
121
- alias lteq? less_than_or_equal_to?
122
+ alias lteq? less_than_or_equal_to?
122
123
 
123
- def markdown_percentage(percent)
124
- to_f * ((100.0 - percent.to_f) / 100.0)
125
- end
124
+ def markdown_percentage(percent)
125
+ to_f * ((100.0 - percent.to_f) / 100.0)
126
+ end
126
127
 
127
- def markup_percentage(percent)
128
- to_f + (to_f * (percent.to_f / 100.0))
129
- end
128
+ def markup_percentage(percent)
129
+ to_f + (to_f * (percent.to_f / 100.0))
130
+ end
130
131
 
131
- def multiply(num)
132
- self * num
133
- end
132
+ def multiply(num)
133
+ self * num
134
+ end
134
135
 
135
- def multiple_of?(number)
136
- return zero? if number.zero?
136
+ def multiple_of?(number)
137
+ return zero? if number.zero?
137
138
 
138
- modulo(number).zero?
139
- end
139
+ modulo(number).zero?
140
+ end
140
141
 
141
- def not_equal_to?(num)
142
- self != num
143
- end
142
+ def not_equal_to?(num)
143
+ self != num
144
+ end
144
145
 
145
- alias not_eq? not_equal_to?
146
- alias inequal_to? not_equal_to?
147
- alias ineq? not_equal_to?
146
+ alias not_eq? not_equal_to?
147
+ alias inequal_to? not_equal_to?
148
+ alias ineq? not_equal_to?
148
149
 
149
- def ordinal
150
- return 'th' if (11..13).cover?(abs % 100)
150
+ def ordinal
151
+ return 'th' if (11..13).cover?(abs % 100)
151
152
 
152
- case abs % 10
153
- when 1 then 'st'
154
- when 2 then 'nd'
155
- when 3 then 'rd'
156
- else 'th'
153
+ case abs % 10
154
+ when 1 then 'st'
155
+ when 2 then 'nd'
156
+ when 3 then 'rd'
157
+ else 'th'
158
+ end
157
159
  end
158
- end
159
160
 
160
- def ordinalize
161
- "#{self}#{ordinal}"
162
- end
161
+ def ordinalize
162
+ "#{self}#{ordinal}"
163
+ end
163
164
 
164
- def outside?(start, finish)
165
- (start > self) || (finish < self)
166
- end
165
+ def outside?(start, finish)
166
+ (start > self) || (finish < self)
167
+ end
167
168
 
168
- def pad(options = {})
169
- pad_number = options[:pad_number] || 0
170
- precision = options[:precision] || 3
169
+ def pad(options = {})
170
+ pad_number = options[:pad_number] || 0
171
+ precision = options[:precision] || 3
171
172
 
172
- to_s.rjust(precision, pad_number.to_s)
173
- end
173
+ to_s.rjust(precision, pad_number.to_s)
174
+ end
174
175
 
175
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
176
- # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
177
- def pad_precision(options = {})
178
- pad_number = options[:pad_number] || 0
179
- precision = options[:precision] || 2
180
- separator = options[:separator] || '.'
181
- string = to_s
176
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
177
+ # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
178
+ def pad_precision(options = {})
179
+ pad_number = options[:pad_number] || 0
180
+ precision = options[:precision] || 2
181
+ separator = options[:separator] || '.'
182
+ string = to_s
183
+
184
+ string << separator unless string.include?(separator)
185
+ ljust_count = string.split(separator).first.length
186
+ ljust_count += (string.count(separator) + precision) if precision.positive?
187
+
188
+ if ljust_count >= string.length
189
+ string.ljust(ljust_count, pad_number.to_s)
190
+ else
191
+ string[0..(ljust_count - 1)]
192
+ end
193
+ end
194
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
195
+ # rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
182
196
 
183
- string << separator unless string.include?(separator)
184
- ljust_count = string.split(separator).first.length
185
- ljust_count += (string.count(separator) + precision) if precision.positive?
197
+ def percentage_of(number)
198
+ return 0 if zero? || number.zero?
186
199
 
187
- if ljust_count >= string.length
188
- string.ljust(ljust_count, pad_number.to_s)
189
- else
190
- string[0..(ljust_count - 1)]
200
+ (self / number.to_f) * 100.0
191
201
  end
192
- end
193
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
194
- # rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
195
202
 
196
- def percentage_of(number)
197
- return 0 if zero? || number.zero?
203
+ def power(num)
204
+ self**num
205
+ end
198
206
 
199
- (self / number.to_f) * 100.0
200
- end
207
+ def range(value)
208
+ (self - value)..(self + value)
209
+ end
201
210
 
202
- def power(num)
203
- self**num
204
- end
211
+ alias plus_minus range
205
212
 
206
- def range(value)
207
- (self - value)..(self + value)
208
- end
209
-
210
- alias plus_minus range
213
+ def root(num)
214
+ self**(1.0 / num)
215
+ end
211
216
 
212
- def root(num)
213
- self**(1.0 / num)
214
- end
217
+ def subtract(num)
218
+ self - num
219
+ end
215
220
 
216
- def subtract(num)
217
- self - num
218
- end
221
+ def to_currency(options = {})
222
+ unit = options[:unit] || '$'
219
223
 
220
- def to_currency(options = {})
221
- unit = options[:unit] || '$'
224
+ "#{unit}#{pad_precision(options.only(:precision))}"
225
+ end
222
226
 
223
- "#{unit}#{pad_precision(options.only(:precision))}"
224
- end
227
+ def to_nearest_value(values = [])
228
+ return self if values.length.zero?
225
229
 
226
- def to_nearest_value(values = [])
227
- return self if values.length.zero?
230
+ value = values.first
231
+ difference = (self - value).abs
228
232
 
229
- value = values.first
230
- difference = (self - value).abs
233
+ values.each do |val|
234
+ next unless (self - val).abs < difference
231
235
 
232
- values.each do |val|
233
- next unless (self - val).abs < difference
236
+ difference = (self - val).abs
237
+ value = val
238
+ end
234
239
 
235
- difference = (self - val).abs
236
- value = val
240
+ value
237
241
  end
238
242
 
239
- value
240
- end
241
-
242
- def to_percentage(options = {})
243
- unit = options[:unit] || '%'
243
+ def to_percentage(options = {})
244
+ unit = options[:unit] || '%'
244
245
 
245
- "#{pad_precision(options.only(:precision))}#{unit}"
246
- end
246
+ "#{pad_precision(options.only(:precision))}#{unit}"
247
+ end
247
248
 
248
- def within?(number, epsilon = 0.01)
249
- return number == self if epsilon.zero?
249
+ def within?(number, epsilon = 0.01)
250
+ return number == self if epsilon.zero?
250
251
 
251
- alpha = to_f
252
- beta = number.to_f
252
+ alpha = to_f
253
+ beta = number.to_f
253
254
 
254
- if alpha.zero? || beta.zero?
255
- (alpha - beta).abs < epsilon
256
- else
257
- (alpha / beta - 1).abs < epsilon
255
+ if alpha.zero? || beta.zero?
256
+ (alpha - beta).abs < epsilon
257
+ else
258
+ (alpha / beta - 1).abs < epsilon
259
+ end
258
260
  end
259
- end
260
261
 
262
+ end
261
263
  end