1 | var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
|
2 |
|
3 | function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
|
4 |
|
5 | import React, { Children } from 'react';
|
6 |
|
7 | var defaultProcessMeta = function defaultProcessMeta(tree) {
|
8 | var name = void 0,
|
9 | type = void 0;
|
10 | if (typeof tree.type === 'string') {
|
11 | name = tree.type;
|
12 | type = 'string';
|
13 | } else if (typeof tree.type === 'function') {
|
14 | name = tree.type.displayName || tree.type.name || 'Unknown';
|
15 | type = 'function';
|
16 | } else {
|
17 | name = 'Unknown';
|
18 | type = 'unknown';
|
19 | }
|
20 | return { name: name, type: type };
|
21 | };
|
22 |
|
23 | var defaultProcessProps = function defaultProcessProps(props) {
|
24 | return props;
|
25 | };
|
26 |
|
27 | export var convertToObject = function convertToObject(tree) {
|
28 | var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
29 |
|
30 | var processMeta = params.processMeta || defaultProcessMeta;
|
31 | var processProps = params.processProps || defaultProcessProps;
|
32 |
|
33 | var convertChild = function convertChild(child) {
|
34 | if (child == null) {
|
35 | return;
|
36 | }
|
37 | if (typeof child === 'boolean' || typeof child === 'number' || typeof child === 'string') {
|
38 | return child;
|
39 | }
|
40 | if (child.type) {
|
41 | return convertComponent(child);
|
42 | }
|
43 | };
|
44 |
|
45 | var convertChildren = function convertChildren(children) {
|
46 | return Array.isArray(children) ? Children.map(children, convertChild) : convertChild(children);
|
47 | };
|
48 |
|
49 | var convertComponent = function convertComponent(tree, key) {
|
50 | if (typeof tree.key === 'string') {
|
51 | key = tree.key;
|
52 | }
|
53 |
|
54 | var _processMeta = processMeta(tree),
|
55 | name = _processMeta.name,
|
56 | type = _processMeta.type;
|
57 |
|
58 | if (type === 'unknown') {
|
59 | return {
|
60 | type: 'Unsupported',
|
61 | props: {
|
62 | children: []
|
63 | }
|
64 | };
|
65 | }
|
66 |
|
67 | var props = processProps(tree.props);
|
68 | if (type === 'function') {
|
69 | return convertComponent(tree.type(props), key);
|
70 | }
|
71 |
|
72 | var children = convertChildren(props.children);
|
73 | return {
|
74 | type: name,
|
75 | props: _extends({}, props, { children: children, key: key })
|
76 | };
|
77 | };
|
78 |
|
79 | return convertComponent(tree);
|
80 | };
|
81 |
|
82 | var convertToJSON = function convertToJSON(tree) {
|
83 | var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
84 |
|
85 | var space = params.space,
|
86 | convertParams = _objectWithoutProperties(params, ['space']);
|
87 |
|
88 | return JSON.stringify(convertToObject(tree, convertParams), null, space);
|
89 | };
|
90 | export { convertToJSON }; |
\ | No newline at end of file |