Coverage

98%
77
76
1

prettyjson.js

98%
67
66
1
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// defaultIndentation: 2 // Indentation on nested objects
30// }
311exports.render = function render(data, options, indentation) {
3255 "use strict";
33
34 // Default value for the indentation param
3555 indentation = indentation || 0;
36
37 // Default values for the options
3855 options = options || {};
3955 options.emptyArrayMsg = options.emptyArrayMsg || '(empty array)';
4055 options.keysColor = options.keysColor || "green";
4155 options.dashColor = options.dashColor || "green";
4255 options.defaultIndentation = options.defaultIndentation || 2;
43
44 // Initialize the output (it's an array of lines)
4555 var output = [];
46
47 // Helper function to detect if an object can be serializable directly
4855 var isSerializable = function(input) {
4979 if (typeof input === 'string' || typeof input === 'boolean' ||
50 typeof input === 'number' || input === null) {
5153 return true;
52 }
5326 return false;
54 };
55
5655 var addColorToData = function(input) {
5735 if (typeof input === 'string') {
58 // Print strings in regular terminal color
5931 return input;
60 }
61
624 if (input === true) {
631 return (input+'').green;
64 }
653 if (input === false) {
661 return (input+'').red;
67 }
682 if (input === null) {
691 return (input+'').grey;
70 }
711 if (typeof input === 'number') {
721 return (input+'').blue;
73 }
740 return (input+'');
75 };
76
77 // Render a string exactly equal
7855 if (isSerializable(data)) {
7935 output.push(Utils.indent(indentation) + addColorToData(data));
80 }
8120 else if (Array.isArray(data)) {
82 // If the array is empty, render the `emptyArrayMsg`
838 if (data.length === 0) {
842 output.push(Utils.indent(indentation) + options.emptyArrayMsg);
85 } else {
866 data.forEach(function(element) {
87 // Prepend the dash at the begining of each array's element line
8813 var line = Utils.indent(indentation) + ('- ')[options.dashColor];
89
90 // If the element of the array is a string, render it in the same line
9113 if (typeof element === 'string') {
9211 line += exports.render(element, options);
9311 output.push(line);
94
95 // If the element of the array is an array or object, render it in next line
96 } else {
972 output.push(line);
982 output.push(
99 exports.render(element, options, indentation + options.defaultIndentation)
100 );
101 }
102 });
103 }
104 }
10512 else if (typeof data === 'object') {
106 // Get the size of the longest index to render all the values on the same column
10712 var maxIndexLength = Utils.getMaxIndexLength(data);
10812 var key;
109
11012 for(var i in data) {
111 // Prepend the index at the beginning of the line
11224 key = Utils.indent(indentation) + (i + ': ')[options.keysColor];
113
114 // If the value is serializable, render it in the same line
11524 if (isSerializable(data[i])) {
11618 key += exports.render(data[i], options, maxIndexLength - i.length);
11718 output.push(key);
118
119 // If the index is an array or object, render it in next line
120 } else {
1216 output.push(key);
1226 output.push(
123 exports.render(data[i], options, indentation + options.defaultIndentation)
124 );
125 }
126 }
127 }
128 // Return all the lines as a string
12955 return output.join('\n');
130};
131
132// ### Render from string function
133// *Parameters:*
134//
135// * **`data`**: Data to render as a string
136// * **`options`**: Hash with different options to configure the parser
137// * **`indentation`**: Base indentation of the parsed output
138//
139// *Example of options hash:*
140//
141// {
142// emptyArrayMsg: '(empty)', // Rendered message on empty strings
143// keysColor: 'blue', // Color for keys in hashes
144// dashColor: 'red', // Color for the dashes in arrays
145// defaultIndentation: 2 // Indentation on nested objects
146// }
1471exports.renderString = function renderString(data, options, indentation) {
1485 "use strict";
149
1505 var output = '';
1515 var parsedData;
152 // If the input is not a string or if it's empty, just return an empty string
1535 if (typeof data !== 'string' || data === '') {
1542 return '';
155 }
156
157 // Remove non-JSON characters from the beginning string
1583 if (data.indexOf('{') !== 0) {
1592 output += data.substr(0, data.indexOf('{')) + "\n";
1602 data = data.substr(data.indexOf('{'));
161 }
162
1633 try {
1643 parsedData = JSON.parse(data);
165 } catch (e) {
166 // Return an error in case of an invalid JSON
1671 return 'Error:'.red + ' Not valid JSON!';
168 }
169
170 // Call the real render() method
1712 output += exports.render(parsedData);
1722 return output;
173};

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) {
774 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) {
1412 var maxWidth = 0;
1512 var key;
16
1712 for (key in input) {
1824 if (key.length > maxWidth) {
1917 maxWidth = key.length;
20 }
21 }
2212 return maxWidth;
23};