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/math/MOD.ts
-rw-r--r--
1216
 1import { DivException } from "../../errors/DivException";
 2import { F7Exception } from "../../errors/F7Exception";
 3import { SheetColumnRowKey } from "../../models/common/SheetColumnRowKey";
 4import { Complex } from "../../models/common/Types";
 5import { Converters } from "../../utils/Converters";
 6import { Numbers } from "../../utils/Numbers";
 7import { AbstractFormula } from "../AbstractFormula";
 8import { FormulaName } from "../FormulaName";
 9
10export class MOD extends AbstractFormula {
11  static SELF: MOD = new MOD();
12  NAME = FormulaName.MOD;
13
14  internal(origin: SheetColumnRowKey, ...values: Array<Complex>) {
15    AbstractFormula.checkLength(values.length, 2, this.NAME);
16    const first = Converters.first(this.collateralLookup(origin, values[0]));
17    const second = Converters.first(this.collateralLookup(origin, values[1]));
18    if (first instanceof F7Exception) {
19      return first;
20    }
21    if (second instanceof F7Exception) {
22      return second;
23    }
24    const firstNumber = Converters.toNumber(first);
25    const secondNumber = Converters.toNumber(second);
26    if (Numbers.isZero(secondNumber)) {
27      return new DivException("MOD parameter 2 cannot be zero.");
28    }
29    return first % second;
30  }
31}