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
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"],
"and": [2, "boolean"],
"or": [2, "boolean"],
"not": [1, "boolean"],
"xor": [2, "boolean"],
"nand": [2, "boolean"],
"nor": [2, "boolean"],
"xnor": [2, "boolean"],
"true": [3, "string"],
"false": [3, "string"],
"if_empty": [1, "any"],
"length": [1, "string"],
"contains": [2, "string"],
"test": [2, "string"], // test whether a string contains a substring, the latter of which can be a regular expression
"match": [2, "string"],
"replace": [3, "string"],
"replaceAll": [3, "string"],
"lowercase": [1, "string"],
"uppercase": [1, "string"],
"repeat": [2, "string", "number"],
"strip_formatting": [1, "string"],
"min": ["infinite", "number"],
"max": ["infinite", "number"],
"absolute": [1, "number"],
"round": [1, "number"],
"ceiling": [1, "number"],
"floor": [1, "number"],
"squareroot": [1, "number"],
"cuberoot": [1, "number"],
"exponent": [2, "number"],
"ln": [1, "number"],
"log10": [1, "number"],
"log2": [1, "number"],
"now": [1, "number"], // receives UTC offset
"minute": [1, "date"],
"hour": [1, "date"],
"day": [1, "date"],
"date": [1, "date"],
"week": [1, "date"],
"month": [1, "date"],
"year": [1, "date"],
"dateAdd": [3, "date", "number", "string"],
"dateSubtract": [3, "date", "number", "string"],
"dateBetween": [3, "date", "date", "string"],
// 'dateRange': [3, 'date'],
// 'dateStart': [2, 'date'],
// 'dateEnd': [2, 'date'],
"timestamp": [1, "date"],
"fromTimestamp": [1, "number"],
"formatDate": [2, "string"],
"parseDate": [2, "string"],
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
syncretizm-formulaendpoint.web.val.run
v19
December 3, 2023