Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: minor type cleanup #619

Merged
merged 3 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading