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 | 62 | "use strict"; |
34 | | |
35 | | // Default value for the indentation param |
36 | 62 | indentation = indentation || 0; |
37 | | |
38 | | // Default values for the options |
39 | 62 | options = options || {}; |
40 | 62 | options.emptyArrayMsg = options.emptyArrayMsg || '(empty array)'; |
41 | 62 | options.keysColor = options.keysColor || "green"; |
42 | 62 | options.dashColor = options.dashColor || "green"; |
43 | 62 | options.defaultIndentation = options.defaultIndentation || 2; |
44 | | |
45 | 62 | options.stringColor = options.stringColor || null; |
46 | | |
47 | | // Initialize the output (it's an array of lines) |
48 | 62 | var output = []; |
49 | | |
50 | | // Helper function to detect if an object can be serializable directly |
51 | 62 | var isSerializable = function(input) { |
52 | 89 | if (typeof input === 'string' || typeof input === 'boolean' || |
53 | | typeof input === 'number' || input === null) { |
54 | 60 | return true; |
55 | | } |
56 | 29 | return false; |
57 | | }; |
58 | | |
59 | 62 | var addColorToData = function(input) { |
60 | 39 | if (typeof input === 'string') { |
61 | | // Print strings in regular terminal color |
62 | 35 | 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 | 62 | if (isSerializable(data)) { |
82 | 39 | output.push(Utils.indent(indentation) + addColorToData(data)); |
83 | | } |
84 | 23 | 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 | 14 | else if (typeof data === 'object') { |
109 | | // Get the size of the longest index to render all the values on the same column |
110 | 14 | var maxIndexLength = Utils.getMaxIndexLength(data); |
111 | 14 | var key; |
112 | | |
113 | 14 | for(var i in data) { |
114 | | // Prepend the index at the beginning of the line |
115 | 27 | key = Utils.indent(indentation) + (i + ': ')[options.keysColor]; |
116 | | |
117 | | // If the value is serializable, render it in the same line |
118 | 27 | if (isSerializable(data[i])) { |
119 | 21 | key += exports.render(data[i], options, maxIndexLength - i.length); |
120 | 21 | output.push(key); |
121 | | |
122 | | // If the index is an array or object, render it in next line |
123 | | } else { |
124 | 6 | output.push(key); |
125 | 6 | output.push( |
126 | | exports.render(data[i], options, indentation + options.defaultIndentation) |
127 | | ); |
128 | | } |
129 | | } |
130 | | } |
131 | | // Return all the lines as a string |
132 | 62 | return output.join('\n'); |
133 | | }; |
134 | | |
135 | | // ### Render from string function |
136 | | // *Parameters:* |
137 | | // |
138 | | // * **`data`**: Data to render as a string |
139 | | // * **`options`**: Hash with different options to configure the parser |
140 | | // * **`indentation`**: Base indentation of the parsed output |
141 | | // |
142 | | // *Example of options hash:* |
143 | | // |
144 | | // { |
145 | | // emptyArrayMsg: '(empty)', // Rendered message on empty strings |
146 | | // keysColor: 'blue', // Color for keys in hashes |
147 | | // dashColor: 'red', // Color for the dashes in arrays |
148 | | // defaultIndentation: 2 // Indentation on nested objects |
149 | | // } |
150 | 1 | exports.renderString = function renderString(data, options, indentation) { |
151 | 7 | "use strict"; |
152 | | |
153 | 7 | var output = ''; |
154 | 7 | var parsedData; |
155 | | // If the input is not a string or if it's empty, just return an empty string |
156 | 7 | if (typeof data !== 'string' || data === '') { |
157 | 2 | return ''; |
158 | | } |
159 | | |
160 | | // Remove non-JSON characters from the beginning string |
161 | 5 | if (data[0] !== '{' && data[0] !== '[') { |
162 | 3 | var beginingOfJson; |
163 | 3 | if (data.indexOf('{') === -1) { |
164 | 2 | beginingOfJson = data.indexOf('['); |
165 | 1 | } else if (data.indexOf('[') === -1) { |
166 | 1 | beginingOfJson = data.indexOf('{'); |
167 | | } else { |
168 | 0 | beginingOfJson = data.indexOf('{') < data.indexOf('[') ? data.indexOf('{') : data.indexOf('['); |
169 | | } |
170 | 3 | output += data.substr(0, beginingOfJson) + "\n"; |
171 | 3 | data = data.substr(beginingOfJson); |
172 | | } |
173 | | |
174 | 5 | try { |
175 | 5 | parsedData = JSON.parse(data); |
176 | | } catch (e) { |
177 | | // Return an error in case of an invalid JSON |
178 | 1 | return 'Error:'.red + ' Not valid JSON!'; |
179 | | } |
180 | | |
181 | | // Call the real render() method |
182 | 4 | output += exports.render(parsedData, options); |
183 | 4 | return output; |
184 | | }; |