Fork
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
export async function formulaEndpoint(req) {
if (req.method !== "GET") {
return new Response("Method Not Allowed", { status: 405 });
}
// PARSE URL
const params = new URLSearchParams(req.url.split("?")[1]);
// Extract the method and remove it from the parameters
const method = params.get("method");
params.delete("method");
// Initialize an object to store the variables
const vars = {};
// Iterate through the parameters
params.forEach((value, key) => {
if (value) {
vars[key] = value;
}
});
// now params is a dictionary of all the parameters that are not 'method'
// if params is empty, then we need to respond with an error.
if (Object.keys(vars).length === 0) {
return new Response("No parameters provided", { status: 400 });
}
// the params contain either numbers or strings, depending on the method passed.
// methods/functions include: add, subtract, multiply, mod, pow, divide, equal, =/=, >, <, >=, <=, and, or, not, xor, nand, nor, xnor, true, false, if_empty, length, contains, test, match, replace, replaceAll, lowercase, uppercase, repeat(#), strip_forma
// variable types include: number, string, boolean, date, array, object, null, undefined
// the return type is always a string.
// EXECUTE REQUEST
let result = null;
// DICTIONARY OF ALL FUNCTIONS, MAX NUMBER OF PARAMETERS, AND ACCEPTED TYPE
const functions = {
"add": ["infinite", "number"],
"subtract": ["infinite", "number"],
"multiply": ["infinite", "number"],
"mod": [2, "number"],
"pow": [2, "number"],
"divide": [2, "number"],
"equal": [2, "any"],
"!=": [2, "any"],
">": [2, "number"],
"<": [2, "number"],
">=": [2, "number"],
"<=": [2, "number"],
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.