1 | 'use strict';
|
2 |
|
3 |
|
4 | require('colors');
|
5 | var Utils = require('./utils');
|
6 |
|
7 | exports.version = require('../package.json').version;
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | exports.render = function render(data, options, indentation) {
|
26 |
|
27 |
|
28 | indentation = indentation || 0;
|
29 | options = options || {};
|
30 | options.emptyArrayMsg = options.emptyArrayMsg || '(empty array)';
|
31 | options.keysColor = options.keysColor || 'green';
|
32 | options.dashColor = options.dashColor || 'green';
|
33 | options.numberColor = options.numberColor || 'blue';
|
34 | options.defaultIndentation = options.defaultIndentation || 2;
|
35 |
|
36 | options.stringColor = options.stringColor || null;
|
37 |
|
38 | var output = [];
|
39 |
|
40 |
|
41 | var isSerializable = function(input) {
|
42 | if (typeof input === 'string' || typeof input === 'boolean' ||
|
43 | typeof input === 'number' || input === null) {
|
44 | return true;
|
45 | }
|
46 | return false;
|
47 | };
|
48 |
|
49 | var addColorToData = function(input) {
|
50 | if (typeof input === 'string') {
|
51 |
|
52 | return options.stringColor ? input[options.stringColor] : input;
|
53 | }
|
54 |
|
55 | if (input === true) {
|
56 | return (input+'').green;
|
57 | }
|
58 | if (input === false) {
|
59 | return (input+'').red;
|
60 | }
|
61 | if (input === null) {
|
62 | return (input+'').grey;
|
63 | }
|
64 | if (typeof input === 'number') {
|
65 | return (input+'')[options.numberColor];
|
66 | }
|
67 | return (input+'');
|
68 | };
|
69 |
|
70 |
|
71 | if (isSerializable(data)) {
|
72 | output.push(Utils.indent(indentation) + addColorToData(data));
|
73 | }
|
74 | else if (Array.isArray(data)) {
|
75 |
|
76 | if (data.length === 0) {
|
77 | output.push(Utils.indent(indentation) + options.emptyArrayMsg);
|
78 | } else {
|
79 | data.forEach(function(element) {
|
80 |
|
81 | var line = Utils.indent(indentation) + ('- ')[options.dashColor];
|
82 |
|
83 |
|
84 |
|
85 | if (isSerializable(element)) {
|
86 | line += exports.render(element, options);
|
87 | output.push(line);
|
88 |
|
89 |
|
90 | } else {
|
91 | output.push(line);
|
92 | output.push(exports.render(
|
93 | element, options, indentation + options.defaultIndentation
|
94 | ));
|
95 | }
|
96 | });
|
97 | }
|
98 | }
|
99 | else if (typeof data === 'object') {
|
100 |
|
101 | var maxIndexLength = Utils.getMaxIndexLength(data);
|
102 | var key;
|
103 | var isError = data instanceof Error;
|
104 |
|
105 | Object.getOwnPropertyNames(data).forEach(function(i) {
|
106 |
|
107 | key = Utils.indent(indentation) + (i + ': ')[options.keysColor];
|
108 |
|
109 |
|
110 | if (data[i] === undefined) {
|
111 | return;
|
112 | }
|
113 |
|
114 |
|
115 | if (isSerializable(data[i]) && (!isError || i !== 'stack')) {
|
116 | key += exports.render(data[i], options, maxIndexLength - i.length);
|
117 | output.push(key);
|
118 |
|
119 |
|
120 | } else {
|
121 | output.push(key);
|
122 | output.push(
|
123 | exports.render(
|
124 | isError && i === 'stack' ? data[i].split('\n') : data[i],
|
125 | options,
|
126 | indentation + options.defaultIndentation
|
127 | )
|
128 | );
|
129 | }
|
130 | });
|
131 | }
|
132 |
|
133 | return output.join('\n');
|
134 | };
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | exports.renderString = function renderString(data, options, indentation) {
|
152 |
|
153 | var output = '';
|
154 | var parsedData;
|
155 |
|
156 | if (typeof data !== 'string' || data === '') {
|
157 | return '';
|
158 | }
|
159 |
|
160 |
|
161 | if (data[0] !== '{' && data[0] !== '[') {
|
162 | var beginingOfJson;
|
163 | if (data.indexOf('{') === -1) {
|
164 | beginingOfJson = data.indexOf('[');
|
165 | } else if (data.indexOf('[') === -1) {
|
166 | beginingOfJson = data.indexOf('{');
|
167 | } else if (data.indexOf('{') < data.indexOf('[')) {
|
168 | beginingOfJson = data.indexOf('{');
|
169 | } else {
|
170 | beginingOfJson = data.indexOf('[');
|
171 | }
|
172 | output += data.substr(0, beginingOfJson) + '\n';
|
173 | data = data.substr(beginingOfJson);
|
174 | }
|
175 |
|
176 | try {
|
177 | parsedData = JSON.parse(data);
|
178 | } catch (e) {
|
179 |
|
180 | return 'Error:'.red + ' Not valid JSON!';
|
181 | }
|
182 |
|
183 |
|
184 | output += exports.render(parsedData, options, indentation);
|
185 | return output;
|
186 | };
|