Adds ability to write .js files to add custom functions to AdaptiveExpressions
$ dotnet add package Iciclecreek.AdaptiveExpressions.Javascript![]()
This library provides the ability to call javascript functions from AdaptiveExpressions
To install
dotnet add package Iciclecreek.AdaptiveExpressions.Javascript
Given a javascript with functions in it:
Example: myfunctions.js
// Export functions
let exports = {};
exports.add2Numbers = add2Numbers;
function add2Numbers(x , y)
{
return x + y;
}
To Load it, simply read the text file and call RegisterFunction with namespace.
var javascript = File.ReadAllText("myfunctions.js");
JavasacriptFunctions.RegisterFunction("contoso", javascript);
If you call Register with a ResourceExplorer then all files of extension ".function.js" will be automatically mounted with the file name (minus .function.js) will be used as the namespace. ResourceExplorer change detection will reload the functions if the file changes.
In startup
ResourceExplorer resourceExplorer = new ResourceExplorer(...) ... ;
JavascriptFunctions.AddJavascriptFunctions(resourceExplorer);
NOTE: As of 4.12 you do not need to modify your startup if you are using the new runtime component It will automatically be registered in resourceExplorer mode.
Every top level function in myfunctions.js will be mounted in the given namespace (the default namespace will be filename if you are using resource explorer). To call a function you simply use the namespace+function name with args.
Example Expression calling function registered with RegisterFunction and a namespace 'contoso'
contoso.Add2Numbers(user.age, 7)
*Example Expression defined as resource file myfunctions.js *
myfunctions.Add2Numbers(user.age, 7)
var expression = Expression.Parse("contoso.Add2Numbers(user.age, 7)");
This project uses Jint javascript interpreter to execute the javascript.
This engine does not have access to the file system, or to the network. Each file is loaded into it's own interpreter, so there is no shared execution environment between javascript files.