1 | | // Package for formatting JSON data in a coloured |
2 | | // YAML-style, perfect for CLI output |
3 | | |
4 | | // ### Export package |
5 | 1 | module.exports = exports; |
6 | | |
7 | | |
8 | | // ### Module dependencies |
9 | 1 | var colors = require('colors'); |
10 | 1 | var Utils = require('./utils'); |
11 | 1 | var fs = require('fs'); |
12 | | |
13 | | // ### Package version |
14 | 1 | exports.version = JSON.parse(fs.readFileSync(__dirname + '/../package.json', 'utf8')).version; |
15 | | |
16 | | // ### Render function |
17 | | // *Parameters:* |
18 | | // |
19 | | // * **`data`**: Data to render |
20 | | // * **`options`**: Hash with different options to configure the parser |
21 | | // * **`indentation`**: Base indentation of the parsed output |
22 | | // |
23 | | // *Example of options hash:* |
24 | | // |
25 | | // { |
26 | | // emptyArrayMsg: '(empty)', // Rendered message on empty strings |
27 | | // keysColor: 'blue', // Color for keys in hashes |
28 | | // dashColor: 'red', // Color for the dashes in arrays |
29 | | // stringColor: 'grey', // Color for strings |
30 | | // defaultIndentation: 2 // Indentation on nested objects |
31 | | // } |
32 | 1 | exports.render = function render(data, options, indentation) { |
33 | | "use strict"; |
34 | | |
35 | | // Default value for the indentation param |
36 | 65 | indentation = indentation || 0; |
37 | | |
38 | | // Default values for the options |
39 | 65 | options = options || {}; |
40 | 65 | options.emptyArrayMsg = options.emptyArrayMsg || '(empty array)'; |
41 | 65 | options.keysColor = options.keysColor || "green"; |
42 | 65 | options.dashColor = options.dashColor || "green"; |
43 | 65 | options.defaultIndentation = options.defaultIndentation || 2; |
44 | | |
45 | 65 | options.stringColor = options.stringColor || null; |
46 | | |
47 | | // Initialize the output (it's an array of lines) |
48 | 65 | var output = []; |
49 | | |
50 | | // Helper function to detect if an object can be serializable directly |
51 | 65 | var isSerializable = function(input) { |
52 | 94 | if (typeof input === 'string' || typeof input === 'boolean' || |
53 | | typeof input === 'number' || input === null) { |
54 | 64 | return true; |
55 | | } |
56 | 30 | return false; |
57 | | }; |
58 | | |
59 | 65 | var addColorToData = function(input) { |
60 | 41 | if (typeof input === 'string') { |
61 | | // Print strings in regular terminal color |
62 | 37 | return options.stringColor ? input[options.stringColor] : input; |
63 | | } |
64 | | |
65 | 4 | if (input === true) { |
66 | 1 | return (input+'').green; |
67 | | } |
68 | 3 | if (input === false) { |
69 | 1 | return (input+'').red; |
70 | | } |
71 | 2 | if (input === null) { |
72 | 1 | return (input+'').grey; |
73 | | } |
74 | 1 | if (typeof input === 'number') { |
75 | 1 | return (input+'').blue; |
76 | | } |
77 | 0 | return (input+''); |
78 | | }; |
79 | | |
80 | | // Render a string exactly equal |
81 | 65 | if (isSerializable(data)) { |
82 | 41 | output.push(Utils.indent(indentation) + addColorToData(data)); |
83 | | } |
84 | 24 | else if (Array.isArray(data)) { |
85 | | // If the array is empty, render the `emptyArrayMsg` |
86 | 9 | if (data.length === 0) { |
87 | 2 | output.push(Utils.indent(indentation) + options.emptyArrayMsg); |
88 | | } else { |
89 | 7 | data.forEach(function(element) { |
90 | | // Prepend the dash at the begining of each array's element line |
91 | 14 | var line = Utils.indent(indentation) + ('- ')[options.dashColor]; |
92 | | |
93 | | // If the element of the array is a string, render it in the same line |
94 | 14 | if (typeof element === 'string') { |
95 | 12 | line += exports.render(element, options); |
96 | 12 | output.push(line); |
97 | | |
98 | | // If the element of the array is an array or object, render it in next line |
99 | | } else { |
100 | 2 | output.push(line); |
101 | 2 | output.push( |
102 | | exports.render(element, options, indentation + options.defaultIndentation) |
103 | | ); |
104 | | } |
105 | | }); |
106 | | } |
107 | | } |
108 | 15 | else if (typeof data === 'object') { |
109 | | // Get the size of the longest index to render all the values on the same column |
110 | 15 | var maxIndexLength = Utils.getMaxIndexLength(data); |
111 | 15 | var key; |
112 | | |
113 | 15 | for(var i in data) { |
114 | 30 | if (data.hasOwnProperty(i)) { |
115 | | // Prepend the index at the beginning of the line |
116 | 29 | key = Utils.indent(indentation) + (i + ': ')[options.keysColor]; |
117 | | |
118 | | // If the value is serializable, render it in the same line |
119 | 29 | if (isSerializable(data[i])) { |
120 | 23 | key += exports.render(data[i], options, maxIndexLength - i.length); |
121 | 23 | output.push(key); |
122 | | |
123 | | // If the index is an array or object, render it in next line |
124 | | } else { |
125 | 6 | output.push(key); |
126 | 6 | output.push( |
127 | | exports.render(data[i], options, indentation + options.defaultIndentation) |
128 | | ); |
129 | | } |
130 | | } |
131 | | } |
132 | | } |
133 | | // Return all the lines as a string |
134 | 65 | return output.join('\n'); |
135 | | }; |
136 | | |
137 | | // ### Render from string function |
138 | | // *Parameters:* |
139 | | // |
140 | | // * **`data`**: Data to render as a string |
141 | | // * **`options`**: Hash with different options to configure the parser |
142 | | // * **`indentation`**: Base indentation of the parsed output |
143 | | // |
144 | | // *Example of options hash:* |
145 | | // |
146 | | // { |
147 | | // emptyArrayMsg: '(empty)', // Rendered message on empty strings |
148 | | // keysColor: 'blue', // Color for keys in hashes |
149 | | // dashColor: 'red', // Color for the dashes in arrays |
150 | | // defaultIndentation: 2 // Indentation on nested objects |
151 | | // } |
152 | 1 | exports.renderString = function renderString(data, options, indentation) { |
153 | | "use strict"; |
154 | | |
155 | 7 | var output = ''; |
156 | 7 | var parsedData; |
157 | | // If the input is not a string or if it's empty, just return an empty string |
158 | 7 | if (typeof data !== 'string' || data === '') { |
159 | 2 | return ''; |
160 | | } |
161 | | |
162 | | // Remove non-JSON characters from the beginning string |
163 | 5 | if (data[0] !== '{' && data[0] !== '[') { |
164 | 3 | var beginingOfJson; |
165 | 3 | if (data.indexOf('{') === -1) { |
166 | 2 | beginingOfJson = data.indexOf('['); |
167 | 1 | } else if (data.indexOf('[') === -1) { |
168 | 1 | beginingOfJson = data.indexOf('{'); |
169 | | } else { |
170 | 0 | beginingOfJson = data.indexOf('{') < data.indexOf('[') ? data.indexOf('{') : data.indexOf('['); |
171 | | } |
172 | 3 | output += data.substr(0, beginingOfJson) + "\n"; |
173 | 3 | data = data.substr(beginingOfJson); |
174 | | } |
175 | | |
176 | 5 | try { |
177 | 5 | parsedData = JSON.parse(data); |
178 | | } catch (e) { |
179 | | // Return an error in case of an invalid JSON |
180 | 1 | return 'Error:'.red + ' Not valid JSON!'; |
181 | | } |
182 | | |
183 | | // Call the real render() method |
184 | 4 | output += exports.render(parsedData, options); |
185 | 4 | return output; |
186 | | }; |
187 | | |