Repository: honojs/hono
File: src/utils/body.ts (TypeScript)

Code before:

  return form as T
}

const handleParsingAllValues = (
  form: BodyData,
  key: string,
  value: FormDataEntryValue,
  dot?: boolean
): void => {
  if (dot && key.includes('.')) {
    handleNestedValues(form, key, value, dot, true)
  } else {
    if (form[key] !== undefined) {
      if (Array.isArray(form[key])) {
        ;(form[key] as (string | File)[]).push(value)
      } else {
        form[key] = [form[key] as string | File, value]
      }
    } else {
      form[key] = value
    }
  }
}

const handleNestedValues = (
  form: BodyData,
  key: string,
  value: FormDataEntryValue,
  dot?: boolean,
  parseAllValues?: boolean
): void => {
  if (dot && key.includes('.')) {
    let nestedForm = form
    const keys = key.split('.')

    keys.forEach((key, index) => {
      if (index === keys.length - 1) {
        if (parseAllValues) {
          if (nestedForm[key] !== undefined) {
            if (Array.isArray(nestedForm[key])) {
              ;(nestedForm[key] as (string | File)[]).push(value)
            } else {
              nestedForm[key] = [nestedForm[key] as string | File, value as string | File]
            }
          } else {
            nestedForm[key] = value
          }
        } else {
          nestedForm[key] = value
        }

Review comment (refactor):
And finally here

```ts
const handleParsingNestedValues = (
  form: BodyData,
  key: string,
  value: FormDataEntryValue,
  parseAllValues: boolean
): void => {
  let nestedForm = form
  const keys = key.split('.')

  keys.forEach((key, index) => {
    if (index === keys.length - 1) {
      if (parseAllValues) {
        handleParsingAllValues(nestedForm, key, value)
      } else {
        nestedForm[key] = value
      }
    } else {
      if (
        !nestedForm[key] ||
        typeof nestedForm[key] !== 'object' ||
        Array.isArray(nestedForm[key]) ||
        nestedForm[key] instanceof File
      ) {
        nestedForm[key] = Object.create(null)
      }
      nestedForm = nestedForm[key] as BodyData
    }
  })
}
```

We can improve this more by removing the `parseAllValues` param in this function and somehow abstracting the logic to assign the value back in the `convertFormDataToBodyData` loop or maybe something else. I didn't want to add a dynamic function to manage this, but it's up to you guys, we can go that way also

Code after:

/**
 * Converts form data to body data based on the provided options.
 *
 * @template T - The type of the parsed body data.
 * @param {FormData} formData - The form data to convert.
 * @param {ParseBodyOptions} options - Options for parsing the form data.
 * @returns {T} The converted body data.
 */
function convertFormDataToBodyData<T extends BodyData = BodyData>(
  formData: FormData,
  options: ParseBodyOptions
): T {
  const form: BodyData = Object.create(null)

  formData.forEach((value, key) => {
    const shouldParseAllValues = options.all || key.endsWith('[]')

    if (shouldParseAllValues) {
      handleParsingAllValues(form, key, value)
    } else {
      form[key] = value
    }
  })

  if (options.dot) {
    const nestedForm: BodyData = Object.create(null)

    Object.entries(form).forEach(([key, value]) => {
      const shouldParseDotValues = key.includes('.')

      if (shouldParseDotValues) {
        handleParsingNestedValues(nestedForm, key, value)
      } else {
        nestedForm[key] = value
      }
    })

    return nestedForm as T
  }

  return form as T
}

/**
 * Handles parsing all values for a given key, supporting multiple values as arrays.
 *
 * @param {BodyData} form - The form data object.
 * @param {string} key - The key to parse.
 * @param {FormDataEntryValue} value - The value to assign.
 */