bijington/expressive

Question: How to add custom function?

Closed this issue · 1 comments

Is there anyway to add a custom function to parse, ex: convert a date to a different time zone?

Something like ConvertDate(#TODAY#,"UTC");

@iw-jway I assume you managed to answer your query but in case you didn't or others are unsure here is some guidance:

1 lambda function

var expression = new Expression("myfunc(1)");
expression.RegisterFunction("ConvertDate", (p, v) =>
{
    // p = parameters
    // v = variables
    var date = p[0].Evaluate() as DateTime;

    if(date is null)
    {
        return null;
    }

    if (p[1].Evaluate() as string == "UTC")
    {
        // however you want to convert.
    }
    // any default to return
});
var result = expression.Evaluate();

2 custom function

This should be documented here but it might not be the most discoverable.

Sorry I have written this on my phone so the example might not be entirely whole and complete but it should provide a good starting point.