smartcal
Version:
SmartCal is a lightweight and powerful library for evaluating complex mathematical expressions dynamically. It supports tokenization, parsing, and interpreting mathematical formulas seamlessly, offering flexibility to integrate custom data objects for dyn
165 lines (152 loc) • 5.73 kB
TypeScript
// Generated by dts-bundle v0.7.3
export { ConditionResult, CompiledExpression };
/**
* Represents a compiled formula expression that can be evaluated with data.
* @interface
*/
export interface CompiledExpression {
type: "CompiledExpression";
/**
* Evaluates the expression with the provided data.
* @template T
* @param {T} data - The data object to evaluate the expression against
* @returns {string | number} The result of the evaluation
*/
evaluate<T extends DataType>(data: T): string | number;
/**
* Returns the string representation of the expression.
* @returns {string} The original expression string
*/
toString(): string;
}
/**
* Implementation of the CompiledExpression interface for formula expressions.
* @class
* @implements {CompiledExpression}
*/
export class CompiledFormulaExpression implements CompiledExpression {
type: "CompiledExpression";
/**
* Creates a new instance of CompiledFormulaExpression.
* @param {string} expression - The formula expression to compile
*/
constructor(expression: string);
/**
* Evaluates the expression with the provided data.
* @template T
* @param {T} data - The data object to evaluate the expression against
* @returns {string | number} The result of the evaluation
*/
evaluate<T extends DataType>(data: T): string | number;
/**
* Returns the string representation of the expression.
* @returns {string} The original expression string
*/
toString(): string;
}
export const REGEX: {
formulaOperatorG: RegExp;
formulaOperator: RegExp;
formulaFieldName: RegExp;
};
export const ConditionResult: {
True: number;
False: number;
};
export const AdditionOperator = "+";
export const SubtractionOperator = "-";
export const DivisionOperator = "/";
export const MultiplicationOperator = "*";
export const ExponentialOperator = "^";
export const ModuloOperator = "%";
export const LogicalAndOperator = "&&";
export const LogicalOrOperator = "||";
export const GreaterThanOperator = ">";
export const LessThanOperator = "<";
export const GreaterThanOrEqualOperator = ">=";
export const LessThanOrEqualOperator = "<=";
export const EqualOperator = "==";
export const NotEqualOperator = "!=";
export const AssignmentOperator = "=";
export const ParenthesisOpenOperator = "(";
export const ParenthesisCloseOperator = ")";
export const ColonOperator = ":";
export const QuestionMarkOperator = "?";
export const BackslashOperator = "\\";
export const ComparisonOperator: string[];
export const ArithmeticOperator: string[];
export const Operators: string[];
export const AllOperators: string[];
export const Priority_1_Operator: string[];
export const Priority_2_Operator: string[];
export const Priority_3_Operator: string[];
export const Priority_4_Operator: string[];
export type DataType = {
[key: string]: number | string | CompiledExpression;
};
const OperatorValue: string[];
/**
* Represents the supported operators in the expression.
*/
export type Operator = (typeof OperatorValue)[number];
export {};
/**
* Evaluates a mathematical expression and returns the result.
*
* This function parses and interprets a mathematical formula represented as a string,
* applying dynamic values from a given object to resolve variables or conditions within the expression.
*
* @template T - A generic type representing the structure of the input object. Keys are variable names, and values can be numbers, strings, or arrays.
* @param {string} expression - The mathematical expression to be evaluated.
* Variables in the expression should correspond to keys in the `obj` parameter.
* @param {T} obj - An object containing the values of the variables referenced in the expression.
* @returns {number | string | any[]} - The result of the evaluated expression, which can be a number, a string, or an array depending on the expression's logic.
*/
export default function SmartCal<T extends DataType>(expression: string, obj?: T): number | string;
/**
* Verify if the given expression is valid formula
* @param expression expression to evaluate
* @returns {boolean} true if the expression is valid
*/
export function isValidExpression(expression: string): boolean;
/**
* Compiles a formula expression string into a CompiledExpression object.
* @param {string} expression - The formula expression to compile
* @returns {CompiledExpression} A compiled expression that can be evaluated
* @example
* const expr = compile("age+3");
* const result = expr.evaluate({age:18});
*/
export function compile(expression: string): CompiledExpression;
export class FormulaInterpreterError implements Error {
message: string;
name: string;
stack?: string | undefined;
constructor(message: string, error?: string);
}
export class FormulaVariableNotFoundError extends Error {
name: string;
data: {
variableName: string;
container: object;
};
constructor(message: string, variableName: string, variableContainer: object);
getData(): {
variableName: string;
container: object;
};
}
export class IncorrectSyntaxError extends Error {
name: string;
constructor(message: string, exp: string);
getData(): {
exp: string;
};
}
export class InvalidFormulaError extends Error {
name: string;
constructor(message: string, exp: string);
getData(): {
exp: string;
};
}