Coverage

97%
84
82
2

prettyjson.js

97%
74
72
2
LineHitsSource
1// Package for formatting JSON data in a coloured
2// YAML-style, perfect for CLI output
3
4// ### Export package
51module.exports = exports;
6
7
8// ### Module dependencies
91var colors = require('colors');
101var Utils = require('./utils');
111var fs = require('fs');
12
13// ### Package version
141exports.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// }
321exports.render = function render(data, options, indentation) {
3362 "use strict";
34
35 // Default value for the indentation param
3662 indentation = indentation || 0;
37
38 // Default values for the options
3962 options = options || {};
4062 options.emptyArrayMsg = options.emptyArrayMsg || '(empty array)';
4162 options.keysColor = options.keysColor || "green";
4262 options.dashColor = options.dashColor || "green";
4362 options.defaultIndentation = options.defaultIndentation || 2;
44
4562 options.stringColor = options.stringColor || null;
46
47 // Initialize the output (it's an array of lines)
4862 var output = [];
49
50 // Helper function to detect if an object can be serializable directly
5162 var isSerializable = function(input) {
5289 if (typeof input === 'string' || typeof input === 'boolean' ||
53 typeof input === 'number' || input === null) {
5460 return true;
55 }
5629 return false;
57 };
58
5962 var addColorToData = function(input) {
6039 if (typeof input === 'string') {
61 // Print strings in regular terminal color
6235 return options.stringColor ? input[options.stringColor] : input;
63 }
64
654 if (input === true) {
661 return (input+'').green;
67 }
683 if (input === false) {
691 return (input+'').red;
70 }
712 if (input === null) {
721 return (input+'').grey;
73 }
741 if (typeof input === 'number') {
751 return (input+'').blue;
76 }
770 return (input+'');
78 };
79
80 // Render a string exactly equal
8162 if (isSerializable(data)) {
8239 output.push(Utils.indent(indentation) + addColorToData(data));
83 }
8423 else if (Array.isArray(data)) {
85 // If the array is empty, render the `emptyArrayMsg`
869 if (data.length === 0) {
872 output.push(Utils.indent(indentation) + options.emptyArrayMsg);
88 } else {
897 data.forEach(function(element) {
90 // Prepend the dash at the begining of each array's element line
9114 var line = Utils.indent(indentation) + ('- ')[options.dashColor];
92
93 // If the element of the array is a string, render it in the same line
9414 if (typeof element === 'string') {
9512 line += exports.render(element, options);
9612 output.push(line);
97
98 // If the element of the array is an array or object, render it in next line
99 } else {
1002 output.push(line);
1012 output.push(
102 exports.render(element, options, indentation + options.defaultIndentation)
103 );
104 }
105 });
106 }
107 }
10814 else if (typeof data === 'object') {
109 // Get the size of the longest index to render all the values on the same column
11014 var maxIndexLength = Utils.getMaxIndexLength(data);
11114 var key;
112
11314 for(var i in data) {
114 // Prepend the index at the beginning of the line
11527 key = Utils.indent(indentation) + (i + ': ')[options.keysColor];
116
117 // If the value is serializable, render it in the same line
11827 if (isSerializable(data[i])) {
11921 key += exports.render(data[i], options, maxIndexLength - i.length);
12021 output.push(key);
121
122 // If the index is an array or object, render it in next line
123 } else {
1246 output.push(key);
1256 output.push(
126 exports.render(data[i], options, indentation + options.defaultIndentation)
127 );
128 }
129 }
130 }
131 // Return all the lines as a string
13262 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// }
1501exports.renderString = function renderString(data, options, indentation) {
1517 "use strict";
152
1537 var output = '';
1547 var parsedData;
155 // If the input is not a string or if it's empty, just return an empty string
1567 if (typeof data !== 'string' || data === '') {
1572 return '';
158 }
159
160 // Remove non-JSON characters from the beginning string
1615 if (data[0] !== '{' && data[0] !== '[') {
1623 var beginingOfJson;
1633 if (data.indexOf('{') === -1) {
1642 beginingOfJson = data.indexOf('[');
1651 } else if (data.indexOf('[') === -1) {
1661 beginingOfJson = data.indexOf('{');
167 } else {
1680 beginingOfJson = data.indexOf('{') < data.indexOf('[') ? data.indexOf('{') : data.indexOf('[');
169 }
1703 output += data.substr(0, beginingOfJson) + "\n";
1713 data = data.substr(beginingOfJson);
172 }
173
1745 try {
1755 parsedData = JSON.parse(data);
176 } catch (e) {
177 // Return an error in case of an invalid JSON
1781 return 'Error:'.red + ' Not valid JSON!';
179 }
180
181 // Call the real render() method
1824 output += exports.render(parsedData, options);
1834 return output;
184};

utils.js

100%
10
10
0
LineHitsSource
11"use strict";
2
3/**
4 * Creates a string with the same length as `numSpaces` parameter
5 **/
61exports.indent = function indent(numSpaces) {
782 return new Array(numSpaces+1).join(' ');
8};
9
10/**
11 * Gets the string length of the longer index in a hash
12 **/
131exports.getMaxIndexLength = function(input) {
1414 var maxWidth = 0;
1514 var key;
16
1714 for (key in input) {
1827 if (key.length > maxWidth) {
1919 maxWidth = key.length;
20 }
21 }
2214 return maxWidth;
23};