-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformula-functions.svelte.js
138 lines (124 loc) · 3.81 KB
/
formula-functions.svelte.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { debounce } from "./helpers.js";
// Import required so code that is `eval`ed can modify formula functions and
// use parser combinators. Do not remove, even though it appears "unused."
import * as parsers from "./parsers.js";
import * as classes from "./classes.svelte.js";
import * as compression from "./compress.js";
import { undefinedArgsToIdentity } from "./helpers.js";
export let functions = $state({});
export function evalCode(code, ret = () => {}) {
if (code == null) {
return ret();
}
// No op that "uses" the imports so tree shaking doesn't consider them dead
// code. This usage must occur here – the imported objects are unavailable in
// the code if this is moved elsewhere. Lots of other no op usages of imported
// objects are eliminated by vite, but empirically, this one seems to stay in
// the final build.
try {
throw parsers;
} catch {}
try {
throw classes;
} catch {}
try {
throw compression;
} catch {}
try {
eval(
code +
// Allows user code to show up in the devtools debugger as "user-code.js"
"\n//# sourceURL=user-code.js",
);
return ret();
} catch (e) {
return ret(e);
}
}
export const evalDebounced = debounce(evalCode, 500);
// All JavaScript Math functions are available as formula functions
Object.getOwnPropertyNames(Math)
.filter((n) => typeof Math[n] === "function")
.forEach((n) => (functions[n] = undefinedArgsToIdentity(Math[n])));
// Core functions
functions.sum = undefinedArgsToIdentity((...args) =>
args.flat(Infinity).reduce((i, j) => i + j, 0),
);
functions.prod = undefinedArgsToIdentity((...args) =>
args.flat(Infinity).reduce((i, j) => i * j, 1),
);
functions.avg = undefinedArgsToIdentity(
(...args) =>
args.flat(Infinity).reduce((i, j) => i + j) / args.flat(Infinity).length,
);
functions.randint = (n) => Math.floor(Math.random() * n);
functions.if = (x, yes, no) => (x ? yes : no);
// Aliases
functions.add = functions.sum;
functions.plus = functions.sum;
functions.times = functions.prod;
functions.product = functions.prod;
functions.mult = functions.prod;
functions.average = functions.avg;
functions.rand = functions.random;
// Miscellaneous utility functions
functions.slider = function slider(min, max, step, value) {
this.update((previous) => {
value = value ?? previous ?? 0;
});
this.element = Object.assign(document.createElement("input"), {
min,
max,
step,
value,
type: "range",
style: `width: 100%;
appearance: auto;
margin: 0 0.5ch;`,
oninput: (e) => this.set(Number(e.target.value)),
});
return value;
};
functions.bold = function (s) {
this.style += "font-weight: bold;";
return s;
};
functions.center = function (s) {
this.style += "text-align: center;";
return s;
};
const dollarFormat = Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
});
functions.dollars = undefinedArgsToIdentity(function (d) {
this.element = document.createTextNode(dollarFormat.format(d));
return d;
});
functions.sparkbars = (...args) => {
const lines = "▁▂▃▄▅▆▇█";
const min = Math.min(...args),
max = Math.max(...args);
const bucketSize = (max - min) / (lines.length - 1);
return args.map((x) => lines[Math.floor((x - min) / bucketSize)]).join("");
};
functions.checkbox = function (label) {
let value;
this.update((previous) => {
value = !!previous;
return !!previous;
});
this.element = Object.assign(document.createElement("label"), {
innerText: label,
style: "display: flex; align-items: center; gap: 1ch; margin: 0 0.5em;",
});
this.element.appendChild(
Object.assign(document.createElement("input"), {
type: "checkbox",
style: "appearance: auto;",
checked: value,
oninput: (e) => this.set(e.target.checked),
}),
);
return value;
};