MCPcopy Create free account
hub / github.com/Shazbot/WH3-Mod-Manager / evaluateFormula

Function evaluateFormula

src/nodeExecutor.ts:1891–1920  ·  view source on GitHub ↗
(formula: string, x: number)

Source from the content-addressed store, hash-verified

1889
1890// Formula evaluation function
1891function evaluateFormula(formula: string, x: number): number {
1892 // console.log(`Evaluating formula: "${formula}" with x=${x}`);
1893
1894 // Sanitize the formula - only allow safe mathematical operations
1895 const sanitized = formula
1896 .replace(/\s+/g, "") // Remove whitespace
1897 .replace(/\^/g, "**") // Convert ^ to ** for exponentiation
1898 .replace(/[^x0-9+\-*/().\s]/g, ""); // Remove any unsafe characters
1899
1900 // Replace 'x' with the actual value
1901 const expression = sanitized.replace(/x/g, x.toString());
1902
1903 // Validate that the expression only contains safe characters
1904 if (!/^[0-9+\-*/().\s]+$/.test(expression)) {
1905 throw new Error("Invalid formula: contains unsafe characters");
1906 }
1907
1908 try {
1909 // Use Function constructor for safe evaluation (better than eval)
1910 const result = new Function("return " + expression)();
1911
1912 if (typeof result !== "number" || isNaN(result) || !isFinite(result)) {
1913 throw new Error("Formula evaluation resulted in invalid number");
1914 }
1915
1916 return result;
1917 } catch (error) {
1918 throw new Error(`Formula evaluation failed: ${error instanceof Error ? error.message : "Unknown error"}`);
1919 }
1920}
1921
1922async function executeColumnSelectionDropdownNode(
1923 nodeId: string,

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected