safe-evaluate-expression
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -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 | ||
) | ||
); |
12
index.js
//************************************************************** | ||
// 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", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18023
101