safe-evaluate-expression
Advanced tools
Comparing version 1.0.3 to 1.0.4
53
index.js
//************************************************************** | ||
// RegEx to find all parameters in function and sub-function | ||
// RegEx to find parameters, comments and arguments | ||
//************************************************************** | ||
const findParams = /\b\w+(\b(?!\())/g; | ||
const FUNC_PARAMS = /\b\w+(\b(?!\())/g; | ||
const STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm; | ||
const ARGUMENT_NAMES = /([^\s,]+)/g; | ||
//************************************************************** | ||
// Returns regular function parameters names | ||
//************************************************************** | ||
function getParamNames(func) { | ||
const fnStr = func.toString().replace(STRIP_COMMENTS, ""); | ||
const result = fnStr.slice(fnStr.indexOf("(") + 1, fnStr.indexOf(")")).match(ARGUMENT_NAMES); | ||
return result || []; | ||
} | ||
//************************************************************** | ||
// Returns labda function body | ||
//************************************************************** | ||
function getLambdaBody(func) { | ||
const fnStr = func.toString().replace(STRIP_COMMENTS, ""); | ||
return fnStr.substring(fnStr.indexOf("=>") + 3); | ||
} | ||
//************************************************************** | ||
// Wrap a param in a try-catch to handle undefined params | ||
//************************************************************** | ||
const makeSafeParam = (param) => `(() => { | ||
const makeSafeParam = (param, undef) => `(() => { | ||
try { | ||
return ${param}; | ||
return ${param} !== undefined ? ${param} : ${undef}; | ||
} catch (e) { | ||
return undefined; | ||
return ${undef}; | ||
} | ||
@@ -21,3 +41,3 @@ })()`; | ||
//************************************************************** | ||
const makeSafe = (str) => str.replace(findParams, (p) => makeSafeParam(p)); | ||
const makeSafe = (str, undef) => str.replace(FUNC_PARAMS, (p) => makeSafeParam(p, undef)); | ||
@@ -28,3 +48,3 @@ //************************************************************** | ||
//************************************************************** | ||
const evaluate = (body, params) => { | ||
const evaluate = (body, params, undef) => { | ||
const input = `{${Object.keys(params) | ||
@@ -34,6 +54,23 @@ .map((k) => k) | ||
const func = new Function(input, `return ${makeSafe(body)}`); | ||
const func = new Function(input, `return ${makeSafe(body, undef)}`); | ||
return func(params); | ||
}; | ||
//************************************************************** | ||
// Get a lambda function as input and optiona parameters | ||
// to default undefined params in returnd function | ||
//************************************************************** | ||
function defaultLambda(func, undef) { | ||
const expression = getLambdaBody(func).trim(); | ||
const names = getParamNames(func); | ||
return function (...vars) { | ||
const params = {}; | ||
names.forEach((name, index) => (params[name] = vars[index])); | ||
return evaluate(expression, params, undef); | ||
}; | ||
} | ||
module.exports = evaluate; | ||
module.exports.defaultLambda = defaultLambda; |
{ | ||
"name": "safe-evaluate-expression", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Small library to dynamically create and evaluate expression with multiple parameters (even undefined)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# safe-evaluate-expression | ||
Small library to dynamically create and evaluate expression with multiple parameters (even undefined). | ||
Small library to dynamically create and evaluate expression with multiple parameters (even undefined). _It also offer an ancillary function to protect lambda function to undefined params inputs._ | ||
@@ -11,2 +11,6 @@ ## Installation | ||
## Usage | ||
### _evaluate(expression:[String], params:[Object])_ | ||
## Example | ||
@@ -43,1 +47,18 @@ | ||
``` | ||
## Default lambda undefined params | ||
### _protectedLambda(lamdaFunc, [undefined dafalut])_ | ||
Protect lambda function by assigning a default value for undefined input paramters. | ||
```javascript | ||
const { defaultLambda } = require("safe-evaluate-expression"); | ||
const lambda = (a, b, c) => a + b + c; | ||
const protectedLambda = defaultLambda(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 | ||
``` |
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
17512
5
86
63