diff options
Diffstat (limited to 'apps/web/src/utils/math.ts')
| -rw-r--r-- | apps/web/src/utils/math.ts | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/apps/web/src/utils/math.ts b/apps/web/src/utils/math.ts index 012ac747..ac82ca17 100644 --- a/apps/web/src/utils/math.ts +++ b/apps/web/src/utils/math.ts @@ -9,3 +9,19 @@ export function clamp({ }): number { return Math.max(min, Math.min(max, value)); } + +export function evaluateMathExpression({ + input, +}: { + input: string; +}): number | null { + const sanitized = input.trim(); + if (!/^[\d.\s+\-*/()]+$/.test(sanitized)) return null; + try { + const result = new Function(`return (${sanitized})`)(); + if (typeof result !== "number" || !Number.isFinite(result)) return null; + return result; + } catch { + return null; + } +} |
