-
Notifications
You must be signed in to change notification settings - Fork 115
/
utils.mjs
109 lines (96 loc) · 2.34 KB
/
utils.mjs
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import globals from './globals.mjs'
export function linspace(lo, hi, n) {
const step = (hi - lo) / (n - 1)
return Array.from({ length: n }, (_, i) => lo + step * i)
}
export function logspace(lo, hi, n) {
return linspace(lo, hi, n).map((x) => Math.pow(10, x))
}
export function isValidNumber(v) {
return typeof v === 'number' && !isNaN(v)
}
export function space(axis, range, n) {
const lo = range[0]
const hi = range[1]
if (axis.type === 'log') {
return logspace(Math.log10(lo), Math.log10(hi), n)
}
// default is linear
return linspace(lo, hi, n)
}
/**
* Creates `n` number of samples between `lo` and `hi` where consecutive
* numbers are bucketed in `nGroups` groups.
*/
export function spaceWithGroups(axis, lo, hi, n, nGroups) {
if (axis.type === 'log') {
lo = Math.log10(lo)
hi = Math.log10(hi)
}
const step = (hi - lo) / (n - 1)
const groups = []
const maxInGroup = Math.ceil(n / nGroups)
let group = []
groups.push(group)
for (let i = 0; i < n; i += 1) {
let v = lo + step * i
if (axis.type === 'log') {
v = Math.pow(10, v)
}
group.push(v)
if (group.length === maxInGroup && groups.length < nGroups) {
group = [v]
groups.push(group)
}
}
return groups
}
export function interval2dTypedArray(n, nGroups) {
const groups = []
for (let i = 0; i < nGroups; i += 1) {
groups.push(new Float32Array((n / nGroups) * 4))
}
return groups
}
export function getterSetter(config, option) {
const me = this
this[option] = function (value) {
if (!arguments.length) {
return config[option]
}
config[option] = value
return me
}
}
export function sgn(v) {
if (v < 0) {
return -1
}
if (v > 0) {
return 1
}
return 0
}
export function clamp(v, vMin, vMax) {
if (v < vMin) return vMin
if (v > vMax) return vMax
return v
}
export function color(data, index) {
const indexModLenColor = index % globals.COLORS.length
return data.color || globals.COLORS[indexModLenColor].formatHex()
}
/**
* infinity is a value that is close to Infinity but not Infinity, it can fit in a JS number.
*/
export function infinity() {
return 9007199254740991
}
/**
* asserts makes an simple assertion and throws `Error(message)` if the assertion failed
*/
export function assert(assertion, message) {
if (!assertion) {
throw new Error(message)
}
}