Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more js functions sync with C# #1505

Merged
merged 1 commit into from
Dec 16, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 91 additions & 1 deletion libraries/botframework-expressions/src/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
import { BuiltInFunctions } from './builtInFunction';
import { Constant } from './constant';
import { ExpressionEvaluator } from './expressionEvaluator';
import { ExpressionEvaluator, EvaluateExpressionDelegate } from './expressionEvaluator';
import { ExpressionType } from './expressionType';
import { SimpleObjectMemory, MemoryInterface } from './memory';
import { Extensions } from './extensions';
Expand Down Expand Up @@ -89,6 +89,96 @@ export class Expression {
return expr;
}

/**
* Construct an expression from a EvaluateExpressionDelegate
* @param func Function to create an expression from.
*/
public static lambaExpression(func: EvaluateExpressionDelegate): Expression {
return new Expression(ExpressionType.Lambda, new ExpressionEvaluator(ExpressionType.Lambda, func));
}

/**
* Construct an expression from a lamba expression over the state.
* Exceptions will be caught and surfaced as an error string.
* @param func ambda expression to evaluate.
* @returns New expression.
*/
public static lambda(func: (arg0: any) => any): Expression {
return new Expression(ExpressionType.Lambda, new ExpressionEvaluator(ExpressionType.Lambda,
(_expression: Expression, state: any): { value: any; error: string } => {
let value: any;
let error: string;
try {
value = func(state);
} catch (funcError) {
error = funcError;
}

return { value, error };
}
));
}

/**
* Construct and validate an Set a property expression to a value expression.
* @param property property expression.
* @param value value expression.
* @returns New expression.
*/
public static setPathToValue(property: Expression, value: any): Expression {
if (value instanceof Expression) {
return Expression.makeExpression(ExpressionType.SetPathToValue, undefined, property, value);
} else {
return Expression.makeExpression(ExpressionType.SetPathToValue, undefined, property, new Constant(value));
}
}


/**
* Construct and validate an Equals expression.
* @param children Child clauses.
* @returns New expression.
*/
public static equalsExpression(...children: Expression[]): Expression {
return Expression.makeExpression(ExpressionType.Equal, undefined, ...children);
}

/**
* Construct and validate an And expression.
* @param children Child clauses.
* @returns New expression.
*/
public static andExpression(...children: Expression[]): Expression {
if (children.length > 1) {
return Expression.makeExpression(ExpressionType.And, undefined, ...children);
} else {
return children[0];
}
}

/**
* Construct and validate an Or expression.
* @param children Child clauses.
* @returns New expression.
*/
public static orExpression(...children: Expression[]): Expression {
if (children.length > 1) {
return Expression.makeExpression(ExpressionType.Or, undefined, ...children);
} else {
return children[0];
}
}

/**
* Construct and validate an Not expression.
* @param children Child clauses.
* @returns New expression.
*/
public static notExpression(child: Expression): Expression {
return Expression.makeExpression(ExpressionType.Not, undefined, child);
}


/**
* Validate immediate expression.
*/
Expand Down