-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformula-functions.svelte.js
More file actions
193 lines (178 loc) · 5.14 KB
/
Copy pathformula-functions.svelte.js
File metadata and controls
193 lines (178 loc) · 5.14 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { debounce } from "./lib/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 classes from "./classes.svelte.js";
import * as parsers from "./lib/parsers.js";
import * as compression from "./lib/compress.js";
import { llmToolFunctions, llmModels } from "./llm.svelte.js";
import { undefinedArgsToIdentity } from "./lib/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 {
throw llmToolFunctions;
} catch {}
try {
throw llmModels;
} catch {}
try {
eval(
code +
// Allows user code to show up in the devtools debugger as "user-code.js"
"\n//# sourceURL=user-code.js",
);
// Canonicalize function names, since only lowercased version is used
Object.entries(functions).forEach(([name, f]) => {
functions[name.toLocaleLowerCase()] = f;
});
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) => {
if (previous != null && typeof previous !== "string") {
value = previous;
} else {
value = value ?? 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) {
const e = this.element;
this.element = Object.assign(document.createElement("div"), {
style: `
display: flex;
flex-direction: column;
flex-wrap: nowrap;
justify-content: center;
align-items: center;
text-align: center;
width: 100%;
height: 100%;
min-width: 0;
min-height: 0;
`,
});
this.element.appendChild(e ?? document.createTextNode(s));
return s;
};
const dollarFormat = Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
});
functions.dollars = undefinedArgsToIdentity(function (d, return_str) {
const str = dollarFormat.format(d);
if (return_str) {
return str;
}
this.element = document.createTextNode(str);
return d;
});
const percentFormat = new Intl.NumberFormat("en-US", {
style: "percent",
maximumFractionDigits: 4,
});
functions.percent = undefinedArgsToIdentity(function (p, return_str) {
const str = percentFormat.format(p);
if (return_str) {
return str;
}
this.element = document.createTextNode(str);
return p;
});
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 value;
});
const e = this.element;
this.element = Object.assign(document.createElement("label"), {
style: `
display: flex;
justify-content: center;
align-items: center;
gap: 1ch;
margin: 0 0.5em;
width: 100%;
`,
});
this.element.appendChild(
Object.assign(document.createElement("input"), {
type: "checkbox",
style: "appearance: auto;",
checked: value,
oninput: (e) => this.set(e.target.checked),
}),
);
this.element.appendChild(e ?? document.createTextNode(label));
return value;
};