Coverage

98%
63
62
1

prettyjson.js

98%
53
52
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) {
3251 "use strict";
33
34 // Default value for the indentation param
3551 indentation = indentation || 0;
36
37 // Default values for the options
3851 options = options || {};
3951 options.emptyArrayMsg = options.emptyArrayMsg || '(empty array)';
4051 options.keysColor = options.keysColor || "green";
4151 options.dashColor = options.dashColor || "green";
4251 options.defaultIndentation = options.defaultIndentation || 2;
43
44 // Initialize the output (it's an array of lines)
4551 var output = [];
46
47 // Helper function to detect if an object can be serializable directly
4851 var isSerializable = function(input) {
4973 if (typeof input === 'string' || typeof input === 'boolean' ||
50 typeof input === 'number' || input === null) {
5149 return true;
52 }
5324 return false;
54 };
55
5651 var addColorToData = function(input) {
5733 if (typeof input === 'string') {
58 // Print strings in regular terminal color
5929 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
7851 if (isSerializable(data)) {
7933 output.push(Utils.indent(indentation) + addColorToData(data));
80 }
8118 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 }
10510 else if (typeof data === 'object') {
106 // Get the size of the longest index to render all the values on the same column
10710 var maxIndexLength = Utils.getMaxIndexLength(data);
10810 var key;
109
11010 for(var i in data) {
111 // Prepend the index at the beginning of the line
11222 key = Utils.indent(indentation) + (i + ': ')[options.keysColor];
113
114 // If the value is serializable, render it in the same line
11522 if (isSerializable(data[i])) {
11616 key += exports.render(data[i], options, maxIndexLength - i.length);
11716 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
12951 return output.join('\n');
130};

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) {
770 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) {
1410 var maxWidth = 0;
1510 var key;
16
1710 for (key in input) {
1822 if (key.length > maxWidth) {
1915 maxWidth = key.length;
20 }
21 }
2210 return maxWidth;
23};