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

@marterrez/eval-expression

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@marterrez/eval-expression - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

23

dist/index.js

@@ -17,3 +17,6 @@ "use strict";

class ExpressionParser {
constructor() {
constructor(opts = {
throwError: false,
debgger: false
}) {
this.context = {

@@ -23,2 +26,3 @@ _: lodash,

};
this.opts = opts;
}

@@ -30,9 +34,3 @@

eval(exp, params = {}, opts = {}) {
this.evalExp(exp, params, opts);
}
evalExp(exp, params, {
throwError = false
}) {
eval(exp, params = {}) {
if (!exp || !exp.length || typeof exp !== 'string') return null;

@@ -42,6 +40,12 @@ const ctx = { ...this.context,

};
if (this.opts.debgger) {
console.log(`* ExpressionParser: eval ${exp}`);
console.log(`* context = ${JSON.stringify(ctx, null, 2)}`);
}
const returnValue = exp.indexOf('return') > -1 ? exp : `return ${exp}`; // eslint-disable-next-line no-new-func
const compiledExp = new Function('ctx', `{ with (ctx) {${returnValue}} }`);
if (throwError) return compiledExp(ctx);
if (this.opts.throwError) return compiledExp(ctx);

@@ -51,2 +55,3 @@ try {

} catch (e) {
if (this.opts.debgger) console.log(`* ExpressionParser: error ${e}`);
return null;

@@ -53,0 +58,0 @@ }

{
"name": "@marterrez/eval-expression",
"version": "0.0.4",
"version": "0.0.5",
"description": "Small lib that allows to eval a string containing javascript code. It allows you do inject helper functions when evaluating the expression",

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

@@ -5,3 +5,3 @@ import * as lodash from 'lodash'

export default class ExpressionParser {
constructor () {
constructor (opts = { throwError: false, debgger: false }) {
this.context = {

@@ -11,2 +11,3 @@ _: lodash,

}
this.opts = opts
}

@@ -18,7 +19,3 @@

eval (exp, params = {}, opts = {}) {
this.evalExp(exp, params, opts)
}
evalExp (exp, params, { throwError = false }) {
eval (exp, params = {}) {
if (!exp || !exp.length || typeof exp !== 'string') return null

@@ -30,6 +27,11 @@

}
if (this.opts.debgger) {
console.log(`* ExpressionParser: eval ${exp}`)
console.log(`* context = ${JSON.stringify(ctx, null, 2)}`)
}
const returnValue = exp.indexOf('return') > -1 ? exp : `return ${exp}`
// eslint-disable-next-line no-new-func
const compiledExp = new Function('ctx', `{ with (ctx) {${returnValue}} }`)
if (throwError) return compiledExp(ctx)
if (this.opts.throwError) return compiledExp(ctx)

@@ -39,2 +41,3 @@ try {

} catch (e) {
if (this.opts.debgger) console.log(`* ExpressionParser: error ${e}`)
return null

@@ -41,0 +44,0 @@ }

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