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

Small library to dynamically create and evaluate expression with multiple parameters (even undefined)

  • 1.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
100
decreased by-0.99%
Maintainers
1
Weekly downloads
 
Created
Source

safe-evaluate-expression

NPM NPM NPM Codecov Libraries.io dependency status for latest release NPM

Small library to dynamically create and evaluate expression with multiple parameters (even undefined). To handle more sofisticate use cases is provided a Factory functionality to build evaluate functions with some spice 🔥.

It also offer an ancillary function to protect lambda function to undefined params inputs.

Installation

npm install safe-evaluate-expression

Evaluate

evaluate(expression:[String], params:[Object]) -> [expression evaluated]

Example

const evaluate = require("safe-evaluate-expression");
evaluate("a > 1", { a: 3 }); // -> true

NB. As constant params in expression you can use only string and integers (eg. 1, "a") no floating numbers!

Advanced Example

const evaluate = require("safe-evaluate-expression");

const operators = {
  isUndefined: (x) => x === undefined,
  isEqual: (a, b) => a === b,
  isGreater: (a, b) => a > b,
  isLower: (a, b) => a < b,
};

const vars = { a: 1, b: 1, c: 2 };
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

// It works also with infinite nested conditions
evaluate("(isUndefined(notDefined) || (isGreater(c, a) && isLower(b, c))) && isEqual(a,1)", params); // -> true

Factory

factory(options:[Object]) -> [evaluate function]

Example

const { factory, operators } = require("safe-evaluate-expression");
const evaluate = factory({ operators, multipleParams: true, translateLogical: true });

const metadata = { x: 1.1, y: 2 };
const list = { k: 3, z: 4 };
const map = new Map([["pi", 3.14]]);

const expression1 = "isLower(x,z)";
const expression2 = "isLower(k,y)";

evaluate(expression1, metadata, list); // -> true
evaluate(expression2, metadata, list); // -> false
evaluate(`${expression1} AND ${expression2}`, metadata, list); // -> false
evaluate(`${expression1} OR ${expression2}`, metadata, list); // -> true

const expression3 = "isLower(notDefined,z)"; // put a not defined value

evaluate(expression3, metadata, list);
evaluate(`${expression3} AND ${expression2}`, metadata, list); // -> false
evaluate(`(isLower(x,z) AND isLower(k,y) OR (isLower(z,P) AND NOT isLower(P,k)))`, metadata, list);

evaluate(`isLower(z,pi)`, metadata, list, map); // -> false

Factory Params

ParamDescriptionDefault
multipleParams Define if evaluate function will take a single object as params (default - eg. eval("espression",{})) or if it takes multiple params (eg. eval("espression", param1, param2, ...)) false
operatorsInScope Define if operators object is converted inline to have all the operators directly in the scope of the evaluation expression. If true the operators are putted in the expression context as: const [operatorName] = [operator]. Otherwise operators are passed thru as a single object while all operators logic inside expression are automatically prefixed. In general: use operatorsInScope = true if you use operators without external dependencies. If you use third party libraries to handle operators logic you must pass operators as an object. false
translateLogical Determine if translate "AND", "OR" and "NOT" words inside expression. If true all the logical operators are converted accordingly (eg. "AND" -> &&) false
operators Object containing all the operators function you want to use to evaluate expression (eg. { isEqual: (a,b) => a === b }). For convenience the library export a small set of default operators.
undef An optional parameter to specify the value to be returned when expression occurs in undefined params. undefined

Safe Lambda

safeLambda(lamdaFunc, [undefined defalut])

Protect lambda function by assigning a default value for undefined input paramters.

const { safeLambda } = require("safe-evaluate-expression");

const lambda = (a, b, c) => a + b + c;
const protectedLambda = safeLambda(lambda, 0);

// The unprotected lambda returns NaN because all values are undefined
// The protected one return zero (default): 0 + 0 + 0
console.log(lambda(), protectedLambda()); // -> NaN 0

Keywords

FAQs

Package last updated on 12 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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