applyArithmetic function

Object? applyArithmetic(
  1. String operator,
  2. Object? a,
  3. Object? b
)

Applies an arithmetic operator to two known values: numbers, or strings for +. Null if they can't be combined.

Implementation

Object? applyArithmetic(String operator, Object? a, Object? b) {
  if (operator == '+' && a is String && b is String) return a + b;
  if (a is! num || b is! num) return null;
  return switch (operator) {
    '+' => a + b,
    '-' => a - b,
    '*' => a * b,
    '/' => b == 0 ? null : a / b,
    '~/' => b == 0 ? null : a ~/ b,
    _ => null,
  };
}