Skip to content

Commit 536828b

Browse files
feat: add js object to typescript converter (#392)
1 parent c6e0748 commit 536828b

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

pages/js-object-to-typescript.tsx

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import * as React from "react";
2+
import { useCallback } from "react";
3+
import ConversionPanel, { Transformer } from "@components/ConversionPanel";
4+
import { useSettings } from "@hooks/useSettings";
5+
import { EditorPanelProps } from "@components/EditorPanel";
6+
import { Settings } from "@constants/svgoConfig";
7+
import Form, { InputType } from "@components/Form";
8+
9+
const formFields = [
10+
{
11+
type: InputType.SWITCH,
12+
key: "typealias",
13+
label: "Create Mono Type"
14+
}
15+
];
16+
17+
export default function JsObjectToTypescript() {
18+
const name = "JS Object to Typescript";
19+
20+
const [settings, setSettings] = useSettings(name, {
21+
typealias: false
22+
});
23+
24+
const transformer = useCallback(
25+
async ({ value }) => {
26+
const result = JSON.stringify
27+
(eval("("
28+
+ value
29+
+ ")"), null, 2);
30+
31+
const { run } = await import("json_typegen_wasm");
32+
33+
return run(
34+
"Root",
35+
result,
36+
JSON.stringify({
37+
output_mode: settings.typealias
38+
? "typescript/typealias"
39+
: "typescript"
40+
})
41+
);
42+
},
43+
[settings]
44+
);
45+
46+
const getSettingsElement = useCallback<EditorPanelProps["settingElement"]>(
47+
({ open, toggle }) => {
48+
return (
49+
<Form<Settings>
50+
title={name}
51+
onSubmit={setSettings}
52+
open={open}
53+
toggle={toggle}
54+
formsFields={formFields}
55+
initialValues={settings}
56+
/>
57+
);
58+
},
59+
[]
60+
);
61+
62+
return (
63+
<ConversionPanel
64+
transformer={transformer}
65+
editorTitle="JS Object"
66+
editorDefaultValue="jsObject"
67+
editorLanguage="javascript"
68+
resultTitle="TypeScript"
69+
resultLanguage={"typescript"}
70+
editorSettingsElement={getSettingsElement}
71+
settings={settings}
72+
/>
73+
);
74+
}

utils/routes.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,12 @@ export const categorizedRoutes = [
217217
label: "to JSON",
218218
path: "/js-object-to-json",
219219
desc: "An online REPL for converting JS Object to JSON."
220-
}
220+
},
221+
{
222+
label: "to Typescript",
223+
path: "/js-object-to-typescript",
224+
desc: "An online REPL for converting JS Object to Typescript."
225+
},
221226
]
222227
},
223228
{

0 commit comments

Comments
 (0)