-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (59 loc) · 1.67 KB
/
index.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
var order = ['covariance', 'std', 'variance', 'mean'] // Order of functions. Last - first
module.exports = function Series () {
var stats
var values = {}
if (arguments.length > 1) {
// Convert Array-like arguments to real array
stats = Array.prototype.slice.call(arguments)
// Sort arguments by level of details
stats.sort(function (a, b) {
return order.indexOf(b.name) - order.indexOf(a.name)
})
} else if (arguments.length === 1) {
// Only one argument provided - maybe array of objects?
if (Array.isArray(arguments[0])) {
stats = arguments[0]
} else {
stats = [arguments[0]]
}
} else {
throw new Error('No function arguments')
}
var series = function series () {
// Check if arguments exist
if (arguments.length) {
// Track n updates
var isCounterUpdated = false
// Values pushed to series object:
var args = arguments
// Add series results to sub-functions arguments
Array.prototype.push.call(args, values)
// Iterate over all series functions
stats.forEach(function (s) {
var stat
var name
if (typeof s === 'object') {
// Detailed stat object
stat = s.stat
name = (s.name && s.name.length) ? s.name : s.stat.name
} else {
// Stat function
stat = s
name = s.name
}
values[name] = stat.apply(null, args)
if (!isCounterUpdated) {
values.n = stat.n
isCounterUpdated = true
}
})
}
return values
}
Object.defineProperty(series, 'values', {
get: function () {
return values
}
})
return series
}