Skip to content

Instantly share code, notes, and snippets.

@guiseek
Created May 8, 2022 07:00
Show Gist options
  • Save guiseek/92b2d1bf8a379b711507c9ec944c15f2 to your computer and use it in GitHub Desktop.
Save guiseek/92b2d1bf8a379b711507c9ec944c15f2 to your computer and use it in GitHub Desktop.
Form to JSON Parser
export function getForrmAsJson<R>(form: HTMLFormElement) {
const data = new FormData(form)
const value = Object.fromEntries(data.entries()) as Record<string, any>
Object.keys(value).forEach((key) => {
value[key] = !isNaN(+value[key]) ? +value[key] : value[key]
})
return value as R
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment