Coverage

97%
83
81
2

prettyjson.js

97%
73
71
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// defaultIndentation: 2 // Indentation on nested objects
30// }
311exports.render = function render(data, options, indentation) {
3257 "use strict";
33
34 // Default value for the indentation param
3557 indentation = indentation || 0;
36
37 // Default values for the options
3857 options = options || {};
3957 options.emptyArrayMsg = options.emptyArrayMsg || '(empty array)';
4057 options.keysColor = options.keysColor || "green";
4157 options.dashColor = options.dashColor || "green";
4257 options.defaultIndentation = options.defaultIndentation || 2;
43
44 // Initialize the output (it's an array of lines)
4557 var output = [];
46
47 // Helper function to detect if an object can be serializable directly
4857 var isSerializable = function(input) {
4981 if (typeof input === 'string' || typeof input === 'boolean' ||
50 typeof input === 'number' || input === null) {
5154 return true;
52 }
5327 return false;
54 };
55
5657 var addColorToData = function(input) {
5736 if (typeof input === 'string') {
58 // Print strings in regular terminal color
5932 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
7857 if (isSerializable(data)) {
7936 output.push(Utils.indent(indentation) + addColorToData(data));
80 }
8121 else if (Array.isArray(data)) {
82 // If the array is empty, render the `emptyArrayMsg`
839 if (data.length === 0) {
842 output.push(Utils.indent(indentation) + options.emptyArrayMsg);
85 } else {
867 data.forEach(function(element) {
87 // Prepend the dash at the begining of each array's element line
8814 var line = Utils.indent(indentation) + ('- ')[options.dashColor];
89
90 // If the element of the array is a string, render it in the same line
9114 if (typeof element === 'string') {
9212 line += exports.render(element, options);
9312 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
12957 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) {
1486 "use strict";
149
1506 var output = '';
1516 var parsedData;
152 // If the input is not a string or if it's empty, just return an empty string
1536 if (typeof data !== 'string' || data === '') {
1542 return '';
155 }
156
157 // Remove non-JSON characters from the beginning string
1584 if (data[0] !== '{' && data[0] !== '[') {
1593 var beginingOfJson;
1603 if (data.indexOf('{') === -1) {
1612 beginingOfJson = data.indexOf('[');
1621 } else if (data.indexOf('[') === -1) {
1631 beginingOfJson = data.indexOf('{');
164 } else {
1650 beginingOfJson = data.indexOf('{') < data.indexOf('[') ? data.indexOf('{') : data.indexOf('[');
166 }
1673 output += data.substr(0, beginingOfJson) + "\n";
1683 data = data.substr(beginingOfJson);
169 }
170
1714 try {
1724 parsedData = JSON.parse(data);
173 } catch (e) {
174 // Return an error in case of an invalid JSON
1751 return 'Error:'.red + ' Not valid JSON!';
176 }
177
178 // Call the real render() method
1793 output += exports.render(parsedData);
1803 return output;
181};

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) {
776 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};