@samatawy/rules
    Preparing search index...

    Interface FunctionProvider

    Implement this interface to provide custom functions that can be used in rules. Each provider can supply one or more functions, and the function factory will use these providers to create function expressions when parsing rules. This allows for extensibility and customization of the functions available in the rule engine without modifying the core codebase.

    interface FunctionProvider {
        names(): string[];
        create(name: string, args: Expression[]): FunctionExpression | undefined;
        mock(name: string, args: Expression[]): FunctionExpression | undefined;
        toJS(name: string): { args: string[]; body: string } | undefined;
    }
    Index

    Methods

    • Get the names of the functions provided by this provider. This is used by the function factory to determine which provider can create a function expression for a given function name.

      Returns string[]

      an array of function names provided by this provider.

    • Create a valid function expression for the given function name and arguments. The function factory will call this method when it needs to create a function expression for a function name that this provider supports.

      Ideally, arguments should be checked for validity (e.g., correct number and types) before creating the function expression, and an error should be thrown if the arguments are invalid.

      Parameters

      • name: string

        the name of the function to create.

      • args: Expression[]

        the arguments to pass to the function.

      Returns FunctionExpression | undefined

      an error if the arguments are invalid for the given function name.

    • Create a mock function expression for the given function name and arguments. This is used by the autocomplete feature to provide suggestions for function names and arguments. Arguments are not checked for validity.

      Parameters

      • name: string

        the name of the function to create a mock for.

      • args: Expression[]

        the arguments to pass to the mock function.

      Returns FunctionExpression | undefined

      a mock function expression, or undefined if the function name is not supported.

    • Convert the function to a JavaScript representation. This can be used to generate JavaScript code from the function expression using dynamic compilation.

      Parameters

      • name: string

        the name of the function to convert.

      Returns { args: string[]; body: string } | undefined

      an object containing the argument names and the function body as a string, or undefined if the function name is not supported.