Skip to content

Commit

Permalink
standard linter added & small refactor to reduce deps
Browse files Browse the repository at this point in the history
  • Loading branch information
diffcunha committed May 11, 2017
1 parent c4d3d46 commit 36247f5
Show file tree
Hide file tree
Showing 10 changed files with 289 additions and 488 deletions.
8 changes: 7 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
"presets": ["es2015"],
"presets": [
"es2015"
],
"plugins": [
"add-module-exports",
"transform-object-rest-spread"
],
"env": {
"test": {
"plugins": ["rewire"]
Expand Down
70 changes: 5 additions & 65 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,69 +1,9 @@
{
"parser": "babel-eslint",
"parserOptions": {
"ecmaFeatures": {
"impliedStrict": true
}
},
"extends": [
"standard"
],
"env": {
"es6": true,
"mocha": true,
"node": true
},
"rules": {
"brace-style": 2,
"camelcase": 2,
"comma-dangle": [2, "never"],
"comma-spacing": [2, { "before": false, "after": true }],
"comma-style": [2, "last"],
"complexity": [1, 8],
"consistent-this": [2, "_this"],
"curly": 2,
"default-case": 2,
"dot-notation": 2,
"eol-last": 2,
"eqeqeq": 2,
"guard-for-in": 1,
"indent": [2, 2, { "SwitchCase": 1 }],
"key-spacing": [2, { "beforeColon": false, "afterColon": true }],
"keyword-spacing": 2,
"new-cap": 2,
"new-parens": 2,
"no-caller": 2,
"no-debugger": 1,
"no-dupe-args": 2,
"no-dupe-keys": 2,
"no-duplicate-case": 2,
"no-eq-null": 0,
"no-eval": 2,
"no-implied-eval": 2,
"no-invalid-regexp": 2,
"no-mixed-spaces-and-tabs": 2,
"no-redeclare": 2,
"no-self-compare": 1,
"no-shadow-restricted-names": 2,
"no-trailing-spaces": 2,
"no-undef": 2,
"no-undef-init": 2,
"no-underscore-dangle": 0,
"no-unreachable": 2,
"no-unused-vars": 1,
"no-use-before-define": 2,
"no-with": 2,
"one-var": [2, "never"],
"operator-assignment": [2, "always"],
"quote-props": [1, "as-needed"],
"quotes": [2, "single"],
"radix": 2,
"semi": [2, "always"],
"semi-spacing": [2, { "before": false, "after": true }],
"sort-vars": [1, { "ignoreCase": true }],
"space-before-function-paren": [2, {"anonymous": "always", "named": "never"}],
"space-in-parens": [2, "never"],
"space-infix-ops": 2,
"space-unary-ops": [2, { "words": true, "nonwords": false }],
"use-isnan": 2,
"valid-jsdoc": 1,
"yoda": [2, "never", { "exceptRange": false }]
"node": true,
"mocha": true
}
}
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ $ npm i --save jsonmltoreact
import _ from 'lodash';
import React from 'react';
import ReactDOMServer from 'react-dom/server';

import JsonmlToReact from './src';
import JsonmlToReact from 'jsonmltoreact';

// Some random React component
class CustomCompoenent extends React.Component {
render() {
return (
<div className="container">
<div className="customCompoenentContainer">
{this.props.header && <h1>{this.props.header}</h1>}
{this.props.subheader && <h2>{this.props.subheader}</h2>}
{this.props.children}
Expand All @@ -36,7 +35,12 @@ class CustomCompoenent extends React.Component {
let jsonmlToReact = new JsonmlToReact({
'custom-tag': (props, data) => ({
type: CustomCompoenent,
props: _.assign({}, props, data),
props: { ...props, ...data },
}),
'span': props => ({
props: {
className: 'foobar'
}
})
});

Expand All @@ -59,5 +63,13 @@ let data = {
let reactComponent = jsonmlToReact.convert(jsonml, data);

console.log(ReactDOMServer.renderToStaticMarkup(reactComponent));
// <div class="container"><div class="container"><h1>foo</h1><h2>bar</h2><span>content</span></div></div>
/*
<div class="container">
<div class="customCompoenentContainer">
<h1>foo</h1>
<h2>bar</h2>
<span class="foobar">content</span>
</div>
</div>
*/
```
37 changes: 19 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
"description": "JsonML to React component converter",
"main": "./lib/index.js",
"scripts": {
"build": "npm run build:js",
"build:js": "./node_modules/.bin/babel ./src --out-dir ./lib",
"build": "./node_modules/.bin/babel ./src --out-dir ./lib",
"coverage": "nyc --reporter=lcov --require babel-core/register mocha ${TEST_FILES:-test/src/index.spec.js}",
"lint": "eslint --ext .js -c .eslintrc .",
"lint": "eslint .",
"prebuild": "rm -rf lib",
"precommit": "npm run lint",
"prepublish": "npm run build",
Expand All @@ -27,26 +26,28 @@
],
"dependencies": {
"jsonml.js": "^0.1.0",
"lodash.camelcase": "^4.1.1",
"lodash.isfunction": "^3.0.8"
"lodash.camelcase": "^4.1.1"
},
"peerDependencies": {
"react": "^0.14.0 || ^15.0.0"
},
"devDependencies": {
"babel-cli": "^6.8.0",
"babel-core": "^6.8.0",
"babel-eslint": "^6.0.4",
"babel-plugin-rewire": "^1.0.0-rc-3",
"babel-preset-es2015": "^6.6.0",
"babel-template": "^6.8.0",
"babel-types": "^6.8.1",
"chai": "^3.5.0",
"eslint": "^2.10.1",
"husky": "^0.11.4",
"mocha": "^2.4.5",
"nyc": "^6.4.4",
"sinon": "^1.17.4"
"babel-cli": "6.24.1",
"babel-plugin-add-module-exports": "0.2.1",
"babel-plugin-rewire": "1.1.0",
"babel-plugin-transform-object-rest-spread": "6.23.0",
"babel-preset-es2015": "6.24.1",
"eslint": "3.19.0",
"eslint-config-standard": "10.2.1",
"eslint-plugin-import": "2.2.0",
"eslint-plugin-node": "4.2.2",
"eslint-plugin-promise": "3.5.0",
"eslint-plugin-standard": "3.0.1",
"husky": "0.13.3",
"mocha": "3.3.0",
"must": "0.13.4",
"nyc": "10.3.2",
"sinon": "2.2.0"
},
"nyc": {
"require": [
Expand Down
74 changes: 0 additions & 74 deletions src/JsonmlToReact.js

This file was deleted.

72 changes: 68 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,69 @@
import JsonmlToReact from './JsonmlToReact';
import React from 'react'
import JsonML from 'jsonml.js/lib/utils'
import { toStyleObject } from './utils'

export {
JsonmlToReact as default
};
/**
* JsonmlToReact class
*/
export default class JsonmlToReact {
/**
* Constructor
* @param {Object} converters - Aditional converters
*/
constructor (converters = {}) {
this.converters = converters
}

/**
* Visit JsonML nodes recursively and convert to React
* @param {Array} node - JsonML structure
* @param {Number} index - Node index to be used as key
* @param {Object} [data] - Data to be passed to the converters
* @returns {Object} React component
* @private
*/
_visit (node, index, data) {
// Is leaf node
if (!node || typeof node === 'string') {
return node
}

let attrs = Object.assign({ key: index }, JsonML.getAttributes(node))

if (attrs.class) {
attrs.className = attrs.class
attrs.class = undefined
}
if (attrs.style) {
attrs.style = toStyleObject(attrs.style)
}

const tag = JsonML.getTagName(node)
const converter = this.converters[tag]
const result = converter ? converter(attrs, data) : {}

const type = result.type || tag
const props = result.props || attrs

// reassign key in case `converter` removed it
props.key = props.key || index

const children = JsonML.getChildren(node)

if (!children || children.length === 0) {
return React.createElement(type, props)
}

return React.createElement(type, props, children.map((child, index) => this._visit(child, index, data)))
}

/**
* Convert JsonML to React component
* @param {Array} jsonml - JsonML structure
* @param {Object} [data] - Data to be passed to the converters
* @returns {Object} React component
*/
convert (jsonml, data) {
return this._visit(jsonml, 0, data)
}
}
Loading

0 comments on commit 36247f5

Please sign in to comment.