/
aggregations.js
executable file
路80 lines (63 loc) 路 1.37 KB
/
aggregations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
export function sum(values, aggregatedValues) {
// It's faster to just add the aggregations together instead of
// process leaf nodes individually
return aggregatedValues.reduce(
(sum, next) => sum + (typeof next === 'number' ? next : 0),
0
)
}
export function min(values) {
let min = 0
values.forEach(value => {
if (typeof value === 'number') {
min = Math.min(min, value)
}
})
return min
}
export function max(values) {
let max = 0
values.forEach(value => {
if (typeof value === 'number') {
max = Math.max(max, value)
}
})
return max
}
export function minMax(values) {
let min = 0
let max = 0
values.forEach(value => {
if (typeof value === 'number') {
min = Math.min(min, value)
max = Math.max(max, value)
}
})
return `${min}..${max}`
}
export function average(values) {
return sum(null, values) / values.length
}
export function median(values) {
if (!values.length) {
return null
}
let min = 0
let max = 0
values.forEach(value => {
if (typeof value === 'number') {
min = Math.min(min, value)
max = Math.max(max, value)
}
})
return (min + max) / 2
}
export function unique(values) {
return [...new Set(values).values()]
}
export function uniqueCount(values) {
return new Set(values).size
}
export function count(values) {
return values.length
}