1 | import { init } from '@vect/vector-init';
|
2 | import { max, min } from '@aryth/comparer';
|
3 | import { FUN } from '@typen/enum-data-types';
|
4 | import { nullish } from '@typen/nullish';
|
5 | import { acquire } from '@vect/vector-merge';
|
6 | import { MERGE, ACCUM, INCRE, COUNT, AVERAGE, MAX, MIN, FIRST, LAST } from '@analys/enum-pivot-mode';
|
7 |
|
8 | const ampliCell = function (side, banner) {
|
9 | return this.rows[arid.call(this, side)][acid.call(this, banner)];
|
10 | };
|
11 | const arid = function (x) {
|
12 | const ri = this.side.indexOf(x);
|
13 | if (ri >= 0) return ri;
|
14 | return this.rows.push(init(this.head.length, this.init)), ri + this.side.push(x);
|
15 | };
|
16 | const acid = function (y) {
|
17 | const ci = this.head.indexOf(y);
|
18 | if (ci >= 0) return ci;
|
19 | return this.rows.forEach(r => r.push(this.init())), ci + this.head.push(y);
|
20 | };
|
21 |
|
22 | const queryCell = function (x, y) {
|
23 | return (x = qrid.call(this, x)) >= 0 && (y = qcid.call(this, y)) >= 0 ? this.rows[x][y] : void 0;
|
24 | };
|
25 | const qrid = function (x) {
|
26 | return this.side.indexOf(x);
|
27 | };
|
28 | const qcid = function (y) {
|
29 | return this.head.indexOf(y);
|
30 | };
|
31 |
|
32 | const NaiveAccumulators = {
|
33 | merge: (target, value) => acquire(target, value),
|
34 | accum: (target, value) => (target.push(value), target),
|
35 | incre: (target, value) => target + value,
|
36 | count: (target, value) => target + 1,
|
37 | average: (target, value) => (target.sum += value, target.count += 1, target),
|
38 | max: (target, value) => max(target, value),
|
39 | min: (target, value) => min(target, value),
|
40 | first: (target, value) => target !== null && target !== void 0 ? target : value,
|
41 | last: (target, value) => value !== null && value !== void 0 ? value : target
|
42 | };
|
43 | const Accumulators = {
|
44 | merge: (target, value) => nullish(value) ? target : acquire(target, value),
|
45 | accum: (target, value) => nullish(value) ? target : (target.push(value), target),
|
46 | incre: (target, value) => nullish(value) ? target : target + value,
|
47 | count: (target, value) => nullish(value) ? target : target + 1,
|
48 | average: (target, value) => nullish(value) ? target : (target.sum += value, target.count += 1, target),
|
49 | max: (target, value) => nullish(value) ? target : max(target, value),
|
50 | min: (target, value) => nullish(value) ? target : min(target, value),
|
51 | first: (target, value) => target !== null && target !== void 0 ? target : value,
|
52 | last: (target, value) => value !== null && value !== void 0 ? value : target
|
53 | };
|
54 | const modeToTally = mode => {
|
55 | let accumulators = Accumulators;
|
56 |
|
57 | if (Array.isArray(mode)) {
|
58 | var _mode$;
|
59 |
|
60 | accumulators = (_mode$ = mode[1]) !== null && _mode$ !== void 0 ? _mode$ : Accumulators;
|
61 | mode = mode[0];
|
62 | }
|
63 |
|
64 | if (typeof mode === FUN) return mode;
|
65 | if (mode in accumulators) return accumulators[mode];
|
66 | return () => {};
|
67 | };
|
68 |
|
69 | const modeToInit = mode => {
|
70 | if (Array.isArray(mode)) mode = mode[0];
|
71 | if (mode === MERGE || mode === ACCUM) return () => [];
|
72 | if (mode === INCRE || mode === COUNT) return () => 0;
|
73 | if (mode === AVERAGE) return () => ({
|
74 | sum: 0,
|
75 | count: 0,
|
76 |
|
77 | get value() {
|
78 | return this.sum / this.count;
|
79 | }
|
80 |
|
81 | });
|
82 | if (mode === MAX) return () => Number.NEGATIVE_INFINITY;
|
83 | if (mode === MIN) return () => Number.POSITIVE_INFINITY;
|
84 | if (mode === FIRST || mode === LAST) return () => void 0;
|
85 | return () => 0;
|
86 | };
|
87 |
|
88 | export { Accumulators, NaiveAccumulators, acid, ampliCell, arid, modeToInit, modeToTally, qcid, qrid, queryCell };
|