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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/Gemfile.lock +1 -1
- data/lib/lite/ruby/array.rb +221 -219
- data/lib/lite/ruby/boolean.rb +18 -16
- data/lib/lite/ruby/date.rb +18 -16
- data/lib/lite/ruby/enumerable.rb +169 -167
- data/lib/lite/ruby/hash.rb +309 -307
- data/lib/lite/ruby/helpers/date_helper.rb +97 -95
- data/lib/lite/ruby/helpers/time_helper.rb +79 -77
- data/lib/lite/ruby/integer.rb +57 -55
- data/lib/lite/ruby/kernel.rb +21 -19
- data/lib/lite/ruby/numeric.rb +189 -187
- data/lib/lite/ruby/object.rb +138 -136
- data/lib/lite/ruby/open_struct.rb +30 -28
- data/lib/lite/ruby/range.rb +23 -21
- data/lib/lite/ruby/string.rb +495 -493
- data/lib/lite/ruby/struct.rb +15 -13
- data/lib/lite/ruby/time.rb +27 -25
- data/lib/lite/ruby/version.rb +1 -1
- metadata +1 -1
data/lib/lite/ruby/enumerable.rb
CHANGED
@@ -1,227 +1,229 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
if Lite::Ruby.configuration.monkey_patches.include?('enumerable')
|
4
|
+
module Enumerable
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
def cluster
|
7
|
+
each_with_object([]) do |ele, results|
|
8
|
+
last_res = results.last
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
if last_res && (yield(ele) == yield(last_res.last))
|
11
|
+
last_res << ele
|
12
|
+
else
|
13
|
+
results << [ele]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def cluster_by(&block)
|
19
|
+
group_by(&block).sort.transpose.pop || []
|
20
|
+
end
|
21
|
+
|
22
|
+
def deduce(identity = 0, &block)
|
23
|
+
if block_given?
|
24
|
+
map(&block).deduce(identity)
|
11
25
|
else
|
12
|
-
|
26
|
+
inject { |acc, val| acc - val } || identity
|
13
27
|
end
|
14
28
|
end
|
15
|
-
end
|
16
29
|
|
17
|
-
|
18
|
-
|
19
|
-
|
30
|
+
def drop_last(num)
|
31
|
+
collection_size = to_a.size
|
32
|
+
return self if num > collection_size
|
20
33
|
|
21
|
-
|
22
|
-
if block_given?
|
23
|
-
map(&block).deduce(identity)
|
24
|
-
else
|
25
|
-
inject { |acc, val| acc - val } || identity
|
34
|
+
self[0...(collection_size - num)]
|
26
35
|
end
|
27
|
-
end
|
28
36
|
|
29
|
-
|
30
|
-
|
31
|
-
|
37
|
+
def drop_last_if
|
38
|
+
dropping = true
|
39
|
+
reverse_each.with_object([]) do |val, arr|
|
40
|
+
next if dropping &&= yield(val)
|
32
41
|
|
33
|
-
|
34
|
-
|
42
|
+
arr.unshift(val)
|
43
|
+
end
|
44
|
+
end
|
35
45
|
|
36
|
-
|
37
|
-
|
38
|
-
reverse_each.with_object([]) do |val, arr|
|
39
|
-
next if dropping &&= yield(val)
|
46
|
+
def exactly?(num)
|
47
|
+
found_count = 0
|
40
48
|
|
41
|
-
|
49
|
+
if block_given?
|
50
|
+
each { |*opt| found_count += 1 if yield(*opt) }
|
51
|
+
else
|
52
|
+
each { |opt| found_count += 1 if opt }
|
53
|
+
end
|
54
|
+
|
55
|
+
found_count > num ? false : num == found_count
|
42
56
|
end
|
43
|
-
end
|
44
57
|
|
45
|
-
|
46
|
-
|
58
|
+
# rubocop:disable Style/CaseEquality
|
59
|
+
def excase?(object)
|
60
|
+
none? { |val| object === val }
|
61
|
+
end
|
62
|
+
# rubocop:enable Style/CaseEquality
|
47
63
|
|
48
|
-
|
49
|
-
|
50
|
-
else
|
51
|
-
each { |opt| found_count += 1 if opt }
|
64
|
+
def exclude?(object)
|
65
|
+
!include?(object)
|
52
66
|
end
|
53
67
|
|
54
|
-
|
55
|
-
|
68
|
+
def expand
|
69
|
+
map { |val| val.is_a?(Enumerable) ? val.expand : val }
|
70
|
+
end
|
56
71
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
72
|
+
def exponential(identity = 0, &block)
|
73
|
+
if block_given?
|
74
|
+
map(&block).exponential(identity)
|
75
|
+
else
|
76
|
+
inject { |acc, val| acc**val } || identity
|
77
|
+
end
|
78
|
+
end
|
62
79
|
|
63
|
-
|
64
|
-
|
65
|
-
|
80
|
+
def frequency
|
81
|
+
each_with_object(Hash.new(0)) { |val, hash| hash[val] += 1 }
|
82
|
+
end
|
66
83
|
|
67
|
-
|
68
|
-
map { |val| val.is_a?(Enumerable) ? val.expand : val }
|
69
|
-
end
|
84
|
+
alias occurrences frequency
|
70
85
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
else
|
75
|
-
inject { |acc, val| acc**val } || identity
|
86
|
+
# rubocop:disable Style/CaseEquality
|
87
|
+
def incase?(object)
|
88
|
+
any? { |val| object === val }
|
76
89
|
end
|
77
|
-
|
90
|
+
# rubocop:enable Style/CaseEquality
|
91
|
+
|
92
|
+
# rubocop:disable Metrics/MethodLength
|
93
|
+
def interpose(sep, &block)
|
94
|
+
enum = Enumerator.new do |val|
|
95
|
+
items = each
|
96
|
+
|
97
|
+
loop do
|
98
|
+
begin
|
99
|
+
val << items.next
|
100
|
+
rescue StopIteration
|
101
|
+
break
|
102
|
+
end
|
103
|
+
|
104
|
+
begin
|
105
|
+
items.peek
|
106
|
+
rescue StopIteration
|
107
|
+
break
|
108
|
+
else
|
109
|
+
val << sep
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
78
113
|
|
79
|
-
|
80
|
-
|
81
|
-
|
114
|
+
block ? enum.each(&block) : enum
|
115
|
+
end
|
116
|
+
# rubocop:enable Metrics/MethodLength
|
82
117
|
|
83
|
-
|
118
|
+
def many?
|
119
|
+
found_count = 0
|
84
120
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
# rubocop:enable Style/CaseEquality
|
90
|
-
|
91
|
-
# rubocop:disable Metrics/MethodLength
|
92
|
-
def interpose(sep, &block)
|
93
|
-
enum = Enumerator.new do |val|
|
94
|
-
items = each
|
95
|
-
|
96
|
-
loop do
|
97
|
-
begin
|
98
|
-
val << items.next
|
99
|
-
rescue StopIteration
|
100
|
-
break
|
121
|
+
if block_given?
|
122
|
+
any? do |val|
|
123
|
+
found_count += 1 if yield(val)
|
124
|
+
found_count > 1
|
101
125
|
end
|
126
|
+
else
|
127
|
+
any? { (found_count += 1) > 1 }
|
128
|
+
end
|
129
|
+
end
|
102
130
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
131
|
+
def modulate(modulo)
|
132
|
+
if modulo == 1
|
133
|
+
to_a
|
134
|
+
elsif size % modulo != 0
|
135
|
+
raise ArgumentError, "Invalid modulo: #{modulo.inspect}"
|
136
|
+
else
|
137
|
+
(0...size).each_with_object(Array.new(modulo, [])) do |i, array|
|
138
|
+
array[i % modulo] += [self[i]]
|
109
139
|
end
|
110
140
|
end
|
111
141
|
end
|
112
142
|
|
113
|
-
|
114
|
-
|
115
|
-
|
143
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
144
|
+
def occur(amount = nil)
|
145
|
+
result = Hash.new { |hash, key| hash[key] = [] }
|
146
|
+
|
147
|
+
each do |item|
|
148
|
+
key = item
|
149
|
+
result[key] << item
|
150
|
+
end
|
116
151
|
|
117
|
-
|
118
|
-
|
152
|
+
if block_given?
|
153
|
+
result.select! { |_key, values| yield(values.size) }
|
154
|
+
else
|
155
|
+
raise ArgumentError, 'Invalid occur amount' unless amount
|
119
156
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
157
|
+
if amount.is_a?(Range)
|
158
|
+
result.select! { |_key, values| amount.include?(values.size) }
|
159
|
+
else
|
160
|
+
result.select! { |_key, values| values.size == amount }
|
161
|
+
end
|
124
162
|
end
|
125
|
-
|
126
|
-
|
163
|
+
|
164
|
+
result.values.flatten.uniq
|
127
165
|
end
|
128
|
-
|
166
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
129
167
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
else
|
136
|
-
(0...size).each_with_object(Array.new(modulo, [])) do |i, array|
|
137
|
-
array[i % modulo] += [self[i]]
|
168
|
+
def produce(identity = 0, &block)
|
169
|
+
if block_given?
|
170
|
+
map(&block).produce(identity)
|
171
|
+
else
|
172
|
+
inject { |acc, val| acc * val } || identity
|
138
173
|
end
|
139
174
|
end
|
140
|
-
end
|
141
175
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
result[key] << item
|
176
|
+
def quotient(identity = 0, &block)
|
177
|
+
if block_given?
|
178
|
+
map(&block).quotient(identity)
|
179
|
+
else
|
180
|
+
inject { |acc, val| acc / val } || identity
|
181
|
+
end
|
149
182
|
end
|
150
183
|
|
151
|
-
|
152
|
-
|
153
|
-
else
|
154
|
-
raise ArgumentError, 'Invalid occur amount' unless amount
|
184
|
+
def several?
|
185
|
+
found_count = 0
|
155
186
|
|
156
|
-
if
|
157
|
-
|
187
|
+
if block_given?
|
188
|
+
each { |*opt| found_count += 1 if yield(*opt) }
|
158
189
|
else
|
159
|
-
|
190
|
+
each { |opt| found_count += 1 if opt }
|
160
191
|
end
|
161
|
-
end
|
162
|
-
|
163
|
-
result.values.flatten.uniq
|
164
|
-
end
|
165
|
-
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
166
192
|
|
167
|
-
|
168
|
-
if block_given?
|
169
|
-
map(&block).produce(identity)
|
170
|
-
else
|
171
|
-
inject { |acc, val| acc * val } || identity
|
193
|
+
found_count > 1
|
172
194
|
end
|
173
|
-
end
|
174
195
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
196
|
+
# rubocop:disable Metrics/MethodLength
|
197
|
+
def squeeze(*limited_to)
|
198
|
+
first = true
|
199
|
+
current = nil
|
200
|
+
|
201
|
+
each_with_object([]) do |val, array|
|
202
|
+
if !limited_to.empty? && !limited_to.include?(val)
|
203
|
+
array << val
|
204
|
+
elsif first || current != val
|
205
|
+
array << val
|
206
|
+
first = false
|
207
|
+
current = val
|
208
|
+
end
|
209
|
+
end
|
180
210
|
end
|
181
|
-
|
211
|
+
# rubocop:enable Metrics/MethodLength
|
182
212
|
|
183
|
-
|
184
|
-
|
213
|
+
def take_last(num)
|
214
|
+
collection_size = to_a.size
|
215
|
+
return self if num > collection_size
|
185
216
|
|
186
|
-
|
187
|
-
each { |*opt| found_count += 1 if yield(*opt) }
|
188
|
-
else
|
189
|
-
each { |opt| found_count += 1 if opt }
|
217
|
+
self[(collection_size - num)..-1]
|
190
218
|
end
|
191
219
|
|
192
|
-
|
193
|
-
|
220
|
+
def take_last_if
|
221
|
+
reverse_each.with_object([]) do |val, arr|
|
222
|
+
break arr unless yield(val)
|
194
223
|
|
195
|
-
|
196
|
-
def squeeze(*limited_to)
|
197
|
-
first = true
|
198
|
-
current = nil
|
199
|
-
|
200
|
-
each_with_object([]) do |val, array|
|
201
|
-
if !limited_to.empty? && !limited_to.include?(val)
|
202
|
-
array << val
|
203
|
-
elsif first || current != val
|
204
|
-
array << val
|
205
|
-
first = false
|
206
|
-
current = val
|
224
|
+
arr.unshift(val)
|
207
225
|
end
|
208
226
|
end
|
209
|
-
end
|
210
|
-
# rubocop:enable Metrics/MethodLength
|
211
|
-
|
212
|
-
def take_last(num)
|
213
|
-
collection_size = to_a.size
|
214
|
-
return self if num > collection_size
|
215
|
-
|
216
|
-
self[(collection_size - num)..-1]
|
217
|
-
end
|
218
|
-
|
219
|
-
def take_last_if
|
220
|
-
reverse_each.with_object([]) do |val, arr|
|
221
|
-
break arr unless yield(val)
|
222
227
|
|
223
|
-
arr.unshift(val)
|
224
|
-
end
|
225
228
|
end
|
226
|
-
|
227
229
|
end
|