evaluateExpression static method

bool evaluateExpression(
  1. String expression
)

Implementation

static bool evaluateExpression(String expression) {
  RegExp regex =
      RegExp(r"(['\`].*?['\`]|\d+)\s*([><!=]=?|==)\s*(['\`].*?['\`]|\d+)");
  final match = regex.firstMatch(expression);

  if (match == null || match.groupCount != 3) {
    return false;
  }

  String leftOperand = match.group(1)!;
  String operator = match.group(2)!;
  String rightOperand = match.group(3)!;

  // Remove quotes if present
  leftOperand = leftOperand.replaceAll("'", "");
  rightOperand = rightOperand.replaceAll("'", "");

  int? leftInt = int.tryParse(leftOperand);
  int? rightInt = int.tryParse(rightOperand);

  if (leftInt != null && rightInt != null) {
    // Both operands are integers
    return evaluateOperation(leftInt, operator, rightInt);
  } else if (leftInt != null) {
    // Only left operand is an integer
    return evaluateOperation(leftInt, operator, rightOperand);
  } else if (rightInt != null) {
    // Only right operand is an integer
    return evaluateOperation(leftOperand, operator, rightInt);
  } else {
    // Neither operand is an integer
    return false;
  }
}