From 91af7851851f1dba7dc09d6d37871fcc1235da57 Mon Sep 17 00:00:00 2001 From: Aaron Date: Sat, 11 May 2024 17:00:43 +0700 Subject: [PATCH] refactor: minor type cleanup (#619) Co-authored-by: Edmund Hung --- packages/conform-dom/formdata.ts | 60 +++++++----------------------- packages/conform-dom/submission.ts | 11 +----- 2 files changed, 16 insertions(+), 55 deletions(-) diff --git a/packages/conform-dom/formdata.ts b/packages/conform-dom/formdata.ts index 7cf18a37..154c8193 100644 --- a/packages/conform-dom/formdata.ts +++ b/packages/conform-dom/formdata.ts @@ -249,67 +249,35 @@ export function normalize< * Flatten a tree into a dictionary */ export function flatten( - data: Record | Array | undefined, - options?: { - resolve?: (data: unknown) => unknown | null; + data: unknown, + options: { + resolve?: (data: unknown) => unknown; prefix?: string; - }, + } = {}, ): Record { const result: Record = {}; - const resolve = options?.resolve ?? ((data) => data); + const resolve = options.resolve ?? ((data) => data); - function setResult(data: unknown, name: string) { + function process(data: unknown, prefix: string) { const value = normalize(resolve(data)); if (typeof value !== 'undefined') { - result[name] = value; + result[prefix] = value; } - } - function processObject( - obj: Record, - prefix: string, - ): void { - setResult(obj, prefix); - - for (const [key, value] of Object.entries(obj)) { - const name = prefix ? `${prefix}.${key}` : key; - - if (Array.isArray(value)) { - processArray(value, name); - } else if (value && isPlainObject(value)) { - processObject(value, name); - } else { - setResult(value, name); + if (Array.isArray(data)) { + for (let i = 0; i < data.length; i++) { + process(data[i], `${prefix}[${i}]`); } - } - } - - function processArray(array: Array, prefix: string): void { - setResult(array, prefix); - - for (let i = 0; i < array.length; i++) { - const item = array[i]; - const name = `${prefix}[${i}]`; - - if (Array.isArray(item)) { - processArray(item, name); - } else if (item && isPlainObject(item)) { - processObject(item, name); - } else { - setResult(item, name); + } else if (isPlainObject(data)) { + for (const [key, value] of Object.entries(data)) { + process(value, prefix ? `${prefix}.${key}` : key); } } } if (data) { - const prefix = options?.prefix ?? ''; - - if (Array.isArray(data)) { - processArray(data, prefix); - } else { - processObject(data, prefix); - } + process(data, options.prefix ?? ''); } return result; diff --git a/packages/conform-dom/submission.ts b/packages/conform-dom/submission.ts index 3b6790d5..8ea699bc 100644 --- a/packages/conform-dom/submission.ts +++ b/packages/conform-dom/submission.ts @@ -482,7 +482,6 @@ export function setState( Object.assign( state, - // @ts-expect-error FIXME flatten should be more flexible flatten(result, { resolve(data) { if (isPlainObject(data) || Array.isArray(data)) { @@ -524,14 +523,11 @@ export function setListState( }); } -export function serialize( - defaultValue: DefaultValue, -): FormValue { +export function serialize(defaultValue: Schema): FormValue { if (isPlainObject(defaultValue)) { // @ts-expect-error FIXME return Object.entries(defaultValue).reduce>( (result, [key, value]) => { - // @ts-ignore-error FIXME result[key] = serialize(value); return result; }, @@ -540,10 +536,7 @@ export function serialize( } else if (Array.isArray(defaultValue)) { // @ts-expect-error FIXME return defaultValue.map(serialize); - } else if ( - // @ts-ignore-error FIXME - defaultValue instanceof Date - ) { + } else if (defaultValue instanceof Date) { // @ts-expect-error FIXME return defaultValue.toISOString(); } else if (typeof defaultValue === 'boolean') {