{"library":"simple-eval","title":"Simple JavaScript Expression Evaluator","description":"simple-eval is a focused JavaScript library designed for evaluating simple expressions safely, providing an alternative to the native `eval()` function with a controlled execution environment. The current stable version is 2.0.0. It aims for a moderate release cadence, primarily for maintenance, bug fixes, or minor feature additions. A key differentiator is its limited instruction set, which enhances security by disallowing declarations, assignments, and complex statements, making it safer than direct `eval` for untrusted input, though it does not provide a full sandbox. It uses `jsep` as the default AST parser but supports any ESTree compliant parser like `acorn`, `@babel/parser`, or `esprima`, offering flexibility in parsing logic. This makes it suitable for scenarios requiring lightweight, controlled expression evaluation.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install simple-eval"],"cli":null},"imports":["import simpleEval from 'simple-eval';","import type { SimpleEvalFunction } from 'simple-eval';","import type { CustomParserOptions } from 'simple-eval';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import simpleEval from 'simple-eval';\n\n// Basic arithmetic evaluation\nconst result1 = simpleEval('2 + 4 * 10 + -4');\nconsole.log(`'2 + 4 * 10 + -4' evaluates to: ${result1}`); // Expected: 38\n\n// Using a context object for external variables or functions\nconst context = {\n  Math,\n  user: {\n    name: 'Alice',\n    age: 30,\n    isAdmin: true\n  },\n  greet: (name) => `Hello, ${name}!`\n};\n\nconst result2 = simpleEval('Math.floor(Math.PI * 10)', context);\nconsole.log(`'Math.floor(Math.PI * 10)' with Math context evaluates to: ${result2}`); // Expected: 31\n\nconst result3 = simpleEval('user.isAdmin ? greet(user.name) : \\'Access Denied\\'', context);\nconsole.log(`Conditional access with custom function and object: ${result3}`); // Expected: 'Hello, Alice!'\n\n// Attempting to use an undeclared variable (will throw if not in context)\ntry {\n  simpleEval('unknownVariable + 5');\n} catch (e) {\n  console.error(`Error evaluating 'unknownVariable + 5': ${e.message}`); // Expected: 'unknownVariable is not defined'\n}\n","lang":"typescript","description":"This quickstart demonstrates basic expression evaluation, passing a context object to allow access to `Math` functions, custom objects, and user-defined functions, and shows how to handle errors from undefined variables.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}