index.d.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * Formula parser
  3. */
  4. export class Parser<T extends (string | number)> {
  5. /**
  6. * Create a new formula parser.
  7. *
  8. * @param formula - the formula string to parse.
  9. * @param options - optional settings.
  10. */
  11. constructor(formula: string, options?: Options);
  12. /**
  13. * Evaluate the formula.
  14. *
  15. * @param context - optional object with runtime formula context used to resolve variables.
  16. *
  17. * @returns the string or number outcome of the resolved formula.
  18. */
  19. evaluate(context?: any): T;
  20. }
  21. export interface Options {
  22. /**
  23. * A hash of key - value pairs used to convert constants to values.
  24. */
  25. readonly constants?: Record<string, any>;
  26. /**
  27. * A regular expression used to validate token variables.
  28. */
  29. readonly tokenRx?: RegExp;
  30. /**
  31. * A variable resolver factory function.
  32. */
  33. readonly reference?: Options.Reference;
  34. /**
  35. * A hash of key-value pairs used to resolve formula functions.
  36. */
  37. readonly functions?: Record<string, Function>;
  38. }
  39. export namespace Options {
  40. type Reference = (name: string) => (context: any) => any;
  41. }