diff --git a/pages/index.js b/pages/index.js index 06260b6..9565965 100644 --- a/pages/index.js +++ b/pages/index.js @@ -4,6 +4,8 @@ import immutablePropsPlugin from "../utils/babel-plugin-js-to-immutable-prop-typ import Layout from "../components/Layout"; import { transform } from "babel-standalone"; import defaultText from "../utils/dummy-json"; +import { toValidJSON } from "../utils/json-to-ts-flow"; +import merge from "lodash/merge" import ConversionPanel from "../components/ConversionPanel"; @@ -13,9 +15,13 @@ export default class Main extends PureComponent { }; getTransformedValue = newValue => { - newValue = "const propTypes = " + newValue; + let x = JSON.parse(toValidJSON(newValue)) + if (typeof x !== "object" || Array.isArray(x)) { + x = merge({}, ...x) + } + x = "const propTypes = " + JSON.stringify(x); const plugin = this.state.isImmutable ? immutablePropsPlugin : propsPlugin; - const { code } = transform(newValue, { + const { code } = transform(x, { presets: ["es2015"], plugins: [plugin] }); diff --git a/pages/json-to-flow-types.js b/pages/json-to-flow-types.js index 3c7d261..2b8f5d0 100644 --- a/pages/json-to-flow-types.js +++ b/pages/json-to-flow-types.js @@ -4,15 +4,14 @@ import { Json2dts, toValidJSON } from "../utils/json-to-ts-flow"; import Layout from "../components/Layout"; import ConversionPanel from "../components/ConversionPanel"; import defaultText from "../utils/dummy-json"; +import merge from "lodash/merge"; export default class Json2Ts extends PureComponent { getTransformedValue = newValue => { const converter = new Json2dts(); let text2Obj = JSON.parse(toValidJSON(newValue)); if (typeof text2Obj !== "object" || Array.isArray(text2Obj)) { - throw new Error( - "Pass a valid JSON Object. Arrays are not acceptable even though they are valid JSON." - ); + text2Obj = merge({}, ...text2Obj) } converter.parse(text2Obj, "props"); return converter.getCode(true); diff --git a/pages/json-to-rust-serde.js b/pages/json-to-rust-serde.js index 6019eaa..a4506a7 100644 --- a/pages/json-to-rust-serde.js +++ b/pages/json-to-rust-serde.js @@ -3,15 +3,13 @@ import { Json2dts, toValidJSON } from "../utils/json-to-rust-serde"; import Layout from "../components/Layout"; import ConversionPanel from "../components/ConversionPanel"; import defaultText from "../utils/dummy-json"; - +import merge from "lodash/merge" export default class Json2Ts extends PureComponent { getTransformedValue = newValue => { const converter = new Json2dts(); let text2Obj = JSON.parse(toValidJSON(newValue)); if (typeof text2Obj !== "object" || Array.isArray(text2Obj)) { - throw new Error( - "Pass a valid JSON Object. Arrays are not acceptable even though they are valid JSON." - ); + text2Obj = merge({}, ...text2Obj) } converter.parse(text2Obj, "RootJson"); diff --git a/pages/json-to-scala-case-class.js b/pages/json-to-scala-case-class.js index 4019b8b..837439d 100644 --- a/pages/json-to-scala-case-class.js +++ b/pages/json-to-scala-case-class.js @@ -3,15 +3,14 @@ import { Json2dts, toValidJSON } from "../utils/json-to-scala-case-class"; import Layout from "../components/Layout"; import ConversionPanel from "../components/ConversionPanel"; import defaultText from "../utils/dummy-json"; +import merge from "lodash/merge" export default class Json2Ts extends PureComponent { getTransformedValue = newValue => { const converter = new Json2dts(); let text2Obj = JSON.parse(toValidJSON(newValue)); if (typeof text2Obj !== "object" || Array.isArray(text2Obj)) { - throw new Error( - "Pass a valid JSON Object. Arrays are not acceptable even though they are valid JSON." - ); + text2Obj = merge({}, ...text2Obj) } converter.parse(text2Obj, "RootJson"); diff --git a/pages/json-to-ts-interface.js b/pages/json-to-ts-interface.js index 7f60ef0..d0bd3bd 100644 --- a/pages/json-to-ts-interface.js +++ b/pages/json-to-ts-interface.js @@ -4,15 +4,14 @@ import { Json2dts, toValidJSON } from "../utils/json-to-ts-flow"; import Layout from "../components/Layout"; import ConversionPanel from "../components/ConversionPanel"; import defaultText from "../utils/dummy-json"; +import merge from "lodash/merge" export default class Json2Ts extends PureComponent { getTransformedValue = newValue => { const converter = new Json2dts(); let text2Obj = JSON.parse(toValidJSON(newValue)); if (typeof text2Obj !== "object" || Array.isArray(text2Obj)) { - throw new Error( - "Pass a valid JSON Object. Arrays are not acceptable even though they are valid JSON." - ); + text2Obj = merge({}, ...text2Obj) } converter.parse(text2Obj, "RootJson"); return converter.getCode();