diff --git a/.all-contributorsrc b/.all-contributorsrc index 1e73351..2875795 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -33,6 +33,15 @@ "contributions": [ "code" ] + }, + { + "login": "mattiamanzati", + "name": "Mattia Manzati", + "avatar_url": "https://avatars1.githubusercontent.com/u/13787614?v=4", + "profile": "https://github.com/mattiamanzati", + "contributions": [ + "code" + ] } ] } diff --git a/components/Layout.js b/components/Layout.js index ea8e6fe..6f93ae3 100644 --- a/components/Layout.js +++ b/components/Layout.js @@ -192,6 +192,11 @@ export default function({ children, pathname }) { JSON to Typescript Interface +
  • + + JSON to MobX-State-Tree Model + +
  • CSS to JS Objects diff --git a/pages/json-to-mobx-state-tree.js b/pages/json-to-mobx-state-tree.js new file mode 100644 index 0000000..9044640 --- /dev/null +++ b/pages/json-to-mobx-state-tree.js @@ -0,0 +1,34 @@ +import React, { PureComponent } from "react"; +import plugin from "../utils/babel-plugin-js-to-mobx-state-tree"; +import Layout from "../components/Layout"; +import { transform } from "babel-standalone"; +import defaultText from "../utils/dummy-json"; + +import ConversionPanel from "../components/ConversionPanel"; + +export default class Main extends PureComponent { + getTransformedValue = newValue => { + newValue = "const MyModel = " + newValue; + const { code } = transform(newValue, { + presets: ["es2015"], + plugins: [plugin] + }); + return code; + }; + + render() { + return ( + + + + ); + } +} diff --git a/readme.md b/readme.md index c895140..628fe3f 100644 --- a/readme.md +++ b/readme.md @@ -19,8 +19,8 @@ MIT @ Ritesh Kumar Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)): -| [
    Florian Gilcher](http://asquera.de)
    [💻](https://github.com/ritz078/transform-www/commits?author=skade "Code") | [
    Pascal Hertleif](http://pascalhertleif.de/)
    [💻](https://github.com/ritz078/transform-www/commits?author=killercup "Code") | [
    Lado Lomidze](http://landish.github.io)
    [💻](https://github.com/ritz078/transform-www/commits?author=Landish "Code") | -| :---: | :---: | :---: | +| [
    Florian Gilcher](http://asquera.de)
    [💻](https://github.com/ritz078/transform-www/commits?author=skade "Code") | [
    Pascal Hertleif](http://pascalhertleif.de/)
    [💻](https://github.com/ritz078/transform-www/commits?author=killercup "Code") | [
    Lado Lomidze](http://landish.github.io)
    [💻](https://github.com/ritz078/transform-www/commits?author=Landish "Code") | [
    Mattia Manzati](https://github.com/mattiamanzati)
    [💻](https://github.com/ritz078/transform-www/commits?author=mattiamanzati "Code") | +| :---: | :---: | :---: | :---: | This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome! \ No newline at end of file diff --git a/utils/babel-plugin-js-to-mobx-state-tree.js b/utils/babel-plugin-js-to-mobx-state-tree.js new file mode 100644 index 0000000..564d71c --- /dev/null +++ b/utils/babel-plugin-js-to-mobx-state-tree.js @@ -0,0 +1,105 @@ +import uniqBy from "lodash/uniqBy"; +import isEmpty from "lodash/isEmpty"; + +/** + * This is a babel plugin that converts an object to PropTypes + * @param t + * @returns {{visitor: {ArrayExpression: (function(*)), NullLiteral: (function(*)), StringLiteral: (function(*)), NumericLiteral: (function(*)), ArrowFunctionExpression: (function(*)), FunctionExpression: (function(*)), BooleanLiteral: (function(*)), Program: (function(*, *)), ObjectExpression: (function(*))}}} + */ +export default function({ types: t }) { + return { + visitor: { + ArrayExpression(path) { + //if (t.isCallExpression(path.parent)) return; + let uniqueElements = uniqBy(path.node.elements, "type"); + + if (t.isObjectExpression(uniqueElements[0])) { + uniqueElements = [ + t.callExpression( + t.memberExpression( + t.identifier("t"), + t.identifier("model") + ), + uniqueElements + ) + ]; + } + + if ( + (!isEmpty(uniqueElements) && uniqueElements.length === 1) || + t.isCallExpression(uniqueElements) + ) { + path.replaceWith( + t.callExpression( + t.memberExpression( + t.identifier("t"), + t.identifier("array") + ), + uniqueElements + ) + ); + } else { + path.replaceWith(t.identifier("t.array")); + } + }, + + NullLiteral(path) { + path.replaceWith(t.identifier("t.null")); + }, + + StringLiteral(path) { + if (path.parentKey === "key") return; + path.replaceWith(t.identifier(`t.string`)); + }, + + NumericLiteral(path) { + path.replaceWith(t.identifier("t.number")); + }, + + ArrowFunctionExpression(path) { + path.replaceWith(t.identifier("t.frozen")); + }, + + FunctionExpression(path) { + path.replaceWith(t.identifier("t.frozen")); + }, + + BooleanLiteral(path) { + path.replaceWith(t.identifier("t.boolean")); + }, + + Program(path) { + path.traverse({ + enter(path) { + t.removeComments(path.node); + } + }); + }, + + ObjectProperty(path){ + if(t.isStringLiteral(path.node.key)){ + path.node.key = t.identifier(path.node.key.value) + } + }, + + ObjectExpression(path) { + if ( + t.isCallExpression(path.parent) + ) + return; + + const elements = [path.node]; + + path.replaceWith( + t.callExpression( + t.memberExpression( + t.identifier("t"), + t.identifier("model") + ), + elements + ) + ); + } + } + }; +}