-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
55 lines (46 loc) · 1.44 KB
/
helper.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
// tiny helper function
// single digits are formatted with one preceding zero
function AddZero(num) {
return (num >= 0 && num < 10) ? "0" + num : num + "";
}
// DD MM YYYY Format (!)
function getDate(separator="-") {
let now = new Date()
return [
now.getFullYear(),
AddZero(now.getMonth() + 1),
AddZero(now.getDate()),
].join(separator)
}
function getTime(separator=":") {
let now = new Date()
return [
AddZero(now.getHours()),
AddZero(now.getMinutes()),
AddZero(now.getSeconds()),
].join(separator)
}
// hide cursor global
function hideCursor() {
let body = document.getElementById("experiment-body")
body.style.cursor = "none"
}
// show cursor globally
function showCursor() {
let body = document.getElementById("experiment-body")
body.style.cursor = ""
}
// implement tiny assert method
// source and additional information (https://stackoverflow.com/questions/15313418/what-is-assert-in-javascript)
function assert(condition, message) {
if (!condition) {
throw new Error(message || "Assertion failed");
}
}
// custom, specific rounding function
// works like Math.round by default
function roundTo(number, digitsAfterDot=0) {
assert(digitsAfterDot >= 0, "Rounding to a negative amount of digits after decimal dot is not possible");
let roundingFactor = 10 ** digitsAfterDot;
return Math.round(number * roundingFactor) / roundingFactor;
}