You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.3 KiB
TypeScript

export const store: Record<string, any> = {};
export const _rt = {
add: (a: number) => (b: number) => a + b,
sub: (a: number) => (b: number) => a - b,
mul: (a: number) => (b: number) => a * b,
div: (a: number) => (b: number) => a / b,
mod: (a: number) => (b: number) => a % b,
cat: (a: string) => (b: string) => a + b,
max: (a: number) => (b: number) => Math.max(a, b),
min: (a: number) => (b: number) => Math.min(a, b),
eq: (a: any) => (b: any) => ({ _tag: a === b ? 'True' : 'False' }),
neq: (a: any) => (b: any) => ({ _tag: a !== b ? 'True' : 'False' }),
gt: (a: any) => (b: any) => ({ _tag: a > b ? 'True' : 'False' }),
lt: (a: any) => (b: any) => ({ _tag: a < b ? 'True' : 'False' }),
gte: (a: any) => (b: any) => ({ _tag: a >= b ? 'True' : 'False' }),
lte: (a: any) => (b: any) => ({ _tag: a <= b ? 'True' : 'False' }),
len: (list: any[]) => list.length,
str: (x: any) => String(x),
slice: (s: string | any[]) => (start: number) => (end: number) => s.slice(start, end),
debug: (label: string) => (value: any) => { console.log(label, value); return value; },
measureText: (text: string) => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
if (ctx) {
ctx.font = '16px "SF Mono", "Monaco", "Menlo", monospace';
return Math.floor(ctx.measureText(text).width);
}
return text.length * 10; // fallback
},
storeSearch: (query: string) => {
const results: string[] = [];
const searchTerm = query.toLowerCase();
console.log("in storeSearch. query: ", searchTerm);
for (const name of Object.keys(store)) {
if (searchTerm === '' || name.toLowerCase().includes(searchTerm)) {
results.push(name);
}
}
return results;
},
rebind: (name: string, pathOrValue: any, maybeValue?: any) => {
if (maybeValue === undefined) {
store[name] = pathOrValue;
} else {
const path = pathOrValue as string[];
let obj = store[name];
for (let i = 0; i < path.length - 1; i++) {
obj = obj[path[i]];
}
obj[path[path.length - 1]] = maybeValue;
}
},
}