(formula: string, x: number)
| 1889 | |
| 1890 | // Formula evaluation function |
| 1891 | function 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 | |
| 1922 | async function executeColumnSelectionDropdownNode( |
| 1923 | nodeId: string, |
no outgoing calls
no test coverage detected