1
0
Fork 0
forked from 2sys/phonograph
phonograph/svelte/src/field.svelte.ts

76 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-08-10 14:32:15 -07:00
import { z } from "zod";
// -------- Encodable -------- //
const encodable_text_schema = z.object({
t: z.literal("Text"),
c: z.string().nullish().transform((x) => x ?? undefined),
});
const encodable_timestamp_schema = z.object({
t: z.literal("Timestamp"),
c: z.coerce.date().nullish().transform((x) => x ?? undefined),
});
const encodable_uuid_schema = z.object({
t: z.literal("Uuid"),
c: z.string().nullish().transform((x) => x ?? undefined),
});
const encodable_schema = z.union([
encodable_text_schema,
encodable_timestamp_schema,
encodable_uuid_schema,
]);
export type Encodable = z.infer<typeof encodable_schema>;
// -------- FieldType -------- //
const integer_field_type_schema = z.object({
t: z.literal("Integer"),
c: z.unknown(),
});
const text_field_type_schema = z.object({
t: "Text",
c: z.unknown(),
});
const uuid_field_type_schema = z.object({
t: "Uuid",
c: z.unknown(),
});
export const field_type_schema = z.union([
integer_field_type_schema,
text_field_type_schema,
uuid_field_type_schema,
]);
export type FieldType = z.infer<typeof field_type_schema>;
// -------- Field -------- //
export type Field = {
id: string;
name: string;
label?: string;
field_type: FieldType;
width_px: number;
};
// -------- Table Utils -------- //
// TODO move this to its own module
export type Coords = [number, number];
export type Row = {
key: string | number;
data: Encodable[];
};
export function coords_eq(a: Coords, b: Coords): boolean {
return a[0] === b[0] && a[1] === b[1];
}