Skip to content

Commit

Permalink
refactor: minor type cleanup (#619)
Browse files Browse the repository at this point in the history
Co-authored-by: Edmund Hung <me@edmund.dev>
  • Loading branch information
lifeiscontent and edmundhung authored May 11, 2024
1 parent 4aab4cb commit 91af785
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 55 deletions.
60 changes: 14 additions & 46 deletions packages/conform-dom/formdata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,67 +249,35 @@ export function normalize<
* Flatten a tree into a dictionary
*/
export function flatten(
data: Record<string | number | symbol, unknown> | Array<unknown> | undefined,
options?: {
resolve?: (data: unknown) => unknown | null;
data: unknown,
options: {
resolve?: (data: unknown) => unknown;
prefix?: string;
},
} = {},
): Record<string, unknown> {
const result: Record<string, unknown> = {};
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<string | number | symbol, unknown>,
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<unknown>, 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;
Expand Down
11 changes: 2 additions & 9 deletions packages/conform-dom/submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -524,14 +523,11 @@ export function setListState(
});
}

export function serialize<Schema>(
defaultValue: DefaultValue<Schema>,
): FormValue<Schema> {
export function serialize<Schema>(defaultValue: Schema): FormValue<Schema> {
if (isPlainObject(defaultValue)) {
// @ts-expect-error FIXME
return Object.entries(defaultValue).reduce<Record<string, unknown>>(
(result, [key, value]) => {
// @ts-ignore-error FIXME
result[key] = serialize(value);
return result;
},
Expand All @@ -540,10 +536,7 @@ export function serialize<Schema>(
} 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') {
Expand Down

0 comments on commit 91af785

Please sign in to comment.