gradio/frontend
1442k
1import type { CellValue } from "../types";2 3export type Headers = string[];4export type Data = CellValue[][];5 6export type Datatype =7 | "str"8 | "number"9 | "bool"10 | "date"11 | "markdown"12 | "html"13 | "image";14 15export type Metadata = {16 [key: string]: string[][] | null;17} | null;18export type HeadersWithIDs = { value: string; id: string }[];19export type DataframeValue = {20 data: Data;21 headers: Headers;22 metadata?: Metadata;23};24 25/**26 * Coerce a value to a given type.27 * @param v - The value to coerce.28 * @param t - The type to coerce to.29 * @returns The coerced value.30 */31export function cast_value_to_type(v: any, t: Datatype): CellValue {32 if (t === "number") {33 const n = Number(v);34 return isNaN(n) ? v : n;35 }36 if (t === "bool") {37 if (typeof v === "boolean") return v;38 if (typeof v === "number") return v !== 0;39 const s = String(v).toLowerCase();40 if (s === "true" || s === "1") return true;41 if (s === "false" || s === "0") return false;42 return v;43 }44 if (t === "date") {45 const d = new Date(v);46 return isNaN(d.getTime()) ? v : d.toISOString();47 }48 return v;49}50 51export interface EditData {52 index: number | [number, number];53 value: string;54 previous_value: string;55}56 