name:
src/main/js/formulas/statistical/MAX.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 MAX extends AbstractFormula {
11 static SELF: MAX = new MAX();
12 NAME = FormulaName.MAX;
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.max);
28 }
29}