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