code-ruby 0.15.12 → 0.15.14

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ab71cebcf9036058b0aa1e3a6eaf2b09afca6152ca4fc79f4cf42c7bad5168e3
4
- data.tar.gz: 1aab6b0efed936151557b27b4ac5fb66cfa6c5c91c9c9042f9430ed62da1720d
3
+ metadata.gz: 6ae4360301b3c6c28a2ac0a340f07a699f030bc33ca160a0c4b7848535b0f162
4
+ data.tar.gz: be716086938c17a3afc952afc81a2fd6ce60cc49f22def8ae1b2b21115294824
5
5
  SHA512:
6
- metadata.gz: 8f495313b944bc0cdf210fee2c19365442192e37131e7a1043f40cebcbe10754dc7c168d575d2d241a45154445bda2e23e2925f52192a97a1bbe1d11dd09b883
7
- data.tar.gz: caef23b5de281011909169a3968c72c95cc9cf20d86825838b40d5ae0efa869ce60aeb86acc2ab78c23dffd5544bd076adc4ad74e85efe2ae2ffe784201fcbb7
6
+ metadata.gz: 01ed7ac37f81e5d76cd9d1e4d27eb2a9581ca03ae78642e1f8e57c920ff86141826998a466013a276f9eb1d5d0953c70e2bde712408c780b903ff68465e6d474
7
+ data.tar.gz: 52ccac205aa5a31a9235a0219a1218545a571bfcebe892e7cbbbeb1ba2eca19d4109648cec1d11a7aa6204179d7ef822674fb4624d99a716edc7c9c3c1b964c4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code-ruby (0.15.12)
4
+ code-ruby (0.15.14)
5
5
  activesupport
6
6
  bigdecimal
7
7
  did-you-mean
@@ -12,23 +12,24 @@ PATH
12
12
  GEM
13
13
  remote: https://rubygems.org/
14
14
  specs:
15
- activesupport (7.1.3.4)
15
+ activesupport (7.2.0)
16
16
  base64
17
17
  bigdecimal
18
- concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ concurrent-ruby (~> 1.0, >= 1.3.1)
19
19
  connection_pool (>= 2.2.5)
20
20
  drb
21
21
  i18n (>= 1.6, < 2)
22
+ logger (>= 1.4.2)
22
23
  minitest (>= 5.1)
23
- mutex_m
24
- tzinfo (~> 2.0)
24
+ securerandom (>= 0.3)
25
+ tzinfo (~> 2.0, >= 2.0.5)
25
26
  ast (2.4.2)
26
27
  base64 (0.2.0)
27
28
  bigdecimal (3.1.8)
28
29
  bundler-audit (0.9.1)
29
30
  bundler (>= 1.2.0, < 3)
30
31
  thor (~> 1.0)
31
- concurrent-ruby (1.3.3)
32
+ concurrent-ruby (1.3.4)
32
33
  connection_pool (2.4.1)
33
34
  did-you-mean (0.1.1)
34
35
  levenshtein (>= 0.2.0)
@@ -41,10 +42,10 @@ GEM
41
42
  zeitwerk
42
43
  language_server-protocol (3.17.0.3)
43
44
  levenshtein (0.2.2)
44
- minitest (5.24.1)
45
- mutex_m (0.2.0)
46
- parallel (1.25.1)
47
- parser (3.3.4.0)
45
+ logger (1.6.0)
46
+ minitest (5.25.0)
47
+ parallel (1.26.2)
48
+ parser (3.3.4.2)
48
49
  ast (~> 2.4.1)
49
50
  racc
50
51
  racc (1.8.1)
@@ -52,7 +53,7 @@ GEM
52
53
  rainbow (3.1.1)
53
54
  rake (13.2.1)
54
55
  regexp_parser (2.9.2)
55
- rexml (3.3.4)
56
+ rexml (3.3.5)
56
57
  strscan
57
58
  rspec (3.13.0)
58
59
  rspec-core (~> 3.13.0)
@@ -98,6 +99,7 @@ GEM
98
99
  rubocop-rails
99
100
  ruby-prof (1.7.0)
100
101
  ruby-progressbar (1.13.0)
102
+ securerandom (0.3.1)
101
103
  strscan (3.1.0)
102
104
  thor (1.3.1)
103
105
  tzinfo (2.0.6)
data/bin/code CHANGED
@@ -64,7 +64,10 @@ argv =
64
64
  end
65
65
  .parse!
66
66
 
67
- options[:input] = argv.join(" ") if options[:input].empty?
67
+ if options[:input].empty?
68
+ options[:input] = argv.join(" ")
69
+ options[:input] = File.read(options[:input]) if File.exist?(options[:input])
70
+ end
68
71
 
69
72
  abort <<~HELP if options[:input].empty?
70
73
  Usage: code INPUT
@@ -26,7 +26,7 @@ class Code
26
26
  sig(args) { Object }
27
27
  code_append(value)
28
28
  when "any?"
29
- sig(args) { Function }
29
+ sig(args) { Function.maybe }
30
30
  code_any?(value, **globals)
31
31
  when "detect"
32
32
  sig(args) { Function }
@@ -49,6 +49,9 @@ class Code
49
49
  when "map"
50
50
  sig(args) { Function }
51
51
  code_map(value, **globals)
52
+ when "map!"
53
+ sig(args) { Function }
54
+ code_map!(value, **globals)
52
55
  when "max"
53
56
  sig(args)
54
57
  code_max
@@ -56,7 +59,7 @@ class Code
56
59
  sig(args) { Function }
57
60
  code_max_by(value, **globals)
58
61
  when "none?"
59
- sig(args) { Function }
62
+ sig(args) { Function.maybe }
60
63
  code_none?(value, **globals)
61
64
  when "reduce"
62
65
  sig(args) { Function }
@@ -70,6 +73,12 @@ class Code
70
73
  when "select!"
71
74
  sig(args) { Function }
72
75
  code_select!(value, **globals)
76
+ when "reject"
77
+ sig(args) { Function }
78
+ code_reject(value, **globals)
79
+ when "reject!"
80
+ sig(args) { Function }
81
+ code_reject!(value, **globals)
73
82
  when "size"
74
83
  sig(args)
75
84
  code_size
@@ -84,15 +93,24 @@ class Code
84
93
  end
85
94
  end
86
95
 
87
- def code_any?(argument, **globals)
96
+ def code_any?(argument = nil, **globals)
97
+ argument ||= Nothing.new
98
+ index = 0
99
+
88
100
  Boolean.new(
89
- raw.any?.with_index do |element, index|
90
- argument.call(
91
- arguments: List.new([element, Integer.new(index), self]),
92
- **globals
93
- ).truthy?
101
+ raw.any? do |element|
102
+ if argument.nothing?
103
+ element.truthy?
104
+ else
105
+ argument.call(
106
+ arguments: List.new([element, Integer.new(index), self]),
107
+ **globals
108
+ ).truthy?
109
+ end
94
110
  rescue Error::Next => e
95
111
  e.value || Nothing.new
112
+ ensure
113
+ index += 1
96
114
  end
97
115
  )
98
116
  end
@@ -169,6 +187,19 @@ class Code
169
187
  )
170
188
  end
171
189
 
190
+ def code_map!(argument, **globals)
191
+ raw.map!.with_index do |element, index|
192
+ argument.call(
193
+ arguments: List.new([element, Integer.new(index), self]),
194
+ **globals
195
+ )
196
+ rescue Error::Next => e
197
+ e.value || Nothing.new
198
+ end
199
+
200
+ self
201
+ end
202
+
172
203
  def code_max
173
204
  raw.max || Nothing.new
174
205
  end
@@ -184,15 +215,24 @@ class Code
184
215
  end || Nothing.new
185
216
  end
186
217
 
187
- def code_none?(argument, **globals)
218
+ def code_none?(argument = nil, **globals)
219
+ argument ||= Nothing.new
220
+ index = 0
221
+
188
222
  Boolean.new(
189
- raw.none?.with_index do |element, index|
190
- argument.call(
191
- arguments: List.new([element, Integer.new(index), self]),
192
- **globals
193
- ).truthy?
223
+ raw.none? do |element|
224
+ if argument.nothing?
225
+ element.truthy?
226
+ else
227
+ argument.call(
228
+ arguments: List.new([element, Integer.new(index), self]),
229
+ **globals
230
+ ).truthy?
231
+ end
194
232
  rescue Error::Next => e
195
233
  (e.value || Nothing.new).truthy?
234
+ ensure
235
+ index += 1
196
236
  end
197
237
  )
198
238
  end
@@ -238,6 +278,32 @@ class Code
238
278
  self
239
279
  end
240
280
 
281
+ def code_reject(argument, **globals)
282
+ List.new(
283
+ raw.reject.with_index do |element, index|
284
+ argument.call(
285
+ arguments: List.new([element, Integer.new(index), self]),
286
+ **globals
287
+ ).truthy?
288
+ rescue Error::Next => e
289
+ (e.value || Nothing.new).truthy?
290
+ end
291
+ )
292
+ end
293
+
294
+ def code_reject!(argument, **globals)
295
+ raw.reject!.with_index do |element, index|
296
+ argument.call(
297
+ arguments: List.new([element, Integer.new(index), self]),
298
+ **globals
299
+ ).truthy?
300
+ rescue Error::Next => e
301
+ (e.value || Nothing.new).truthy?
302
+ end
303
+
304
+ self
305
+ end
306
+
241
307
  def code_join(separator = nil)
242
308
  separator ||= String.new("")
243
309
 
@@ -10,6 +10,10 @@ class Code
10
10
  def truthy?
11
11
  false
12
12
  end
13
+
14
+ def nothing?
15
+ true
16
+ end
13
17
  end
14
18
  end
15
19
  end
data/lib/code/object.rb CHANGED
@@ -7,6 +7,14 @@ class Code
7
7
  def initialize(...)
8
8
  end
9
9
 
10
+ def self.nothing?
11
+ false
12
+ end
13
+
14
+ def nothing?
15
+ false
16
+ end
17
+
10
18
  def self.maybe
11
19
  Type::Maybe.new(self)
12
20
  end
data/lib/code/version.rb CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  require_relative "../code"
4
4
 
5
- Code::Version = Gem::Version.new("0.15.12")
5
+ Code::Version = Gem::Version.new("0.15.14")
data/spec/code_spec.rb CHANGED
@@ -302,6 +302,16 @@ RSpec.describe Code do
302
302
  ["[1, 2].join(',')", "'1,2'"],
303
303
  ["[nothing, 2].join(',')", "',2'"],
304
304
  ["[nothing, nothing].join", "''"],
305
+ ["[1, 2].select(&:even?)", "[2]"],
306
+ ["[1, 2].reject(&:even?)", "[1]"],
307
+ ["a = [1, 2] a.select!(&:even?) a", "[2]"],
308
+ ["a = [1, 2] a.reject!(&:even?) a", "[1]"],
309
+ ["[1, 2].map(&:even?)", "[false, true]"],
310
+ ["a = [1, 2] a.map!(&:even?) a", "[false, true]"],
311
+ ["[1, 2, 3].any?", "true"],
312
+ ["[1, 2, 3].any?(&:even?)", "true"],
313
+ ["[1, 2, 3].none?", "false"],
314
+ ["[1, 2, 3].none?(&:even?)", "false"],
305
315
  ["'{1} {2}'", "'1 2'"],
306
316
  ["", ""]
307
317
  ].each do |input, expected|
@@ -323,4 +333,19 @@ RSpec.describe Code do
323
333
  expect(output.string).to eq(expected)
324
334
  end
325
335
  end
336
+
337
+ it "doesn't crash with dictionnary as parameter" do
338
+ Code.evaluate(<<~INPUT)
339
+ [
340
+ {
341
+ videos: [{}]
342
+ },
343
+ {
344
+ videos: [{}]
345
+ }
346
+ ].map do |post|
347
+ post.videos.map { |video| }
348
+ end
349
+ INPUT
350
+ end
326
351
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.12
4
+ version: 0.15.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-06 00:00:00.000000000 Z
11
+ date: 2024-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport