Skip to content

Commit

Permalink
Add support for short boolean syntax (#30)
Browse files Browse the repository at this point in the history
* Test for shortBooleanSyntax option

* shortBooleanSyntax implementation & add test

* Add a simple test for shortBooleanSyntax

* Documentation for shortBooleanSyntax option

* props={false} are not hidden by shortBooleanSyntax option

* Remove useless filter

* Modify README documentation

* Update index.js
  • Loading branch information
lucmerceron authored and alansouzati committed Nov 17, 2017
1 parent a23fe03 commit b1c4d31
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,28 @@ let Basic = React.createClass({
console.log(jsxToString(<Basic><svg /><img /><p>I am alone</p></Basic>, {
ignoreTags: ['svg', 'img']
})); //outputs: <Basic><p>I am alone</p></Basic>
```

* shortBooleanSyntax (boolean)

Optional. Defaults to false. Whether or not to show the short or long boolean syntax.

```js
import React from 'react';
import jsxToString from 'jsx-to-string';
//or var jsxToString = require('jsx-to-string');

let Basic = React.createClass({
render() {
return (
<div />
);
}
}); //this is your react component

console.log(jsxToString(<Basic test test2={false} test3={true}>, {
shortBooleanSyntax: true,
})); //outputs: <Basic test test2={false} test3 />
```

* displayName (string)
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ function jsxToString (component, options) {
value = opts.keyValueOverride[key](component.props[key]);
} else if (opts.keyValueOverride[key]) {
value = opts.keyValueOverride[key]
} else if (opts.shortBooleanSyntax && typeof component.props[key] === 'boolean' && component.props[key]) {
return key;
} else {
value = serializeItem(component.props[key], {...opts, key});
}

if (typeof value !== 'string' || value[0] !== "'") {
value = `{${value}}`;
}
Expand Down
31 changes: 31 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,34 @@ test('test a deep react component with multiple ignore tags', function(t) {

t.equal(output, '<Basic>\n <p>\n <div>\n <p>\n Title 2\n </p>\n </div>\n <BasicChild>\n Title 3\n </BasicChild>\n </p>\n</Basic>');
});

test('test a simple react component with boolean props and shortBooleanSyntax on', function(t) {
t.plan(1);

let output = jsxToString(
<Basic test test2={false} test3={true}/>,
{
shortBooleanSyntax: true,
}
);

t.equal(output, '<Basic test\n test2={false}\n test3 />');
});

test('test a complex react component with boolean props and shortBooleanSyntax on', function(t) {
t.plan(1);

let output = jsxToString(
<Basic test="abc" test2={4} test4={true}
test5={{abc: "abc"}} test6="" test7={false} test8>
<BasicChild test1 test2={false} test3={5} test4={6}>
Title 1
</BasicChild>
</Basic>,
{
shortBooleanSyntax: true,
}
);

t.equal(output, '<Basic test=\'abc\'\n test2={4}\n test4\n test5={{"abc": "abc"}}\n test6=\'\'\n test7={false}\n test8>\n <BasicChild test1\n test2={false}\n test3={5}\n test4={6}>\n Title 1\n </BasicChild>\n</Basic>');
});

0 comments on commit b1c4d31

Please sign in to comment.