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

Commit a6192db

Browse files
author
Brandon Carroll
committed
feat: build custom toJSON
1 parent f5ae050 commit a6192db

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

src/lib/__tests__/to-json.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from 'react';
2+
import { Text, View } from 'react-native';
3+
4+
import { prettyPrint, render } from '../../';
5+
import { toJSON } from '../to-json';
6+
7+
test('it converts to json', () => {
8+
function ParentComponent({ children }) {
9+
return <View>{children}</View>;
10+
}
11+
12+
function MiddleComponent({ children }) {
13+
return (
14+
<View>
15+
<Text>{children}</Text>
16+
</View>
17+
);
18+
}
19+
20+
const { container } = render(
21+
<ParentComponent>
22+
<View>
23+
<MiddleComponent>hello</MiddleComponent>
24+
<View>
25+
<Text>world</Text>
26+
</View>
27+
</View>
28+
</ParentComponent>,
29+
);
30+
31+
console.log(prettyPrint(toJSON(container)));
32+
});

src/lib/to-json.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
47+
function flat(arr) {
48+
return arr.reduce((arr, toFlatten) => {
49+
return arr.concat(Array.isArray(toFlatten) ? flat(toFlatten) : toFlatten);
50+
}, []);
51+
}
52+
53+
function toJSON(node) {
54+
if (typeof node === 'string') {
55+
return node;
56+
}
57+
58+
const { children, ...props } = node.props;
59+
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);
70+
71+
if (node.parent === null) {
72+
return renderedChildren[0];
73+
}
74+
75+
if (typeof node.type !== 'string') {
76+
return renderedChildren;
77+
}
78+
79+
const json = {
80+
type: node.type,
81+
props,
82+
children: renderedChildren,
83+
};
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;
91+
}
92+
93+
export { toJSON };

0 commit comments

Comments
 (0)