Skip to content
This repository was archived by the owner on Jul 30, 2020. It is now read-only.

Commit 64b60d4

Browse files
author
Brandon Carroll
committed
chore: clean up toJSON
1 parent 87e62c3 commit 64b60d4

File tree

2 files changed

+13
-65
lines changed

2 files changed

+13
-65
lines changed

src/lib/__tests__/to-json.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ test('it converts to json', () => {
2323
<MiddleComponent>hello</MiddleComponent>
2424
<View>
2525
<Text>world</Text>
26+
<Text>foo bar</Text>
2627
</View>
28+
<View />
2729
</View>
2830
</ParentComponent>,
2931
);

src/lib/to-json.js

Lines changed: 11 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,39 @@
1-
const tree = {
2-
type: 'View',
3-
props: {},
4-
children: [
5-
{
6-
type: function ParentComponent() {},
7-
props: {},
8-
children: [
9-
{
10-
type: 'View',
11-
props: {},
12-
children: [
13-
{
14-
type: function MiddleComponent() {},
15-
props: {},
16-
children: [
17-
{
18-
type: 'View',
19-
props: {},
20-
children: [
21-
{
22-
type: 'Text',
23-
props: {},
24-
children: ['hello'],
25-
},
26-
],
27-
},
28-
{
29-
type: 'Text',
30-
props: {},
31-
children: ['world'],
32-
},
33-
],
34-
},
35-
],
36-
},
37-
{
38-
type: 'Text',
39-
props: {},
40-
children: ['hello', ' ', 'world'],
41-
},
42-
],
43-
},
44-
],
45-
};
46-
471
function flat(arr) {
482
return arr.reduce((arr, toFlatten) => {
493
return arr.concat(Array.isArray(toFlatten) ? flat(toFlatten) : toFlatten);
504
}, []);
515
}
526

537
function toJSON(node) {
8+
// If the node is a string return it
549
if (typeof node === 'string') {
5510
return node;
5611
}
5712

13+
// We don't want children being included in the props
5814
const { children, ...props } = node.props;
5915

60-
let renderedChildren = [];
61-
if (node.children && node.children.length) {
62-
for (let i = 0; i < node.children.length; i++) {
63-
const renderedChild = toJSON(node.children[i]);
64-
65-
renderedChildren.push(renderedChild);
66-
}
67-
}
68-
69-
renderedChildren = flat(renderedChildren);
16+
// Convert all children to the JSON format
17+
const renderedChildren = flat(node.children.map(child => toJSON(child)));
7018

19+
// If there's no parent, return the base element not in an array
7120
if (node.parent === null) {
7221
return renderedChildren[0];
7322
}
7423

24+
// Hoist children so that only "native elements" are in the output
7525
if (typeof node.type !== 'string') {
7626
return renderedChildren;
7727
}
7828

79-
const json = {
29+
// Finally, create the JSON object
30+
return {
31+
$$typeof: Symbol.for('react.test.json'),
32+
parent: node.parent,
8033
type: node.type,
8134
props,
8235
children: renderedChildren,
8336
};
84-
Object.defineProperty(json, '$$typeof', {
85-
value: Symbol.for('react.test.json'),
86-
});
87-
Object.defineProperty(json, 'parent', {
88-
value: node.parent,
89-
});
90-
return json;
9137
}
9238

93-
export { toJSON };
39+
export { flat, toJSON };

0 commit comments

Comments
 (0)