name:
src/main/js/formulas/statistical/COUNT.ts
-rw-r--r--
1267
1import { isNotNull } from "../../utils/Other";
2import { SheetColumnRowKey } from "../../models/common/SheetColumnRowKey";
3import { Complex } from "../../models/common/Types";
4import { Converters } from "../../utils/Converters";
5import { Counter } from "../../utils/Counter";
6import { Mappers } from "../../utils/Mappers";
7import { Predicates } from "../../utils/Predicates";
8import { Reducers } from "../../utils/Reducers";
9import { AbstractFormula } from "../AbstractFormula";
10import { FormulaName } from "../FormulaName";
11
12export class COUNT extends AbstractFormula {
13 static SELF: COUNT = new COUNT();
14 NAME = FormulaName.COUNT;
15
16 internal(origin: SheetColumnRowKey, ...values: Array<Complex>) {
17 AbstractFormula.checkAtLeastLength(values.length, 1, this.NAME);
18 const counter = new Counter<number>();
19 values
20 .map(this.lookup)
21 .map(Mappers.flattenGridsToArrays) // 1, 2, [null, 1, "0", "Text.", null]
22 .map(Mappers.ensureAllAreArrays) // [1], [2], [null, 1, "0", null]
23 .reduce(Reducers.join) // [1, 2, null, 1, "0", null]
24 .filter(isNotNull) // [1, 2, 1, "0"]
25 .filter(Predicates.isNumeric)
26 .map(Converters.toNumber)
27 .map((value) => counter.count(value)); // [1, 2, 1]
28 return counter.getCount();
29 }
30}