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

string-function-exec

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

string-function-exec

Compiles and executes stringified javascript's/node.js functions

  • 0.5.15
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
486
increased by80%
Maintainers
1
Weekly downloads
 
Created
Source

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


// A string containing "regular" javascript function to be executed
var fn1_s = "function f(alias, args) { return alias + \" \" + JSON.stringify(args); }";
 
console.log(new StringFunction(fn1_s).parse_function("fn1",[1,2,3]));
 
// A string containing "regular" javascript function containing "anonymous" and "arrow" functions
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]));
 
// A string containing "anonymous" javascript function to be executed
var fn3_s = "function (alias, args) { return alias.toString() + \" \" + JSON.stringify(args); }";
 
console.log(new StringFunction(fn3_s).parse_function("fn3",[100,200,300]));
 
// A string containing "arrow" javascript function to be executed
var fn4_s = "(alias, args)=>{ return alias.toString() + \" \" + JSON.stringify(args); }";
 
console.log(new StringFunction(fn4_s).parse_function("fn4",[1000,2000,3000]));
 
// A string containing two nested "arrow" javascript function to be executed
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


// A string containing "regular" javascript function to be executed
var fn1_s = "function f(alias, args) { return alias + \" \" + JSON.stringify(args); }";
 
console.log(new StringFunction(fn1_s).execute("fn1",[1,2,3]));
 
// A string containing "regular" javascript function containing "anonymous" and "arrow" functions
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]));
 
// A string containing "anonymous" javascript function to be executed
var fn3_s = "function (alias, args) { return alias.toString() + \" \" + JSON.stringify(args); }";
 
console.log(new StringFunction(fn3_s).execute("fn3",[100,200,300]));
 
// A string containing "arrow" javascript function to be executed
var fn4_s = "(alias, args)=>{ return alias.toString() + \" \" + JSON.stringify(args); }";
 
console.log(new StringFunction(fn4_s).execute("fn4",[1000,2000,3000]));
 
// A string containing two nested "arrow" javascript function to be executed
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.

Keywords

FAQs

Package last updated on 19 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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