Comparing version 1.0.0 to 2.0.0
{ | ||
"parser": "babel-eslint", | ||
"extends": "airbnb/base", | ||
"rules": { | ||
"semi": [2, "never"], | ||
"prefer-arrow-callback": 0, | ||
"space-before-function-paren": [2, "never"], | ||
"func-names": 0, | ||
"no-param-reassign": 0, | ||
"comma-dangle": [2, "never"], | ||
"max-len": [1, 120], | ||
"no-console": 0, | ||
"prefer-template": 0, | ||
"no-nested-ternary": 0, | ||
"no-multi-spaces": 0 | ||
}, | ||
"globals": { | ||
"atom": true | ||
} | ||
"extends": "steelbrain" | ||
} |
@@ -0,3 +1,13 @@ | ||
## 2.0.0 | ||
- Self close empty tags | ||
- Convert second bool param to object | ||
- Replace `_` with `:` in attribute names to help with XML generation | ||
## 1.0.1 | ||
- Fix a bug with more than one colons in attributes | ||
## 1.0.0 | ||
- Initial release |
'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
/** @jsx h */ | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
var _escapeHtml = require('escape-html'); | ||
@@ -10,2 +10,8 @@ | ||
var _helpers = require('./helpers'); | ||
var Helpers = _interopRequireWildcard(_helpers); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -18,3 +24,3 @@ | ||
var props = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; | ||
var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
@@ -28,42 +34,25 @@ return { | ||
function jsx(item) { | ||
var escape = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1]; | ||
function handleString(item, options) { | ||
if (options.escape) { | ||
return (0, _escapeHtml2.default)(item); | ||
} | ||
return item; | ||
} | ||
var type = typeof item === 'undefined' ? 'undefined' : _typeof(item); | ||
if (type === 'undefined' || item === null) { | ||
return ''; | ||
} | ||
if (type === 'number' || type === 'boolean') { | ||
return String(item); | ||
} | ||
if (type === 'string') { | ||
if (escape) { | ||
return (0, _escapeHtml2.default)(item); | ||
} | ||
return item; | ||
} | ||
if (type !== 'object') { | ||
console.warn('[jsx-string] Unknown object type encountered:', type, 'of', item); | ||
} | ||
if (Array.isArray(item)) { | ||
var _output = []; | ||
for (var i = 0, length = item.length; i < length; ++i) { | ||
var result = jsx(item[i], escape); | ||
if (result.length) { | ||
_output.push(result); | ||
} | ||
} | ||
return _output.join(''); | ||
} | ||
if (typeof item.name !== 'string' || _typeof(item.props) !== 'object' || !Array.isArray(item.children)) { | ||
throw new Error('Unknown item encountered in jsx-string'); | ||
} | ||
function handleArray(items, options) { | ||
// eslint-disable-next-line no-use-before-define | ||
return items.map(function (item) { | ||
return handle(item, options); | ||
}).join(''); | ||
} | ||
function handleObect(item, options) { | ||
var attributes = []; | ||
if (item.props !== null) { | ||
var props = Object.keys(item.props); | ||
for (var _i = 0, _length = props.length; _i < _length; ++_i) { | ||
var name = props[_i]; | ||
for (var i = 0, length = props.length; i < length; ++i) { | ||
var name = props[i]; | ||
var value = item.props[name]; | ||
if (typeof value !== 'undefined' && value !== 'null') { | ||
var renderFriendlyName = name.replace('_', ':'); | ||
var renderFriendlyName = name.replace(/_/g, ':'); | ||
attributes.push(renderFriendlyName + '="' + (0, _escapeHtml2.default)(value) + '"'); | ||
@@ -73,4 +62,11 @@ } | ||
} | ||
var output = ['<' + item.name + (attributes.length ? ' ' + attributes.join(' ') : '') + '>']; | ||
var children = jsx(item.children, escape); | ||
var itemName = item.name.replace(/_/g, ':'); | ||
var openingTag = '<' + itemName + (attributes.length ? ' ' + attributes.join(' ') : '') + '>'; | ||
if (!item.children.length) { | ||
return openingTag.slice(0, -1) + ' />'; | ||
} | ||
var output = [openingTag]; | ||
// eslint-disable-next-line no-use-before-define | ||
var children = handle(item.children, options); | ||
if (children.length) { | ||
@@ -83,3 +79,25 @@ output.push(children); | ||
module.exports = jsx; | ||
function handle(item, options) { | ||
var type = typeof item === 'undefined' ? 'undefined' : _typeof(item); | ||
if (type === 'undefined' || item === null) { | ||
return ''; | ||
} | ||
if (type === 'number' || type === 'boolean') { | ||
return item.toString(); | ||
} | ||
if (type === 'string') { | ||
return handleString(item, options); | ||
} | ||
if (Array.isArray(item)) { | ||
return handleArray(item, options); | ||
} | ||
if (type === 'object') { | ||
return handleObect(item, options); | ||
} | ||
throw new Error('Unrecognized input type provided to jsx-string: ' + type); | ||
} | ||
module.exports = function (input, options) { | ||
return handle(input, Helpers.fillOptions(options)); | ||
}; | ||
module.exports.h = h; |
{ | ||
"name": "jsx-string", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "JSX in, strings out", | ||
@@ -9,10 +9,8 @@ "main": "lib/index.js", | ||
"compile": "ucompiler go", | ||
"watch": "ucompiler watch", | ||
"lint": "eslint ." | ||
}, | ||
"devDependencies": { | ||
"eslint": "^2.5.3", | ||
"babel-eslint": "^6.0.0", | ||
"babel-preset-steelbrain": "^2.0.1", | ||
"eslint-config-airbnb": "^6.2.0", | ||
"eslint-plugin-react": "^4.2.3", | ||
"babel-preset-steelbrain": "^5.0.2", | ||
"eslint-config-steelbrain": "^3.0.0", | ||
"ucompiler": "^3.1.8", | ||
@@ -19,0 +17,0 @@ "ucompiler-plugin-babel": "^3.1.1" |
# JSX-String | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/steelbrain/jsx-string.svg)](https://greenkeeper.io/) | ||
JSX-string is a cool module that allows you to write JSX and receive strings out of it. The main benefits include writing XML as JSX. | ||
Because React by design doesn't support colons (`:`) in attribute names, just wrote `a_b` and it'll be converted to `a:b` | ||
Because React by design doesn't support colons (`:`) in attribute and tags, just write `a_b` and it'll be converted to `a:b` | ||
@@ -13,2 +15,13 @@ ## Installation | ||
## Types | ||
```js | ||
export type Options = { | ||
escape: boolean = false, | ||
}; | ||
export default function jsx(input: any, options: Options); | ||
export function h(...); | ||
``` | ||
## Usage | ||
@@ -25,3 +38,5 @@ | ||
<param>{username}</param> | ||
</methodCall>) | ||
</methodCall>, { | ||
escape: false, | ||
}) | ||
console.log(typeof query) // 'string' | ||
@@ -28,0 +43,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
7412
4
12
97
46