| 44 | const defaultProcessProps = (props: Object) => props |
| 45 | |
| 46 | export const convertToObject = ( |
| 47 | tree: Element<*>, |
| 48 | params?: ConvertParams = {}, |
| 49 | ): ConvertedElement => { |
| 50 | const processMeta = params.processMeta || defaultProcessMeta |
| 51 | const processProps = params.processProps || defaultProcessProps |
| 52 | |
| 53 | const convertChild = (child: ElementChild): ConvertedChild => { |
| 54 | if (child == null) { |
| 55 | return |
| 56 | } |
| 57 | if ( |
| 58 | typeof child === 'boolean' || |
| 59 | typeof child === 'number' || |
| 60 | typeof child === 'string' |
| 61 | ) { |
| 62 | return child |
| 63 | } |
| 64 | if (child.type) { |
| 65 | return convertComponent(child) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | const convertChildren = (children: ElementChildren): ConvertedChildren => { |
| 70 | return Array.isArray(children) |
| 71 | ? Children.map(children, convertChild) |
| 72 | : convertChild(children) |
| 73 | } |
| 74 | |
| 75 | const convertComponent = (tree: Element<*>, key?: string) => { |
| 76 | if (typeof tree.key === 'string') { |
| 77 | key = tree.key |
| 78 | } |
| 79 | |
| 80 | const { name, type } = processMeta(tree) |
| 81 | if (type === 'unknown') { |
| 82 | return { |
| 83 | type: 'Unsupported', |
| 84 | props: { |
| 85 | children: [], |
| 86 | }, |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | const props = processProps(tree.props) |
| 91 | if (type === 'function') { |
| 92 | return convertComponent(tree.type(props), key) |
| 93 | } |
| 94 | |
| 95 | const children = convertChildren(props.children) |
| 96 | return { |
| 97 | type: name, |
| 98 | props: { ...props, children, key }, |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return convertComponent(tree) |
| 103 | } |