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
string-function-exec
module installed:
How to use the Parsing JavaScript/Node.JS stringified functions
// A string containing "regular" javascript function to be executedvar fn1_s = "function f(alias, args) { return alias + \" \" + JSON.stringify(args); }"; console; // A string containing "regular" javascript function containing "anonymous" and "arrow" functionsvar 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; // A string containing "anonymous" javascript function to be executedvar fn3_s = "function (alias, args) { return alias.toString() + \" \" + JSON.stringify(args); }"; console; // A string containing "arrow" javascript function to be executedvar fn4_s = "(alias, args)=>{ return alias.toString() + \" \" + JSON.stringify(args); }"; console; // A string containing two nested "arrow" javascript function to be executedvar fn5_s = "(alias, args) => { var f1 = (p1, p2)=>{ return p1 + \" \" + JSON.stringify(p2); }; return f1(alias, args);}"; console;
Stringified JavaScript/Node.JS functions execution
// A string containing "regular" javascript function to be executedvar fn1_s = "function f(alias, args) { return alias + \" \" + JSON.stringify(args); }"; console; // A string containing "regular" javascript function containing "anonymous" and "arrow" functionsvar 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; // A string containing "anonymous" javascript function to be executedvar fn3_s = "function (alias, args) { return alias.toString() + \" \" + JSON.stringify(args); }"; console; // A string containing "arrow" javascript function to be executedvar fn4_s = "(alias, args)=>{ return alias.toString() + \" \" + JSON.stringify(args); }"; console; // A string containing two nested "arrow" javascript function to be executedvar fn5_s = "(alias, args) => { var f1 = (p1, p2)=>{ return p1 + \" \" + JSON.stringify(p2); }; return f1(alias, args);}"; console;
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 = ; var sf_obj = "function test(p1, p2) { return p1 + p2; }"; var compiled_func = sf_obj;var result = sf_obj; if compiled_funclength > 0 && result != undefined console; process;
Conclusion
That's All Folks :)
Author
Arthur V. Ratz @ Epsilon Software Development Labs.