UNPKG

1.93 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.sum = sum;
7exports.max = max;
8exports.min = min;
9exports.mean = mean;
10exports.mode = mode;
11exports.getColumn = getColumn;
12exports.getSatByColumn = getSatByColumn;
13exports.statMap = void 0;
14
15function max(x) {
16 if (x.length === 0) {
17 throw new Error('max requires at least one data point');
18 }
19
20 var value = x[0];
21
22 for (var i = 1; i < x.length; i++) {
23 if (x[i] > value) {
24 value = x[i];
25 }
26 }
27
28 return value * 1;
29}
30
31function min(x) {
32 if (x.length === 0) {
33 throw new Error('min requires at least one data point');
34 }
35
36 var value = x[0];
37
38 for (var i = 1; i < x.length; i++) {
39 if (x[i] < value) {
40 value = x[i];
41 }
42 }
43
44 return value * 1;
45}
46
47function sum(x) {
48 if (x.length === 0) {
49 return 0;
50 }
51
52 var sumNum = x[0] * 1;
53
54 for (var i = 1; i < x.length; i++) {
55 sumNum += x[i] * 1;
56 }
57
58 return sumNum;
59}
60
61function mean(x) {
62 if (x.length === 0) {
63 throw new Error('mean requires at least one data point');
64 }
65
66 return sum(x) / x.length;
67}
68
69function mode(x) {
70 if (x.length === 0) {
71 throw new Error('mean requires at least one data point');
72 }
73
74 if (x.length < 3) {
75 return x[0];
76 }
77
78 x.sort();
79 var last = x[0];
80 var value = NaN;
81 var maxSeen = 0;
82 var seenThis = 1;
83
84 for (var i = 1; i < x.length + 1; i++) {
85 if (x[i] !== last) {
86 if (seenThis > maxSeen) {
87 maxSeen = seenThis;
88 value = last;
89 }
90
91 seenThis = 1;
92 last = x[i];
93 } else {
94 seenThis++;
95 }
96 }
97
98 return value * 1;
99}
100
101var statMap = {
102 min: min,
103 max: max,
104 mean: mean,
105 sum: sum,
106 mode: mode
107};
108exports.statMap = statMap;
109
110function getColumn(data, columnName) {
111 return data.map(function (item) {
112 return item[columnName];
113 });
114}
115
116function getSatByColumn(type, column) {
117 return statMap[type](column);
118}
119//# sourceMappingURL=statistics.js.map
\No newline at end of file