Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

safe-evaluate-expression

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

safe-evaluate-expression - npm Package Compare versions

Comparing version 1.0.4 to 1.0.5

39

examples.js

@@ -9,23 +9,32 @@ const evaluate = require("./");

isLower: (a, b) => a < b,
greaterEqualThan: (a, b) => Number(a) >= Number(b),
lessEqualThan: (a, b) => Number(a) <= Number(b),
};
const vars = { a: 1, b: 1, c: 2 };
const vars = { a: 1, b: 1, c: 2, when_current_hour: 12 };
const params = { ...vars, ...operators };
evaluate("isEqual(a,b)", params); // -> true
evaluate("isEqual(a,c)", params); // -> false
evaluate("isEqual(a,notDefined)", params); // -> false
evaluate("isUndefined(a)", params); // -> false
evaluate("isUndefined(notDefined)", params); // -> true
// evaluate("isEqual(a,b)", params); // -> true
// evaluate("isEqual(a,c)", params); // -> false
// evaluate("isEqual(a,notDefined)", params); // -> false
// evaluate("isUndefined(a)", params); // -> false
// evaluate("isUndefined(notDefined)", params); // -> true
// It works also with infinite nested conditions
const complex = evaluate(
"(isUndefined(notDefined) || (isGreater(c, a) && isLower(b, c))) && isEqual(a,1)",
params
); // -> true
// // It works also with infinite nested conditions
// const complex = evaluate(
// "(isUndefined(notDefined) || (isGreater(c, a) && isLower(b, c))) && isEqual(a,1)",
// params
// ); // -> true
const lambda = (a, b, c) => a + b + c;
const protectedLambda = defaultLambda(lambda, 0);
// const lambda = (a, b, c) => a + b + c;
// const protectedLambda = defaultLambda(lambda, 0);
console.log("Evaluate Comples", complex);
console.log(lambda(), protectedLambda());
// console.log("Evaluate Comples", complex);
// console.log(lambda(), protectedLambda());
console.log(
evaluate(
`greaterEqualThan(when_current_hour, "12") && lessEqualThan(when_current_hour, "15")`,
params
)
);
//**************************************************************
// RegEx to find parameters, comments and arguments
//**************************************************************
const FUNC_PARAMS = /\b\w+(\b(?!\())/g;
const FUNC_PARAMS_SIMPLE = /\b\w+(\b(?!\())/g;
const FUNC_PARAMS = /[\"|\']?\w+(\b(?!\())[\"|\']?/;
const STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm;

@@ -29,3 +30,5 @@ const ARGUMENT_NAMES = /([^\s,]+)/g;

//**************************************************************
const makeSafeParam = (param, undef) => `(() => {
const makeSafeParam = (param, undef) => {
console.log(param.toString());
const wrap = `(() => {
try {

@@ -37,2 +40,5 @@ return ${param} !== undefined ? ${param} : ${undef};

})()`;
//console.log("param", param, wrap);
return wrap;
};

@@ -54,2 +60,4 @@ //**************************************************************

console.log(makeSafe(body, undef));
const func = new Function(input, `return ${makeSafe(body, undef)}`);

@@ -56,0 +64,0 @@ return func(params);

{
"name": "safe-evaluate-expression",
"version": "1.0.4",
"version": "1.0.5",
"description": "Small library to dynamically create and evaluate expression with multiple parameters (even undefined)",

@@ -5,0 +5,0 @@ "main": "index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc