@@ -40,7 +40,11 @@ export class CalculatorNode extends ExecutableNode {
4040 async execute ( context : NodeContext ) : Promise < NodeExecution > {
4141 const { expression } = context . inputs ;
4242
43- if ( ! expression || typeof expression !== "string" || expression . trim ( ) === "" ) {
43+ if (
44+ ! expression ||
45+ typeof expression !== "string" ||
46+ expression . trim ( ) === ""
47+ ) {
4448 return this . createErrorResult ( "Missing or empty expression." ) ;
4549 }
4650
@@ -49,19 +53,22 @@ export class CalculatorNode extends ExecutableNode {
4953 try {
5054 // Validate expression for safety - only allow mathematical operations
5155 const allowedPattern = / ^ [ 0 - 9 + \- * / ( ) . , \t \n \r \s ^ % & | < > ~ ] + $ / ;
52- const mathFunctions = / ( s i n | c o s | t a n | a s i n | a c o s | a t a n | s i n h | c o s h | t a n h | a s i n h | a c o s h | a t a n h | s q r t | c b r t | p o w | e x p | l o g | l o g 1 0 | a b s | f l o o r | c e i l | r o u n d | m i n | m a x | r a n d o m | P I | E | s i g n | t r u n c | h y p o t | a t a n 2 ) / g;
53-
56+ const mathFunctions =
57+ / ( s i n | c o s | t a n | a s i n | a c o s | a t a n | s i n h | c o s h | t a n h | a s i n h | a c o s h | a t a n h | s q r t | c b r t | p o w | e x p | l o g | l o g 1 0 | a b s | f l o o r | c e i l | r o u n d | m i n | m a x | r a n d o m | P I | E | s i g n | t r u n c | h y p o t | a t a n 2 ) / g;
58+
5459 // Check if expression contains only allowed characters and math functions
55- const cleanExpression = expression . replace ( mathFunctions , '' ) ;
60+ const cleanExpression = expression . replace ( mathFunctions , "" ) ;
5661 if ( cleanExpression && ! allowedPattern . test ( cleanExpression ) ) {
57- return this . createErrorResult ( "Expression contains invalid characters. Only numbers, operators (+, -, *, /, ^, %, &, |, <, >, ~), parentheses, and math functions are allowed." ) ;
62+ return this . createErrorResult (
63+ "Expression contains invalid characters. Only numbers, operators (+, -, *, /, ^, %, &, |, <, >, ~), parentheses, and math functions are allowed."
64+ ) ;
5865 }
5966
6067 const QuickJSModule = await getQuickJSWASMModule ( ) ;
6168 vm = QuickJSModule . newContext ( ) ;
6269
6370 // Replace ^ with ** for JavaScript exponentiation
64- const jsExpression = expression . replace ( / \^ / g, '**' ) ;
71+ const jsExpression = expression . replace ( / \^ / g, "**" ) ;
6572
6673 // Create a bootstrap script that sets up the Math environment
6774 const bootstrapScript = `
@@ -159,8 +166,10 @@ export class CalculatorNode extends ExecutableNode {
159166 evalResult . value . dispose ( ) ;
160167
161168 // Validate the result
162- if ( typeof result !== 'number' || isNaN ( result ) || ! isFinite ( result ) ) {
163- return this . createErrorResult ( "Expression evaluation resulted in an invalid number (NaN or Infinity)." ) ;
169+ if ( typeof result !== "number" || isNaN ( result ) || ! isFinite ( result ) ) {
170+ return this . createErrorResult (
171+ "Expression evaluation resulted in an invalid number (NaN or Infinity)."
172+ ) ;
164173 }
165174
166175 return this . createSuccessResult ( { result } ) ;
@@ -174,5 +183,4 @@ export class CalculatorNode extends ExecutableNode {
174183 }
175184 }
176185 }
177-
178- }
186+ }
0 commit comments