operateInternal method Null safety

  1. @internal
Object operateInternal(
  1. CalculationOperator operator,
  2. Object left,
  3. Object right,
  4. {required bool inMinMax}
)

Like operate, but with the internal-only inMinMax parameter.

If inMinMax is true, this allows unitless numbers to be added and subtracted with numbers with units, for backwards-compatibility with the old global min() and max() functions.

Implementation

@internal
static Object operateInternal(
    CalculationOperator operator, Object left, Object right,
    {required bool inMinMax}) {
  left = _simplify(left);
  right = _simplify(right);

  if (operator == CalculationOperator.plus ||
      operator == CalculationOperator.minus) {
    if (left is SassNumber &&
        right is SassNumber &&
        (inMinMax
            ? left.isComparableTo(right)
            : left.hasCompatibleUnits(right))) {
      return operator == CalculationOperator.plus
          ? left.plus(right)
          : left.minus(right);
    }

    _verifyCompatibleNumbers([left, right]);

    if (right is SassNumber && fuzzyLessThan(right.value, 0)) {
      right = right.times(SassNumber(-1));
      operator = operator == CalculationOperator.plus
          ? CalculationOperator.minus
          : CalculationOperator.plus;
    }

    return CalculationOperation._(operator, left, right);
  } else if (left is SassNumber && right is SassNumber) {
    return operator == CalculationOperator.times
        ? left.times(right)
        : left.dividedBy(right);
  } else {
    return CalculationOperation._(operator, left, right);
  }
}