# Coverage

98%
63
62
1

## prettyjson.js

98%
53
52
1

Line | Hits | Source
--- | --- | ---
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 |  | // defaultIndentation: 2 // Indentation on nested objects
30 |  | // }
31 | 1 | exports.render = function render(data, options, indentation) {
32 | 51 | "use strict";
33 |  |
34 |  | // Default value for the indentation param
35 | 51 | indentation = indentation || 0;
36 |  |
37 |  | // Default values for the options
38 | 51 | options = options || {};
39 | 51 | options.emptyArrayMsg = options.emptyArrayMsg || '(empty array)';
40 | 51 | options.keysColor = options.keysColor || "green";
41 | 51 | options.dashColor = options.dashColor || "green";
42 | 51 | options.defaultIndentation = options.defaultIndentation || 2;
43 |  |
44 |  | // Initialize the output (it's an array of lines)
45 | 51 | var output = [];
46 |  |
47 |  | // Helper function to detect if an object can be serializable directly
48 | 51 | var isSerializable = function(input) {
49 | 73 | if (typeof input === 'string' || typeof input === 'boolean' ||
50 |  | typeof input === 'number' || input === null) {
51 | 49 | return true;
52 |  | }
53 | 24 | return false;
54 |  | };
55 |  |
56 | 51 | var addColorToData = function(input) {
57 | 33 | if (typeof input === 'string') {
58 |  | // Print strings in regular terminal color
59 | 29 | return input;
60 |  | }
61 |  |
62 | 4 | if (input === true) {
63 | 1 | return (input+'').green;
64 |  | }
65 | 3 | if (input === false) {
66 | 1 | return (input+'').red;
67 |  | }
68 | 2 | if (input === null) {
69 | 1 | return (input+'').grey;
70 |  | }
71 | 1 | if (typeof input === 'number') {
72 | 1 | return (input+'').blue;
73 |  | }
74 | 0 | return (input+'');
75 |  | };
76 |  |
77 |  | // Render a string exactly equal
78 | 51 | if (isSerializable(data)) {
79 | 33 | output.push(Utils.indent(indentation) + addColorToData(data));
80 |  | }
81 | 18 | else if (Array.isArray(data)) {
82 |  | // If the array is empty, render the `emptyArrayMsg`
83 | 8 | if (data.length === 0) {
84 | 2 | output.push(Utils.indent(indentation) + options.emptyArrayMsg);
85 |  | } else {
86 | 6 | data.forEach(function(element) {
87 |  | // Prepend the dash at the begining of each array's element line
88 | 13 | var line = Utils.indent(indentation) + ('- ')[options.dashColor];
89 |  |
90 |  | // If the element of the array is a string, render it in the same line
91 | 13 | if (typeof element === 'string') {
92 | 11 | line += exports.render(element, options);
93 | 11 | output.push(line);
94 |  |
95 |  | // If the element of the array is an array or object, render it in next line
96 |  | } else {
97 | 2 | output.push(line);
98 | 2 | output.push(
99 |  | exports.render(element, options, indentation + options.defaultIndentation)
100 |  | );
101 |  | }
102 |  | });
103 |  | }
104 |  | }
105 | 10 | else if (typeof data === 'object') {
106 |  | // Get the size of the longest index to render all the values on the same column
107 | 10 | var maxIndexLength = Utils.getMaxIndexLength(data);
108 | 10 | var key;
109 |  |
110 | 10 | for(var i in data) {
111 |  | // Prepend the index at the beginning of the line
112 | 22 | key = Utils.indent(indentation) + (i + ': ')[options.keysColor];
113 |  |
114 |  | // If the value is serializable, render it in the same line
115 | 22 | if (isSerializable(data[i])) {
116 | 16 | key += exports.render(data[i], options, maxIndexLength - i.length);
117 | 16 | output.push(key);
118 |  |
119 |  | // If the index is an array or object, render it in next line
120 |  | } else {
121 | 6 | output.push(key);
122 | 6 | output.push(
123 |  | exports.render(data[i], options, indentation + options.defaultIndentation)
124 |  | );
125 |  | }
126 |  | }
127 |  | }
128 |  | // Return all the lines as a string
129 | 51 | return output.join('\n');
130 |  | };

## utils.js

100%
10
10
0

Line | Hits | Source
--- | --- | ---
1 | 1 | "use strict";
2 |  |
3 |  | /**
4 |  | * Creates a string with the same length as `numSpaces` parameter
5 |  | **/
6 | 1 | exports.indent = function indent(numSpaces) {
7 | 70 | return new Array(numSpaces+1).join(' ');
8 |  | };
9 |  |
10 |  | /**
11 |  | * Gets the string length of the longer index in a hash
12 |  | **/
13 | 1 | exports.getMaxIndexLength = function(input) {
14 | 10 | var maxWidth = 0;
15 | 10 | var key;
16 |  |
17 | 10 | for (key in input) {
18 | 22 | if (key.length > maxWidth) {
19 | 15 | maxWidth = key.length;
20 |  | }
21 |  | }
22 | 10 | return maxWidth;
23 |  | };