Standard Custom Expression Functions
Summary
- Standard Custom Expression Functions are evaluated on a per-row basis
- The return type is a single value
Standard Custom Expression Functions are Expression Functions which are:
- provided by developers at design-time
- operate on a single row a time during evaluation
Although, the return type can be any value, AdapTable divides them into 2 groups for ease of use:
- Boolean Expression Functions - return true / false value
- Scalare Expression Functions - return any value
How It Works
The process of defining, using and evaluation a Custom Expression Function is as follows:
-
A developer defines an Expression Function - it includes various properties including a
handlerfunctionHint
- Many of the properties are 'convenience' ones which provide help and examples
- These are designed to aid the user when accessing the function in the Expression Editor
-
The Expression Function is then treated like any other Function and is included in the Expression Editor
-
A run-time User includes the Expresssion Function in an Expression (this can be done in Initial State also)
-
AdapTableQL invokes the
handlerfunction when it evaluates the Expression and returns the correct value
Developer Guide
Defining a Custom Expression Function
In this Guide we will create 2 Custom Expressions:
- a Boolean Function -
THIS_BUSINESS_YEAR- which will return true or false - a Scalar Function -
USD_CONVERT- which returns a number
// Expression Options
expressionOptions: {
customBooleanFunctions: {
THIS_BUSINESS_YEAR: {
handler(args: any[], context: ExpressionContext) {
const startBusinessYear = new Date(2023, 4, 1);
return new Date(args[0]) > startBusinessYear;
},
returnType: 'boolean',
description: 'Returns true if Date is in current Business year',
signatures: ['THIS_BUSINESS_YEAR(dateToCheck: Date)'],
examples: ['THIS_BUSINESS_YEAR([tradeDate])'],
},
},
customScalarFunctions: {
USD_CONVERT: {
handler(args, context: ExpressionContext) {
const inputValue: number = args[0];
if (isNaN(inputValue)) {
return undefined;
}
const oldCurrency = args[1];
if (oldCurrency === 'USD') {
return inputValue;
}
if (oldCurrency === 'EUR') {
return inputValue * 1.15;
} else if (oldCurrency === 'GBP') {
return inputValue * 1.2;
}
return undefined;
},
returnType: 'number',
description: 'Converts EUR & GBP Currencies to Dollar',
signatures: ['USD_CONVERT(val: number, ccy: string)'],
examples: [
'USD_CONVERT([value],[currency])',
'USD_CONVERT([value], "GBP")',
],
},
},
}Add the function to relevant section in ExpressionOptions:
- boolean functions:
customBooleanFunctions - scalar functions:
customScalarFunctions
Both types are a Record with 2 properties:
- keys: custom function names
- values: of type
ExpressionFunction
By convention, the Function name is in CAPITAL LETTERS - to make it more readable when used in an Expression.
The handler property is the most important property, providing the actual implementation of the Function.
It receives args array (of type any[]) and context property (type ExpressionContext) and returns a Boolean.
Set the returnType of the function
This is particularly important for boolean Expressions
The Description is designed to help a User accessing the Function in the Expression Editor
It should explain what the Function does
There are 2 extra properties that can provide context sensitive help to users accessing the Custom Function:
signatures: Shows how the Function should be calledexamples: Small examples of Function in action
Expression Function Object
Each Standard Custom Expression function is an instance of an ExpressionFunction object.
Deep Dive
Anatomy of an Expression Function
This object contains many properties, including handler which performs the actual evaluation.
Hint
It's advisable to provide the other properties as it greatly helps run-time users accessing the function in an Expression
Expression Function handler
As noted above, handler is the key (and only mandatory) property as is performs the actual evaluation.
It takes the form of a function which receives 2 parameters (args and context) and returns any value.
Deep Dive
Understanding the handler property
- This example creates 2 Custom Scalar Expression Functions - used in a table showing (random) purchases:
- a
THIS_BUSINESS_YEARCustom Boolean Expression Function - returns true if Date is in current Business Year (after 1 April) - a
USD_CONVERTCustom Scalar Expression Function - fictitiously converts USD and GBP prices to Dollars
- a
- We then reference each of these Expression Functions in other Modules:
THIS_BUSINESS_YEARin a Format Column which puts matching rows in Bold, Italics and a Blue FontUSD_CONVERTin theDollar PriceCalculated Column- Both Custom Expressions in the
Old High ExchangeNamed Query
Expand to see the Definitions provided
Open the Expression Editor and see the 2 custom Expression Functions in the Functions Dropdown
Defining Custom Expression Functions
As noted above Custom Expression functions can be of 2 return types:
- Boolean - used in Grid Filter, Conditional Styles, Reports etc. and return a true / false value
- Scalar - used in Calculated Columns and can have any return type
Boolean Expression Functions
The customBooleanFunctions property is used to define custom Boolean Expression Functions:
customBooleanFunctions
Default: nullRecord<string, ExpressionFunction>Scalar Expression Functions
The customScalarFunctions property is used to define Scalar Expression Functions:
customScalarFunctions
Default: nullRecord<string, ExpressionFunction>FAQ
Does the Custom Expression Function's name have to be capitalised? No, its not mandatory but it is a convention that we encourage, as it makes the Expression more readable.