Skip to content

Commit eaca1a2

Browse files
author
vvo
committed
fix(whitespace): handle {true} {false}
fixes #6 fixes #7
1 parent 1e63273 commit eaca1a2

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Features:
2222
- sort object keys in a deterministic order (`o={{a: 1, b:2}} === o={{b:2, a:1}}`)
2323
- handle `ref` and `key` attributes, they are always on top of props
2424
- React's documentation indent style for JSX
25+
- Display whitespace as `<whitespace>` when needed
2526

2627
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
2728
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

index-test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,48 @@ describe(`reactElementToJSXString(ReactElement)`, () => {
346346

347347
expect(reactElementToJSXString(element)).toEqual(`<div />`);
348348
});
349+
350+
it(`reactElementToJSXString(<div>{true}</div>)`, () => {
351+
expect(
352+
reactElementToJSXString(<div>{true}</div>)
353+
).toEqual(`<div />`);
354+
});
355+
356+
it(`reactElementToJSXString(<div>{false}</div>)`, () => {
357+
expect(
358+
reactElementToJSXString(<div>{false}</div>)
359+
).toEqual(`<div />`);
360+
});
361+
362+
it(`reactElementToJSXString(<div>{false}</div>)`, () => {
363+
expect(
364+
reactElementToJSXString(<div>
365+
{false}
366+
</div>)
367+
).toEqual(
368+
reactElementToJSXString(<div></div>)
369+
);
370+
});
371+
372+
it(`reactElementToJSXString(<div>\n{false}\n</div>)`, () => {
373+
expect(
374+
reactElementToJSXString(<div>
375+
{false}
376+
</div>)
377+
).toEqual(`<div />`);
378+
});
379+
380+
it(`reactElementToJSXString(<div> {false} </div>)`, () => {
381+
expect(
382+
reactElementToJSXString(<div> {false} </div>)
383+
).toEqual(`<div>
384+
<whitespace><whitespace>
385+
</div>`);
386+
});
387+
388+
it(`reactElementToJSXString(<div>{null}</div>)`, () => {
389+
expect(
390+
reactElementToJSXString(<div>{null}</div>)
391+
).toEqual(`<div />`);
392+
});
349393
});

index.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ function toJSXString({ReactElement = null, lvl = 0, inline = false}) {
3232
let out = `<${tagName}`;
3333
let props = formatProps(ReactElement.props);
3434
let attributes = [];
35-
let children = ReactElement.props.children;
35+
let children = React.Children.toArray(ReactElement.props.children)
36+
.filter(onlyMeaningfulChildren)
37+
.map(displayWhitespace);
3638

3739
if (ReactElement.ref !== null) {
3840
attributes.push(getJSXAttribute('ref', ReactElement.ref));
@@ -60,7 +62,7 @@ function toJSXString({ReactElement = null, lvl = 0, inline = false}) {
6062
out += `\n${spacer(lvl)}`;
6163
}
6264

63-
if (React.Children.count(children) > 0) {
65+
if (children.length > 0) {
6466
out += `>`;
6567
lvl++;
6668
if (!inline) {
@@ -71,8 +73,7 @@ function toJSXString({ReactElement = null, lvl = 0, inline = false}) {
7173
if (typeof children === 'string') {
7274
out += children;
7375
} else {
74-
out += React.Children
75-
.toArray(children)
76+
out += children
7677
.reduce(mergePlainStringChildren, [])
7778
.map(
7879
recurse({lvl, inline})
@@ -187,3 +188,15 @@ function spacer(times) {
187188
function noChildren(propName) {
188189
return propName !== 'children';
189190
}
191+
192+
function onlyMeaningfulChildren(children) {
193+
return children !== true && children !== false && children !== null;
194+
}
195+
196+
function displayWhitespace(children) {
197+
if (children === ' ') {
198+
return '<whitespace>';
199+
}
200+
201+
return children;
202+
}

0 commit comments

Comments
 (0)