[+] Typed array serialization

This commit is contained in:
2024-12-16 14:03:41 -05:00
parent a2e54e21ee
commit f2c6c9ee5d
+18 -12
View File
@@ -18,20 +18,26 @@ export const nStates = {
complete: 2 complete: 2
} }
export function sty(styles: any) { export const sty = (styles: any) => Object.entries(styles).map(([k, v]) => `${k}:${v}`).join(';')
return Object.entries(styles) export const pos = (x: number, y: number) => `top:${y}px;left:${x}px;`
.map(([key, value]) => `${key}:${value}`) export const randInt = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1)) + min
.join(';'); export const range = (n: number) => Array.from({length: n}, (_, i) => i)
}
export function pos(x: number, y: number) { // Json with TypedArray serialization
return `top:${y}px;left:${x}px;`; export const JsonTy = {
stringify: (obj: any) => JSON.stringify(obj, (_, v) => {
if (ArrayBuffer.isView(v)) {
// @ts-ignore Check if it's a TypedArray and serialize it
return { __typedArray__: true, type: v.constructor.name, data: Array.from(v) };
} }
return v; // For all other data types, keep it as is
}),
export function randInt(min: number, max: number) { parse: (json: string) => JSON.parse(json, (_, v) => {
return Math.floor(Math.random() * (max - min + 1)) + min; if (v && v.__typedArray__) {
// @ts-ignore Deserialize the TypedArray
return new globalThis[v.type](v.data);
} }
return v; // For all other data types, keep it as is
export function range(n: number) { })
return Array.from({length: n}, (_, i) => i);
} }