Module: Odin::Transform::Verbs::CollectionVerbs

Defined in:
lib/odin/transform/verbs/collection_verbs.rb

Class Method Summary collapse

Class Method Details

.compare_dyn_values(a, b) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/odin/transform/verbs/collection_verbs.rb', line 23

def compare_dyn_values(a, b)
  av = NumericVerbs.to_double(a)
  bv = NumericVerbs.to_double(b)
  if av && bv
    av <=> bv
  else
    a.to_string <=> b.to_string
  end
end

.extract_items(v) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/odin/transform/verbs/collection_verbs.rb', line 9

def extract_items(v)
  return [] if v.nil? || v.null?
  return v.value if v.array?
  if v.string?
    begin
      parsed = Types::DynValue.extract_array(v.value)
      return parsed.value
    rescue
      return []
    end
  end
  []
end

.matches_condition?(item, field, op, compare) ⇒ Boolean

Field/operator/value condition matching used by find, findIndex, partition.

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/odin/transform/verbs/collection_verbs.rb', line 41

def matches_condition?(item, field, op, compare)
  val = item.object? ? item.get(field) : item
  val ||= Types::DynValue.of_null
  case op
  when "=", "==" then val.to_string == compare.to_string
  when "!=", "<>" then val.to_string != compare.to_string
  when "<" then (NumericVerbs.to_double(val) || 0) < (NumericVerbs.to_double(compare) || 0)
  when "<=" then (NumericVerbs.to_double(val) || 0) <= (NumericVerbs.to_double(compare) || 0)
  when ">" then (NumericVerbs.to_double(val) || 0) > (NumericVerbs.to_double(compare) || 0)
  when ">=" then (NumericVerbs.to_double(val) || 0) >= (NumericVerbs.to_double(compare) || 0)
  when "contains" then val.to_string.include?(compare.to_string)
  when "startsWith" then val.to_string.start_with?(compare.to_string)
  when "endsWith" then val.to_string.end_with?(compare.to_string)
  else false
  end
end

.register(registry) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# File 'lib/odin/transform/verbs/collection_verbs.rb', line 58

def register(registry)
  dv = Types::DynValue

  registry["filter"] = ->(args, _ctx) {
    arr_val = args[0]
    items = CollectionVerbs.extract_items(arr_val)
    return dv.of_array([]) if items.empty?

    if args.length >= 4
      # filter(array, fieldName, operator, compareValue)
      field = args[1]&.to_string || ""
      op = args[2]&.to_string || "="
      compare = args[3]

      filtered = items.select do |item|
        val = item.object? ? item.get(field) : item
        next false if val.nil? || val.null?

        case op
        when "=", "=="
          val.to_string == compare.to_string
        when "!=", "<>"
          val.to_string != compare.to_string
        when "<"
          (NumericVerbs.to_double(val) || 0) < (NumericVerbs.to_double(compare) || 0)
        when "<="
          (NumericVerbs.to_double(val) || 0) <= (NumericVerbs.to_double(compare) || 0)
        when ">"
          (NumericVerbs.to_double(val) || 0) > (NumericVerbs.to_double(compare) || 0)
        when ">="
          (NumericVerbs.to_double(val) || 0) >= (NumericVerbs.to_double(compare) || 0)
        when "contains"
          val.to_string.include?(compare.to_string)
        when "startsWith"
          val.to_string.start_with?(compare.to_string)
        when "endsWith"
          val.to_string.end_with?(compare.to_string)
        else
          val.truthy?
        end
      end
      dv.of_array(filtered)
    elsif args.length >= 2
      # filter(array, fieldName) — truthy on field
      field = args[1]&.to_string || ""
      filtered = items.select do |item|
        val = item.object? ? item.get(field) : item
        val&.truthy? || false
      end
      dv.of_array(filtered)
    else
      # filter(array) — truthy items only
      dv.of_array(items.select(&:truthy?))
    end
  }

  registry["flatten"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    result = []
    items.each do |item|
      if item.array?
        result.concat(item.value)
      else
        result << item
      end
    end
    dv.of_array(result)
  }

  registry["distinct"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    seen = {}
    result = items.select do |item|
      key = item.to_string
      if seen.key?(key)
        false
      else
        seen[key] = true
        true
      end
    end
    dv.of_array(result)
  }
  registry["unique"] = registry["distinct"]

  registry["sort"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    sorted = items.sort { |a, b| CollectionVerbs.compare_dyn_values(a, b) }
    dv.of_array(sorted)
  }

  registry["sortDesc"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    sorted = items.sort { |a, b| CollectionVerbs.compare_dyn_values(b, a) }
    dv.of_array(sorted)
  }

  registry["sortBy"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    field = args[1]&.to_string || ""
    sorted = items.sort do |a, b|
      av = a.object? ? a.get(field) : dv.of_null
      bv = b.object? ? b.get(field) : dv.of_null
      CollectionVerbs.compare_dyn_values(av || dv.of_null, bv || dv.of_null)
    end
    dv.of_array(sorted)
  }

  registry["map"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    field = args[1]&.to_string || ""
    result = items.map do |item|
      if item.object?
        item.get(field) || dv.of_null
      else
        item
      end
    end
    dv.of_array(result)
  }

  registry["pluck"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    field = args[1]&.to_string || ""
    result = items.map do |item|
      if item.object?
        item.get(field) || dv.of_null
      else
        dv.of_null
      end
    end
    dv.of_array(result)
  }

  registry["indexOf"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    search = args[1]
    idx = items.index { |item| CollectionVerbs.values_equal?(item, search) }
    dv.of_integer(idx || -1)
  }

  registry["at"] = ->(args, _ctx) {
    arr = args[0]
    return dv.of_null unless arr&.array?
    idx = NumericVerbs.to_double(args[1])&.to_i || 0
    items = arr.value
    idx = items.length + idx if idx < 0
    (idx >= 0 && idx < items.length) ? items[idx] : dv.of_null
  }

  registry["slice"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    start_idx = NumericVerbs.to_double(args[1])&.to_i || 0
    end_idx = args[2] ? (NumericVerbs.to_double(args[2])&.to_i || items.length) : items.length
    start_idx = items.length + start_idx if start_idx < 0
    end_idx = items.length + end_idx if end_idx < 0
    start_idx = [[start_idx, 0].max, items.length].min
    end_idx = [[end_idx, 0].max, items.length].min
    dv.of_array(start_idx < end_idx ? items[start_idx...end_idx] : [])
  }

  registry["reverse"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    dv.of_array(items.reverse)
  }

  registry["every"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    return dv.of_bool(true) if items.empty?
    if args.length >= 2
      field = args[1]&.to_string || ""
      dv.of_bool(items.all? { |item| (item.object? ? item.get(field) : item)&.truthy? || false })
    else
      dv.of_bool(items.all?(&:truthy?))
    end
  }

  registry["some"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    return dv.of_bool(false) if items.empty?
    if args.length >= 2
      field = args[1]&.to_string || ""
      dv.of_bool(items.any? { |item| (item.object? ? item.get(field) : item)&.truthy? || false })
    else
      dv.of_bool(items.any?(&:truthy?))
    end
  }

  registry["find"] = ->(args, _ctx) {
    return dv.of_null if args.length < 4

    items = CollectionVerbs.extract_items(args[0])
    field = args[1]&.to_string || ""
    op = args[2]&.to_string || "="
    compare = args[3]
    found = items.find { |item| CollectionVerbs.matches_condition?(item, field, op, compare) }
    found || dv.of_null
  }

  registry["findIndex"] = ->(args, _ctx) {
    return dv.of_integer(-1) if args.length < 4

    items = CollectionVerbs.extract_items(args[0])
    field = args[1]&.to_string || ""
    op = args[2]&.to_string || "="
    compare = args[3]
    idx = items.index { |item| CollectionVerbs.matches_condition?(item, field, op, compare) }
    dv.of_integer(idx || -1)
  }

  registry["includes"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    search = args[1]
    dv.of_bool(items.any? { |item| CollectionVerbs.values_equal?(item, search) })
  }

  registry["concatArrays"] = ->(args, _ctx) {
    result = []
    args.each do |a|
      if a&.array?
        result.concat(a.value)
      elsif !a.nil? && !a.null?
        result << a
      end
    end
    dv.of_array(result)
  }

  registry["zip"] = ->(args, _ctx) {
    arrays = args.map { |a| CollectionVerbs.extract_items(a) }
    return dv.of_array([]) if arrays.empty?
    max_len = arrays.map(&:length).max || 0
    result = (0...max_len).map do |i|
      pair = arrays.map { |arr| i < arr.length ? arr[i] : dv.of_null }
      dv.of_array(pair)
    end
    dv.of_array(result)
  }

  registry["groupBy"] = ->(args, _ctx) {
    return dv.of_null if args.length < 2

    items = CollectionVerbs.extract_items(args[0])
    field = args[1]&.to_string || ""
    groups = {}
    order = []
    items.each do |item|
      key = if item.object?
              (item.get(field) || dv.of_null).to_string
            else
              item.to_string
            end
      unless groups.key?(key)
        groups[key] = []
        order << key
      end
      groups[key] << item
    end
    result = order.map do |key|
      dv.of_object({ "key" => dv.of_string(key), "items" => dv.of_array(groups[key]) })
    end
    dv.of_array(result)
  }

  registry["partition"] = ->(args, _ctx) {
    return dv.of_null if args.length < 4

    items = CollectionVerbs.extract_items(args[0])
    field = args[1]&.to_string || ""
    op = args[2]&.to_string || "="
    compare = args[3]
    pass_items, fail_items = items.partition { |item| CollectionVerbs.matches_condition?(item, field, op, compare) }
    dv.of_array([dv.of_array(pass_items), dv.of_array(fail_items)])
  }

  registry["take"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    n = NumericVerbs.to_double(args[1])&.to_i || 0
    dv.of_array(items.first([n, 0].max))
  }
  registry["limit"] = registry["take"]

  registry["drop"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    n = NumericVerbs.to_double(args[1])&.to_i || 0
    dv.of_array(items.drop([n, 0].max))
  }

  registry["chunk"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    size = NumericVerbs.to_double(args[1])&.to_i || 1
    size = 1 if size < 1
    chunks = items.each_slice(size).map { |c| dv.of_array(c) }
    dv.of_array(chunks)
  }

  registry["range"] = ->(args, _ctx) {
    if args.length == 1
      end_val = NumericVerbs.to_double(args[0])&.to_i || 0
      start_val = 0
      step = 1
    elsif args.length == 2
      start_val = NumericVerbs.to_double(args[0])&.to_i || 0
      end_val = NumericVerbs.to_double(args[1])&.to_i || 0
      step = start_val <= end_val ? 1 : -1
    else
      start_val = NumericVerbs.to_double(args[0])&.to_i || 0
      end_val = NumericVerbs.to_double(args[1])&.to_i || 0
      step = NumericVerbs.to_double(args[2])&.to_i || 1
      step = 1 if step == 0
    end

    result = []
    max_items = 10_000
    if step > 0
      i = start_val
      while i < end_val && result.length < max_items
        result << dv.of_integer(i)
        i += step
      end
    else
      i = start_val
      while i > end_val && result.length < max_items
        result << dv.of_integer(i)
        i += step
      end
    end
    dv.of_array(result)
  }

  registry["compact"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    dv.of_array(items.reject { |item| item.null? || (item.string? && item.value.empty?) })
  }

  registry["rowNumber"] = ->(args, _ctx) {
    return dv.of_null if args.empty?

    items = CollectionVerbs.extract_items(args[0])
    result = items.each_with_index.map do |item, i|
      num = dv.of_integer(i + 1)
      if item.object?
        dv.of_object({ "_rowNum" => num }.merge(item.value))
      else
        dv.of_object({ "_rowNum" => num, "value" => item })
      end
    end
    dv.of_array(result)
  }

  registry["sample"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    count = NumericVerbs.to_double(args[1])&.to_i || 1
    seed_arg = args[2]
    if seed_arg
      # Deterministic sampling using Mulberry32 Fisher-Yates
      seed_val = if seed_arg.type == :string
                   NumericVerbs.string_to_seed(seed_arg.to_string)
                 else
                   (NumericVerbs.to_double(seed_arg)&.to_i || 0)
                 end
      rng = Mulberry32.new(seed_val)
      shuffled = items.dup
      (shuffled.length - 1).downto(1) do |i|
        j = rng.next_int(i + 1)
        shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
      end
      dv.of_array(shuffled.first([count, shuffled.length].min))
    else
      rng = Random.new
      shuffled = items.dup
      (shuffled.length - 1).downto(1) do |i|
        j = rng.rand(i + 1)
        shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
      end
      dv.of_array(shuffled.first([count, shuffled.length].min))
    end
  }

  registry["dedupe"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    field = args[1]&.to_string
    result = []
    last_key = nil
    items.each do |item|
      key = if field && item.object?
               (item.get(field) || dv.of_null).to_string
             else
               item.to_string
             end
      if key != last_key
        result << item
        last_key = key
      end
    end
    dv.of_array(result)
  }

  registry["cumsum"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    sum = 0.0
    result = items.map do |item|
      n = NumericVerbs.to_double(item)
      if n.nil?
        dv.of_null
      else
        sum += n
        NumericVerbs.numeric_result(sum)
      end
    end
    dv.of_array(result)
  }

  registry["cumprod"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    prod = 1.0
    result = items.map do |item|
      n = NumericVerbs.to_double(item)
      if n.nil?
        dv.of_null
      else
        prod *= n
        NumericVerbs.numeric_result(prod)
      end
    end
    dv.of_array(result)
  }

  registry["diff"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    lag = args[1] ? (NumericVerbs.to_double(args[1])&.to_i || 1) : 1
    result = items.each_with_index.map do |item, i|
      if i < lag
        dv.of_null
      else
        curr = NumericVerbs.to_double(item)
        prev = NumericVerbs.to_double(items[i - lag])
        if curr.nil? || prev.nil?
          dv.of_null
        else
          NumericVerbs.numeric_result(curr - prev)
        end
      end
    end
    dv.of_array(result)
  }

  registry["pctChange"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    lag = args[1] ? (NumericVerbs.to_double(args[1])&.to_i || 1) : 1
    result = items.each_with_index.map do |item, i|
      if i < lag
        dv.of_null
      else
        curr = NumericVerbs.to_double(item)
        prev = NumericVerbs.to_double(items[i - lag])
        if curr.nil? || prev.nil? || prev == 0.0
          dv.of_null
        else
          dv.of_float((curr - prev) / prev)
        end
      end
    end
    dv.of_array(result)
  }

  registry["shift"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    n = NumericVerbs.to_double(args[1])&.to_i || 0
    fill = args[2] || dv.of_null
    result = Array.new(items.length, fill)
    items.each_with_index do |item, i|
      new_idx = i + n
      result[new_idx] = item if new_idx >= 0 && new_idx < items.length
    end
    dv.of_array(result)
  }

  registry["lag"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    n = NumericVerbs.to_double(args[1])&.to_i || 1
    fill = args[2] || dv.of_null
    result = items.each_with_index.map do |_item, i|
      prev_idx = i - n
      prev_idx >= 0 ? items[prev_idx] : fill
    end
    dv.of_array(result)
  }

  registry["lead"] = ->(args, _ctx) {
    items = CollectionVerbs.extract_items(args[0])
    n = NumericVerbs.to_double(args[1])&.to_i || 1
    fill = args[2] || dv.of_null
    result = items.each_with_index.map do |_item, i|
      next_idx = i + n
      next_idx < items.length ? items[next_idx] : fill
    end
    dv.of_array(result)
  }

  registry["rank"] = ->(args, _ctx) {
    return dv.of_null if args.empty?

    items = CollectionVerbs.extract_items(args[0])
    field = args.length > 1 ? args[1]&.to_string : nil
    field = nil if field&.empty?
    direction = (args.length > 2 ? args[2]&.to_string : "desc").to_s.downcase

    comparable = ->(item) do
      raw = field && item.object? ? item.get(field) : item
      num = NumericVerbs.to_double(raw)
      num.nil? ? (raw.nil? ? "" : raw.to_string) : num
    end

    indexed = items.each_index.map { |i| [i, comparable.call(items[i])] }
    mult = direction == "asc" ? 1 : -1
    sorted = indexed.sort do |a, b|
      av = a[1]
      bv = b[1]
      if av.is_a?(Numeric) && bv.is_a?(Numeric)
        (av <=> bv) * mult
      else
        (av.to_s <=> bv.to_s) * mult
      end
    end

    ranks = {}
    current_rank = 1
    sorted.each_with_index do |(orig_idx, val), i|
      current_rank = i + 1 if i.positive? && val != sorted[i - 1][1]
      ranks[orig_idx] = current_rank
    end

    result = items.each_with_index.map do |item, i|
      rank_val = dv.of_integer(ranks[i] || (i + 1))
      if item.object?
        dv.of_object({ "_rank" => rank_val }.merge(item.value))
      else
        dv.of_object({ "_rank" => rank_val, "value" => item })
      end
    end
    dv.of_array(result)
  }

  registry["fillMissing"] = ->(args, _ctx) {
    return dv.of_null if args.empty?

    items = CollectionVerbs.extract_items(args[0])
    fill_val = args.length >= 2 ? args[1] : dv.of_null
    strategy = (args.length >= 3 ? args[2]&.to_string : "value").to_s.downcase
    nullish = ->(item) { item.nil? || item.null? }

    case strategy
    when "forward"
      last_val = fill_val
      result = items.map do |item|
        if nullish.call(item)
          last_val
        else
          last_val = item
          item
        end
      end
    when "backward"
      last_val = fill_val
      result = items.reverse.map do |item|
        if nullish.call(item)
          last_val
        else
          last_val = item
          item
        end
      end.reverse
    when "mean"
      nums = items.reject { |i| nullish.call(i) }.filter_map { |i| NumericVerbs.to_double(i) }
      mean = nums.empty? ? 0.0 : nums.inject(0.0) { |s, v| s + v } / nums.length
      mean_val = dv.of_float(mean)
      result = items.map { |item| nullish.call(item) ? mean_val : item }
    else
      result = items.map { |item| nullish.call(item) ? fill_val : item }
    end
    dv.of_array(result)
  }
  registry["reduce"] = ->(args, _ctx) {
    return dv.of_null if args.length < 3
    arr = args[0]
    return dv.of_null unless arr&.array?
    verb_name = args[1]&.to_string
    return dv.of_null if verb_name.nil?
    accumulator = args[2]
    verb_fn = registry[verb_name]
    return dv.of_null unless verb_fn
    arr.as_array.each do |item|
      accumulator = verb_fn.call([accumulator, item], _ctx)
    end
    accumulator
  }

  registry["pivot"] = ->(args, _ctx) {
    return dv.of_null if args.length < 3
    arr = args[0]
    return dv.of_null unless arr&.array?
    key_field = args[1]&.to_string
    value_field = args[2]&.to_string
    return dv.of_null if key_field.nil? || value_field.nil?
    result = {}
    arr.as_array.each do |item|
      next unless item.object?
      k = item.get(key_field)
      next if k.nil? || k.null?
      key_str = k.to_string
      v = item.get(value_field) || dv.of_null
      result[key_str] = v
    end
    dv.of_object(result)
  }

  registry["unpivot"] = ->(args, _ctx) {
    return dv.of_null if args.length < 3
    obj = args[0]
    return dv.of_null unless obj&.object?
    key_name = args[1]&.to_string
    value_name = args[2]&.to_string
    return dv.of_null if key_name.nil? || value_name.nil?
    result = []
    obj.as_object.each do |k, v|
      result << dv.of_object({
        key_name => dv.of_string(k.to_s),
        value_name => v
      })
    end
    dv.of_array(result)
  }
end

.values_equal?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/odin/transform/verbs/collection_verbs.rb', line 33

def values_equal?(a, b)
  return true if a.nil? && b.nil?
  return false if a.nil? || b.nil?
  return true if a.null? && b.null?
  a.to_string == b.to_string
end