Team Ai
Apppublic

LiquidAI/LFM2-WebGPU

sourceHugging Faceapache-2.0updated 3mo agoView on Hugging Face
94likes
math_eval.js55 linesDownload Raw Back to tools
1/**2 * Evaluate a math expression.3 * @param {string} expression - The math expression (e.g., "2 + 2 * (3 - 1)").4 * @returns {number} The result of the expression.5 */6export function math_eval(expression) {7  // Only allow numbers, spaces, and math symbols: + - * / % ( ) .8  if (!/^[\d\s+\-*/%.()]+$/.test(expression)) {9    throw new Error("Invalid characters in expression.");10  }11  return Function('"use strict";return (' + expression + ")")();12}13 14export default (input, output) =>15  React.createElement(16    "div",17    { className: "bg-emerald-50 border border-emerald-200 rounded-lg p-4" },18    React.createElement(19      "div",20      { className: "flex items-center mb-2" },21      React.createElement(22        "div",23        {24          className:25            "w-8 h-8 bg-emerald-100 rounded-full flex items-center justify-center mr-3",26        },27        "🧮",28      ),29      React.createElement(30        "h3",31        { className: "text-emerald-900 font-semibold" },32        "Math Evaluation",33      ),34    ),35    React.createElement(36      "div",37      { className: "text-center" },38      React.createElement(39        "div",40        { className: "text-lg font-mono text-emerald-700 mb-1" },41        input.expression || "Unknown expression",42      ),43      React.createElement(44        "div",45        { className: "text-2xl font-bold text-emerald-600 mb-1" },46        `= ${output}`,47      ),48      React.createElement(49        "p",50        { className: "text-emerald-500 text-xs" },51        "Calculation result",52      ),53    ),54  );55