@marterrez/eval-expression
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -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 @@ } |
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
5067
80