Created
May 8, 2022 07:00
-
-
Save guiseek/92b2d1bf8a379b711507c9ec944c15f2 to your computer and use it in GitHub Desktop.
Form to JSON Parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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