f7
f7 is a spreadsheet formula execution library
git clone https://git.vogt.world/f7.git
Log | Files | README.md | LICENSE.md
← All files
name: src/main/js/formulas/AbstractFormula.ts
-rw-r--r--
3208
 1import { NAException } from "../errors/NAException";
 2import { SheetColumnRowKey } from "../models/common/SheetColumnRowKey";
 3import { CollateralLookupFunction, Complex, LookupFunction } from "../models/common/Types";
 4import { Formula } from "./Formula";
 5import { FormulaName } from "./FormulaName";
 6
 7/**
 8 * Abstract formula that has some defaults to ensure the safe execution of formulas.
 9 */
10export class AbstractFormula implements Formula {
11  NAME: FormulaName = null;
12  lookup: LookupFunction;
13  collateralLookup: CollateralLookupFunction;
14
15  constructor(lookup?: LookupFunction, collateralLookup?: CollateralLookupFunction) {
16    this.lookup = lookup ? lookup : AbstractFormula.DEFAULT_LOOKUP;
17    this.collateralLookup = collateralLookup
18      ? collateralLookup
19      : AbstractFormula.DEFAULT_COLLATERAL_LOOKUP;
20  }
21
22  protected static checkLength(actualLength: number, expectedLength: number, caller: FormulaName) {
23    if (actualLength != expectedLength) {
24      throw new NAException(
25        `Wrong number of arguments to ${caller}. Expected ${expectedLength} arguments, but got ${actualLength} arguments`
26      );
27    }
28  }
29
30  protected static checkAtLeastLength(
31    actualLength: number,
32    minExpectedLength: number,
33    caller: FormulaName
34  ) {
35    if (actualLength < minExpectedLength) {
36      throw new NAException(
37        `Wrong number of arguments to ${caller}. Expected at least ${minExpectedLength} arguments, but got ${actualLength} arguments`
38      );
39    }
40  }
41
42  /**
43   * Ensure that number of arguments is between a minmum and maximum, inclusively.
44   * @param actualLength - actual length of arguments.
45   * @param min - minimum expected length of arguments, inclusively
46   * @param max - maximum expected length of arguments, inclusively
47   * @param caller - NAME of formula being called.
48   */
49  protected static checkLengthBetween(
50    actualLength: number,
51    min: number,
52    max: number,
53    caller: FormulaName
54  ) {
55    if (actualLength < min || actualLength > max) {
56      throw new NAException(
57        `Wrong number of arguments to ${caller}. Expected between ${min} and ${max} arguments, but got ${actualLength} arguments`
58      );
59    }
60  }
61
62  /**
63   * Default lookup just passes value through.
64   * @param value - value to return.
65   * @constructor
66   */
67  private static DEFAULT_LOOKUP: LookupFunction = (value) => value;
68
69  /**
70   * Default collateral lookup just passes value through as well.
71   * @param origin - doesn't matter.
72   * @param value - value to return.
73   * @constructor
74   */
75  private static DEFAULT_COLLATERAL_LOOKUP: CollateralLookupFunction = (origin, value) => value;
76
77  /**
78   * This should be implemented by the actual formula.
79   * @param origin - of caller.
80   * @param values - arguments for formula.
81   */
82  internal(origin: SheetColumnRowKey, ...values: Array<Complex>): unknown {
83    return null;
84  }
85
86  /**
87   * Call internal, catching and returning error as value.
88   * @param origin - of caller.
89   * @param values - arguments for formula.
90   */
91  run(origin: SheetColumnRowKey, ...values: Array<Complex>): unknown {
92    try {
93      return this.internal(origin, ...values);
94    } catch (error) {
95      return error;
96    }
97  }
98}