StringFunction
This module provides an ability to execute stringified javascript/node.js
functions. StringFunction
module can basically used
to execute any type of javascript's string-compiled routines such as a various of regular
, anonymous
or arrow
- functions.
Note #1 This module
can be used with any existing version of node.js
and npm
.
How to install the module:
By using npmjs.org
:
npm install string-function-exec --save
How to use the string-function-exec
module installed:
Parsing JavaScript/Node.JS stringified functions
var fn1_s = "function f(alias, args) { return alias + \" \" + JSON.stringify(args); }";
console.log(new StringFunction(fn1_s).parse_function("fn1",[1,2,3]));
var fn2_s = "function f(alias, args) { var f1 = function (t_param1) { return t_param1; }; \
var f2 = (t_param2, t_args) => { return t_param2 + \" \" + t_args; }; \
return f1(alias) + \" \" + f2(alias, args) + \" \" + JSON.stringify(args); }";
console.log(new StringFunction(fn2_s).parse_function("fn2",[10,20,30]));
var fn3_s = "function (alias, args) { return alias.toString() + \" \" + JSON.stringify(args); }";
console.log(new StringFunction(fn3_s).parse_function("fn3",[100,200,300]));
var fn4_s = "(alias, args)=>{ return alias.toString() + \" \" + JSON.stringify(args); }";
console.log(new StringFunction(fn4_s).parse_function("fn4",[1000,2000,3000]));
var fn5_s = "(alias, args) => { var f1 = (p1, p2)=>{ return p1 + \" \" + JSON.stringify(p2); }; return f1(alias, args);}";
console.log(new StringFunction(fn5_s).parse_function("fn5",[10000,20000,30000]));
Stringified JavaScript/Node.JS functions execution
var fn1_s = "function f(alias, args) { return alias + \" \" + JSON.stringify(args); }";
console.log(new StringFunction(fn1_s).execute("fn1",[1,2,3]));
var fn2_s = "function f(alias, args) { var f1 = function (t_param1) { return t_param1; }; \
var f2 = (t_param2, t_args) => { return t_param2 + \" \" + t_args; }; \
return f1(alias) + \" \" + f2(alias, args) + \" \" + JSON.stringify(args); }";
console.log(new StringFunction(fn2_s).execute("fn2",[10,20,30]));
var fn3_s = "function (alias, args) { return alias.toString() + \" \" + JSON.stringify(args); }";
console.log(new StringFunction(fn3_s).execute("fn3",[100,200,300]));
var fn4_s = "(alias, args)=>{ return alias.toString() + \" \" + JSON.stringify(args); }";
console.log(new StringFunction(fn4_s).execute("fn4",[1000,2000,3000]));
var fn5_s = "(alias, args) => { var f1 = (p1, p2)=>{ return p1 + \" \" + JSON.stringify(p2); }; return f1(alias, args);}";
console.log(new StringFunction(fn5_s).execute("fn5",[10000,20000,30000]));
Constructor
.StringFunction
#####.StringFunction(StringFunc)
StringFunc - an input string containing function's code,
Methods
.parse_function(Args)
parses a stringified javascript/node.js function and returns the compiled function's code ready to be executed by using generic javascript's global.eval
function. Method parse_function
accepts an array of arguments Args
of any time for a javascript's function being executed.
.execute( arguments )
console.log(new StringFunction(fn1_s).execute("fn1",[1,2,3]));
compiles and executes javascript/node.js function passed as a parameter of string type to the StringFunction
object's constructor. The method execute
accepts the list of arguments passed to a stringified function being executed. This method normally returns a value passed at the end of stringified function execution or an specific code if a compilation or execution error has occured.
Sample
'use strict'
var StringFunction = require('string-function-exec');
var sf_obj = new StringFunction("function test(p1, p2) { return p1 + p2; }");
var compiled_func = sf_obj.parse_function(10, 20);
var result = sf_obj.execute(10,20);
if (compiled_func.length > 0 && result != undefined) {
console.log("compiled function = " +
compiled_func + "\n" + "return value = " + JSON.stringify(result));
}
process.exit(0);
Conclusion
That's All Folks :)
Author
Arthur V. Ratz @ Epsilon Software Development Labs.