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/statistical/MIN.ts
-rw-r--r--
1201
 1import { isNotNull } from "../../utils/Other";
 2import { SheetColumnRowKey } from "../../models/common/SheetColumnRowKey";
 3import { Complex } from "../../models/common/Types";
 4import { Converters } from "../../utils/Converters";
 5import { Mappers } from "../../utils/Mappers";
 6import { Reducers } from "../../utils/Reducers";
 7import { AbstractFormula } from "../AbstractFormula";
 8import { FormulaName } from "../FormulaName";
 9
10export class MIN extends AbstractFormula {
11  static SELF: MIN = new MIN();
12  NAME = FormulaName.MIN;
13
14  internal(origin: SheetColumnRowKey, ...values: Array<Complex>) {
15    AbstractFormula.checkAtLeastLength(values.length, 1, this.NAME);
16    const constructedValues = values
17      .map(this.lookup)
18      .map(Mappers.flattenGridsToArrays) // 1, 2, [1, "0", "Nope.", null]
19      .map(Mappers.filterArrayValuesByIsCoercableToNumeric) // Eg: 1, 2, [1, "0"]
20      .map(Mappers.ensureAllAreArrays) // [1], [2], [1, "0"]
21      .reduce(Reducers.join) // [1, 2, 1, "0"]
22      .filter(isNotNull) // [1, 2, 1, "0"]
23      .map(Converters.toNumber); // [1, 2, 1, 0]
24    if (constructedValues.length === 0) {
25      return 0;
26    }
27    return constructedValues.reduce(Reducers.min);
28  }
29}