UNPKG

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