Module: Odin::Transform::Verbs::NumericVerbs

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

Constant Summary collapse

UNIT_FAMILIES =
{
  "mass" => { "g" => 1.0, "kg" => 1000.0, "mg" => 0.001, "lb" => 453.592, "oz" => 28.3495, "ton" => 907185.0, "tonne" => 1000000.0 },
  "length" => { "m" => 1.0, "km" => 1000.0, "cm" => 0.01, "mm" => 0.001, "mi" => 1609.344, "yd" => 0.9144, "ft" => 0.3048, "in" => 0.0254 },
  "volume" => { "l" => 1.0, "ml" => 0.001, "gal" => 3.78541, "qt" => 0.946353, "pt" => 0.473176, "cup" => 0.236588, "floz" => 0.0295735 },
  "speed" => { "m/s" => 1.0, "km/h" => 0.277778, "mph" => 0.44704, "knot" => 0.514444, "ft/s" => 0.3048 },
  "area" => { "m2" => 1.0, "km2" => 1000000.0, "cm2" => 0.0001, "ha" => 10000.0, "acre" => 4046.86, "ft2" => 0.092903, "in2" => 0.00064516, "mi2" => 2589988.0 },
  "data" => { "B" => 1.0, "KB" => 1024.0, "MB" => 1048576.0, "GB" => 1073741824.0, "TB" => 1099511627776.0 },
  "time" => { "s" => 1.0, "ms" => 0.001, "min" => 60.0, "h" => 3600.0, "d" => 86400.0, "wk" => 604800.0 }
}.freeze

Class Method Summary collapse

Class Method Details

.away_from_zero_round(value, places) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/odin/transform/verbs/numeric_verbs.rb', line 37

def away_from_zero_round(value, places)
  return value if places < 0
  # Use BigDecimal for precision to avoid floating-point issues like 1.005
  bd = BigDecimal(value.to_s)
  factor = BigDecimal("10") ** places
  scaled = bd * factor
  if value >= 0
    (scaled + BigDecimal("0.5")).floor.to_f / factor.to_f
  else
    (scaled - BigDecimal("0.5")).ceil.to_f / factor.to_f
  end
end

.find_unit_family(unit) ⇒ Object



84
85
86
87
88
89
# File 'lib/odin/transform/verbs/numeric_verbs.rb', line 84

def find_unit_family(unit)
  UNIT_FAMILIES.each do |family_name, units|
    return [family_name, units[unit]] if units.key?(unit)
  end
  nil
end

.numeric_result(val) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/odin/transform/verbs/numeric_verbs.rb', line 66

def numeric_result(val)
  if val == val.floor && val.abs < 2**53
    Types::DynValue.of_integer(val.to_i)
  else
    Types::DynValue.of_float(val)
  end
end

.register(registry) ⇒ Object



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
# File 'lib/odin/transform/verbs/numeric_verbs.rb', line 91

def register(registry)
  dv = Types::DynValue

  registry["formatNumber"] = ->(args, _ctx) {
    raw = NumericVerbs.to_double(args[0])
    return dv.of_null if raw.nil?
    places = NumericVerbs.to_double(args[1])&.to_i || 0
    rounded = NumericVerbs.away_from_zero_round(raw, places)
    dv.of_string(format("%.#{places}f", rounded))
  }

  registry["formatInteger"] = ->(args, _ctx) {
    raw = NumericVerbs.to_double(args[0])
    return dv.of_null if raw.nil?
    dv.of_string(raw.floor.to_s)
  }

  registry["formatCurrency"] = ->(args, _ctx) {
    raw = NumericVerbs.to_double(args[0])
    return dv.of_null if raw.nil?
    rounded = NumericVerbs.away_from_zero_round(raw, 2)
    dv.of_string(format("%.2f", rounded))
  }

  registry["formatPercent"] = ->(args, _ctx) {
    raw = NumericVerbs.to_double(args[0])
    return dv.of_null if raw.nil?
    places = NumericVerbs.to_double(args[1])&.to_i || 0
    pct = raw * 100.0
    rounded = NumericVerbs.away_from_zero_round(pct, places)
    dv.of_string("#{format("%.#{places}f", rounded)}%")
  }

  registry["add"] = ->(args, _ctx) {
    a, b = args
    av = NumericVerbs.to_double(a)
    bv = NumericVerbs.to_double(b)
    return dv.of_null if av.nil? || bv.nil?
    result = av + bv
    NumericVerbs.numeric_result(result)
  }

  registry["subtract"] = ->(args, _ctx) {
    a, b = args
    av = NumericVerbs.to_double(a)
    bv = NumericVerbs.to_double(b)
    return dv.of_null if av.nil? || bv.nil?
    result = av - bv
    NumericVerbs.numeric_result(result)
  }

  registry["multiply"] = ->(args, _ctx) {
    a, b = args
    av = NumericVerbs.to_double(a)
    bv = NumericVerbs.to_double(b)
    return dv.of_null if av.nil? || bv.nil?
    result = av * bv
    NumericVerbs.numeric_result(result)
  }

  registry["divide"] = ->(args, _ctx) {
    a, b = args
    av = NumericVerbs.to_double(a)
    bv = NumericVerbs.to_double(b)
    return dv.of_null if av.nil? || bv.nil? || bv == 0.0
    dv.of_float(av / bv)
  }

  registry["mod"] = ->(args, _ctx) {
    a, b = args
    av = NumericVerbs.to_double(a)
    bv = NumericVerbs.to_double(b)
    return dv.of_null if av.nil? || bv.nil? || bv == 0.0
    result = av.to_i % bv.to_i
    dv.of_integer(result)
  }

  registry["abs"] = ->(args, _ctx) {
    v = args[0]
    n = NumericVerbs.to_double(v)
    return dv.of_null if n.nil?
    v.integer? ? dv.of_integer(n.abs.to_i) : dv.of_float(n.abs)
  }

  registry["floor"] = ->(args, _ctx) {
    n = NumericVerbs.to_double(args[0])
    return dv.of_null if n.nil?
    dv.of_integer(n.floor)
  }

  registry["ceil"] = ->(args, _ctx) {
    n = NumericVerbs.to_double(args[0])
    return dv.of_null if n.nil?
    dv.of_integer(n.ceil)
  }

  registry["round"] = ->(args, _ctx) {
    n = NumericVerbs.to_double(args[0])
    return dv.of_null if n.nil?
    places = NumericVerbs.to_double(args[1])&.to_i || 0
    rounded = NumericVerbs.away_from_zero_round(n, places)
    if rounded == rounded.to_i
      dv.of_integer(rounded.to_i)
    else
      dv.of_float(rounded)
    end
  }

  registry["negate"] = ->(args, _ctx) {
    n = NumericVerbs.to_double(args[0])
    return dv.of_null if n.nil?
    NumericVerbs.numeric_result(-n)
  }

  registry["sign"] = ->(args, _ctx) {
    n = NumericVerbs.to_double(args[0])
    return dv.of_null if n.nil?
    dv.of_integer(n > 0 ? 1 : (n < 0 ? -1 : 0))
  }

  registry["trunc"] = ->(args, _ctx) {
    n = NumericVerbs.to_double(args[0])
    return dv.of_null if n.nil?
    dv.of_integer(n.truncate)
  }

  registry["random"] = ->(args, _ctx) {
    if args.length == 1 && args[0]&.type == :string
      # Seeded float mode: random("seed") -> deterministic [0,1)
      seed = NumericVerbs.string_to_seed(args[0].to_string)
      rng = Mulberry32.new(seed)
      dv.of_float(rng.next_float)
    elsif args.length == 3 && args[2]&.type == :string
      # Seeded integer range: random(min, max, "seed") -> deterministic int in [min,max]
      min_v = (NumericVerbs.to_double(args[0]) || 0.0).to_i
      max_v = (NumericVerbs.to_double(args[1]) || 1.0).to_i
      seed = NumericVerbs.string_to_seed(args[2].to_string)
      rng = Mulberry32.new(seed)
      dv.of_integer(min_v + rng.next_int(max_v - min_v + 1))
    elsif args.length >= 2
      min_v = NumericVerbs.to_double(args[0]) || 0.0
      max_v = NumericVerbs.to_double(args[1]) || 1.0
      places = NumericVerbs.to_double(args[2])&.to_i || 2
      val = min_v + rand * (max_v - min_v)
      factor = 10.0**places
      val = (val * factor).round / factor
      dv.of_float(val)
    else
      dv.of_float(rand)
    end
  }

  registry["minOf"] = ->(args, _ctx) {
    # Flatten any array args
    items = []
    args.each do |a|
      if a&.array?
        a.value.each { |item| items << item }
      else
        items << a
      end
    end
    nums = items.filter_map { |a| NumericVerbs.to_double(a) }
    return dv.of_null if nums.empty?
    result = nums.min
    NumericVerbs.numeric_result(result)
  }

  registry["maxOf"] = ->(args, _ctx) {
    items = []
    args.each do |a|
      if a&.array?
        a.value.each { |item| items << item }
      else
        items << a
      end
    end
    nums = items.filter_map { |a| NumericVerbs.to_double(a) }
    return dv.of_null if nums.empty?
    result = nums.max
    NumericVerbs.numeric_result(result)
  }

  registry["parseInt"] = ->(args, _ctx) {
    s = args[0]&.to_string || ""
    radix = NumericVerbs.to_double(args[1])&.to_i || 10
    begin
      dv.of_integer(s.to_i(radix))
    rescue ArgumentError
      dv.of_null
    end
  }

  registry["safeDivide"] = ->(args, _ctx) {
    a, b, default_val = args
    av = NumericVerbs.to_double(a)
    bv = NumericVerbs.to_double(b)
    if bv.nil? || bv == 0.0
      default_val || dv.of_null
    else
      av ||= 0.0
      dv.of_float(av / bv)
    end
  }

  registry["formatLocaleNumber"] = ->(args, _ctx) {
    raw = NumericVerbs.to_double(args[0])
    return dv.of_null if raw.nil?
    places = NumericVerbs.to_double(args[1])&.to_i || 2
    rounded = NumericVerbs.away_from_zero_round(raw, places)
    formatted = format("%.#{places}f", rounded)
    # Add comma thousands separator
    int_part, dec_part = formatted.split(".")
    int_part = int_part.gsub(/(\d)(?=(\d{3})+(?!\d))/, '\\1,')
    result = dec_part ? "#{int_part}.#{dec_part}" : int_part
    dv.of_string(result)
  }

  registry["log"] = ->(args, _ctx) {
    v = NumericVerbs.to_double(args[0])
    base = NumericVerbs.to_double(args[1])
    return dv.of_null if v.nil? || base.nil? || v <= 0 || base <= 0 || base == 1
    result = Math.log(v) / Math.log(base)
    return dv.of_null if result.nan? || result.infinite?
    NumericVerbs.numeric_result(result)
  }

  registry["ln"] = ->(args, _ctx) {
    v = NumericVerbs.to_double(args[0])
    return dv.of_null if v.nil? || v <= 0
    result = Math.log(v)
    return dv.of_null if result.nan? || result.infinite?
    NumericVerbs.numeric_result(result)
  }

  registry["log10"] = ->(args, _ctx) {
    v = NumericVerbs.to_double(args[0])
    return dv.of_null if v.nil? || v <= 0
    result = Math.log10(v)
    return dv.of_null if result.nan? || result.infinite?
    NumericVerbs.numeric_result(result)
  }

  registry["exp"] = ->(args, _ctx) {
    v = NumericVerbs.to_double(args[0])
    return dv.of_null if v.nil?
    result = Math.exp(v)
    return dv.of_null if result.nan? || result.infinite?
    dv.of_float(result)
  }

  registry["pow"] = ->(args, _ctx) {
    base = NumericVerbs.to_double(args[0])
    exp = NumericVerbs.to_double(args[1])
    return dv.of_null if base.nil? || exp.nil?
    result = base**exp
    return dv.of_null if result.nan? || result.infinite?
    NumericVerbs.numeric_result(result)
  }

  registry["sqrt"] = ->(args, _ctx) {
    v = NumericVerbs.to_double(args[0])
    return dv.of_null if v.nil? || v < 0
    result = Math.sqrt(v)
    return dv.of_null if result.nan? || result.infinite?
    NumericVerbs.numeric_result(result)
  }

  registry["clamp"] = ->(args, _ctx) {
    v = NumericVerbs.to_double(args[0])
    min_v = NumericVerbs.to_double(args[1])
    max_v = NumericVerbs.to_double(args[2])
    return dv.of_null if v.nil? || min_v.nil? || max_v.nil?
    result = [[v, min_v].max, max_v].min
    NumericVerbs.numeric_result(result)
  }

  registry["isFinite"] = ->(args, _ctx) {
    v = args[0]
    return dv.of_bool(false) if v.nil? || v.null?
    n = NumericVerbs.to_double(v)
    dv.of_bool(!n.nil? && !n.nan? && !n.infinite?)
  }

  registry["isNaN"] = ->(args, _ctx) {
    v = args[0]
    return dv.of_bool(true) if v.nil? || v.null?
    n = NumericVerbs.to_double(v)
    dv.of_bool(n.nil? || n.nan?)
  }

  registry["convertUnit"] = ->(args, _ctx) {
    return dv.of_null if args.length < 3
    value = NumericVerbs.to_double(args[0])
    return dv.of_null if value.nil?
    from_unit = args[1]&.to_string
    to_unit = args[2]&.to_string
    return dv.of_null if from_unit.nil? || to_unit.nil?

    temp_units = %w[C F K]
    if temp_units.include?(from_unit) && temp_units.include?(to_unit)
      # Convert to Celsius first
      celsius = case from_unit
                when "F" then (value - 32) * 5.0 / 9.0
                when "K" then value - 273.15
                else value
                end
      result = case to_unit
               when "F" then celsius * 9.0 / 5.0 + 32
               when "K" then celsius + 273.15
               else celsius
               end
      result = (result * 1_000_000).round / 1_000_000.0
      next NumericVerbs.numeric_result(result)
    end

    # One is temp, other is not → incompatible
    if temp_units.include?(from_unit) || temp_units.include?(to_unit)
      next dv.of_null
    end

    from_info = NumericVerbs.find_unit_family(from_unit)
    to_info = NumericVerbs.find_unit_family(to_unit)
    if from_info.nil? || to_info.nil?
      next dv.of_null
    end

    from_family, from_factor = from_info
    to_family, to_factor = to_info
    if from_family != to_family
      next dv.of_null
    end

    result = value * from_factor / to_factor
    result = (result * 1_000_000).round / 1_000_000.0
    NumericVerbs.numeric_result(result)
  }
end

.string_to_seed(s) ⇒ Object



31
32
33
34
35
# File 'lib/odin/transform/verbs/numeric_verbs.rb', line 31

def string_to_seed(s)
  h = 0
  s.each_byte { |b| h = ((h << 5) - h + b) & 0xFFFFFFFF }
  h
end

.to_double(v) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/odin/transform/verbs/numeric_verbs.rb', line 50

def to_double(v)
  return nil if v.nil? || v.null?
  case v.type
  when :integer then v.value.to_f
  when :float, :percent then v.value.to_f
  when :float_raw, :currency_raw then v.value.to_f
  when :currency then v.value.to_f
  when :string
    s = v.value.strip
    return nil if s.empty?
    Float(s) rescue nil
  when :bool then v.value ? 1.0 : 0.0
  else nil
  end
end