31 lines
1008 B
TypeScript
31 lines
1008 B
TypeScript
|
|
// Helpers for the FormField.options column, which is stored as a JSON-encoded
|
||
|
|
// string in SQLite (no native scalar lists). The client/API contract keeps
|
||
|
|
// `options` as a real string[] — these convert at the DB boundary.
|
||
|
|
|
||
|
|
export function parseOptions(raw: unknown): string[] {
|
||
|
|
if (Array.isArray(raw)) return raw as string[]
|
||
|
|
if (typeof raw !== 'string' || raw.length === 0) return []
|
||
|
|
try {
|
||
|
|
const v = JSON.parse(raw)
|
||
|
|
return Array.isArray(v) ? v.map(String) : []
|
||
|
|
} catch {
|
||
|
|
return []
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function serializeOptions(opts: unknown): string {
|
||
|
|
return JSON.stringify(Array.isArray(opts) ? opts : [])
|
||
|
|
}
|
||
|
|
|
||
|
|
// Returns a shallow copy of a form with each field's `options` parsed to string[].
|
||
|
|
export function withParsedFields<T extends { fields?: any[] | null }>(form: T | null): T | null {
|
||
|
|
if (!form) return form
|
||
|
|
if (Array.isArray(form.fields)) {
|
||
|
|
return {
|
||
|
|
...form,
|
||
|
|
fields: form.fields.map(f => ({ ...f, options: parseOptions(f.options) })),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return form
|
||
|
|
}
|