From 536828b7a345c59407d6005859167f2f82d50fee Mon Sep 17 00:00:00 2001 From: Mohd Salman Date: Sat, 17 Feb 2024 15:16:42 +0530 Subject: [PATCH] feat: add js object to typescript converter (#392) --- pages/js-object-to-typescript.tsx | 74 +++++++++++++++++++++++++++++++ utils/routes.tsx | 7 ++- 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 pages/js-object-to-typescript.tsx diff --git a/pages/js-object-to-typescript.tsx b/pages/js-object-to-typescript.tsx new file mode 100644 index 0000000..2923be0 --- /dev/null +++ b/pages/js-object-to-typescript.tsx @@ -0,0 +1,74 @@ +import * as React from "react"; +import { useCallback } from "react"; +import ConversionPanel, { Transformer } from "@components/ConversionPanel"; +import { useSettings } from "@hooks/useSettings"; +import { EditorPanelProps } from "@components/EditorPanel"; +import { Settings } from "@constants/svgoConfig"; +import Form, { InputType } from "@components/Form"; + +const formFields = [ + { + type: InputType.SWITCH, + key: "typealias", + label: "Create Mono Type" + } +]; + +export default function JsObjectToTypescript() { + const name = "JS Object to Typescript"; + + const [settings, setSettings] = useSettings(name, { + typealias: false + }); + + const transformer = useCallback( + async ({ value }) => { + const result = JSON.stringify + (eval("(" + + value + + ")"), null, 2); + + const { run } = await import("json_typegen_wasm"); + + return run( + "Root", + result, + JSON.stringify({ + output_mode: settings.typealias + ? "typescript/typealias" + : "typescript" + }) + ); + }, + [settings] + ); + + const getSettingsElement = useCallback( + ({ open, toggle }) => { + return ( + + title={name} + onSubmit={setSettings} + open={open} + toggle={toggle} + formsFields={formFields} + initialValues={settings} + /> + ); + }, + [] + ); + + return ( + + ); +} diff --git a/utils/routes.tsx b/utils/routes.tsx index 2d1bb2d..cb19f16 100644 --- a/utils/routes.tsx +++ b/utils/routes.tsx @@ -217,7 +217,12 @@ export const categorizedRoutes = [ label: "to JSON", path: "/js-object-to-json", desc: "An online REPL for converting JS Object to JSON." - } + }, + { + label: "to Typescript", + path: "/js-object-to-typescript", + desc: "An online REPL for converting JS Object to Typescript." + }, ] }, {