Skip to main content

Adds ability to write .js files to add custom functions to AdaptiveExpressions

Total Downloads
28.3K
28,322
Published
Jun 16, 2023
3y ago

Get Started

dotnet add package Iciclecreek.AdaptiveExpressions.Javascript --version 4.20.0
ProductVersions
.NET Standard
netstandard2.0

README

icon

Javascript Functions for AdaptiveExpression

This library provides the ability to call javascript functions from AdaptiveExpressions

Installation

To install dotnet add package Iciclecreek.AdaptiveExpressions.Javascript

Define functions

Given a javascript with functions in it:

Example: myfunctions.js

// Export functions
let exports = {};
exports.add2Numbers = add2Numbers;

function add2Numbers(x , y)
{
    return x + y;
}

(Option 1) Load functions into adaptive expressions.

To Load it, simply read the text file and call RegisterFunction with namespace.

var javascript = File.ReadAllText("myfunctions.js");
JavasacriptFunctions.RegisterFunction("contoso", javascript);

(Option 2) Load functions with Bot Framework Resource Explorer

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.

To call a function that has been added

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)

Example Expression parsing from C#

var expression = Expression.Parse("contoso.Add2Numbers(user.age, 7)");

Internal Details

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.

Downloads by Version

Similar & Alternative Packages

Hand-picked matching NuGet libraries based on tags & ecosystem

Depended On By

Find packages that declare this package as a dependency. NuGet does not provide a reverse-dependency endpoint, so candidates are checked on demand.

Adoption Assessment

Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.

Compatibility metadata lists 1 target framework; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.