npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

string-function-exec

v0.5.15

Published

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

Downloads

957

Readme

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.