prettyjson
Advanced tools
Comparing version 1.1.3 to 1.2.0
'use strict'; | ||
// ### Module dependencies | ||
require('colors'); | ||
var colors = require('colors/safe'); | ||
var Utils = require('./utils'); | ||
@@ -9,2 +9,165 @@ | ||
// Helper function to detect if an object can be directly serializable | ||
var isSerializable = function(input, onlyPrimitives, options) { | ||
if (typeof input === 'boolean' || | ||
typeof input === 'number' || input === null || | ||
input instanceof Date) { | ||
return true; | ||
} | ||
if (typeof input === 'string' && input.indexOf('\n') === -1) { | ||
return true; | ||
} | ||
if (options.inlineArrays && !onlyPrimitives) { | ||
if (Array.isArray(input) && isSerializable(input[0], true, options)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
var addColorToData = function(input, options) { | ||
if (options.noColor) { | ||
return input; | ||
} | ||
if (typeof input === 'string') { | ||
// Print strings in regular terminal color | ||
return options.stringColor ? colors[options.stringColor](input) : input; | ||
} | ||
var sInput = input + ''; | ||
if (input === true) { | ||
return colors.green(sInput); | ||
} | ||
if (input === false) { | ||
return colors.red(sInput); | ||
} | ||
if (input === null) { | ||
return colors.grey(sInput); | ||
} | ||
if (typeof input === 'number') { | ||
return colors[options.numberColor](sInput); | ||
} | ||
if (Array.isArray(input)) { | ||
return input.join(', '); | ||
} | ||
return sInput; | ||
}; | ||
var indentLines = function(string, spaces){ | ||
var lines = string.split('\n'); | ||
lines = lines.map(function(line){ | ||
return Utils.indent(spaces) + line; | ||
}); | ||
return lines.join('\n'); | ||
}; | ||
var renderToArray = function(data, options, indentation) { | ||
if (isSerializable(data, false, options)) { | ||
return [Utils.indent(indentation) + addColorToData(data, options)]; | ||
} | ||
//unserializable string means it's multiline | ||
if (typeof data === 'string') { | ||
return [ | ||
Utils.indent(indentation) + '"""', | ||
indentLines(data, indentation + options.defaultIndentation), | ||
Utils.indent(indentation) + '"""' | ||
]; | ||
} | ||
if (Array.isArray(data)) { | ||
// If the array is empty, render the `emptyArrayMsg` | ||
if (data.length === 0) { | ||
return [Utils.indent(indentation) + options.emptyArrayMsg]; | ||
} | ||
var outputArray = []; | ||
data.forEach(function(element) { | ||
// Prepend the dash at the begining of each array's element line | ||
var line = '- '; | ||
if (!options.noColor) { | ||
line = colors[options.dashColor](line); | ||
} | ||
line = Utils.indent(indentation) + line; | ||
// If the element of the array is a string, bool, number, or null | ||
// render it in the same line | ||
if (isSerializable(element, false, options)) { | ||
line += renderToArray(element, options, 0)[0]; | ||
outputArray.push(line); | ||
// If the element is an array or object, render it in next line | ||
} else { | ||
outputArray.push(line); | ||
outputArray.push.apply( | ||
outputArray, | ||
renderToArray( | ||
element, options, indentation + options.defaultIndentation | ||
) | ||
); | ||
} | ||
}); | ||
return outputArray; | ||
} | ||
if (data instanceof Error) { | ||
return renderToArray( | ||
{ | ||
message: data.message, | ||
stack: data.stack.split('\n') | ||
}, | ||
options, | ||
indentation | ||
); | ||
} | ||
// If values alignment is enabled, get the size of the longest index | ||
// to align all the values | ||
var maxIndexLength = options.noAlign ? 0 : Utils.getMaxIndexLength(data); | ||
var key; | ||
var output = []; | ||
Object.getOwnPropertyNames(data).forEach(function(i) { | ||
// Prepend the index at the beginning of the line | ||
key = (i + ': '); | ||
if (!options.noColor) { | ||
key = colors[options.keysColor](key); | ||
} | ||
key = Utils.indent(indentation) + key; | ||
// Skip `undefined`, it's not a valid JSON value. | ||
if (data[i] === undefined) { | ||
return; | ||
} | ||
// If the value is serializable, render it in the same line | ||
if (isSerializable(data[i], false, options)) { | ||
var nextIndentation = options.noAlign ? 0 : maxIndexLength - i.length; | ||
key += renderToArray(data[i], options, nextIndentation)[0]; | ||
output.push(key); | ||
// If the index is an array or object, render it in next line | ||
} else { | ||
output.push(key); | ||
output.push.apply( | ||
output, | ||
renderToArray( | ||
data[i], | ||
options, | ||
indentation + options.defaultIndentation | ||
) | ||
); | ||
} | ||
}); | ||
return output; | ||
}; | ||
// ### Render function | ||
@@ -36,144 +199,7 @@ // *Parameters:* | ||
options.noColor = !!options.noColor; | ||
options.noAlign = !!options.noAlign; | ||
options.stringColor = options.stringColor || null; | ||
var output = []; | ||
// Helper function to detect if an object can be directly serializable | ||
var isSerializable = function(input, onlyPrimitives) { | ||
if (typeof input === 'boolean' || | ||
typeof input === 'number' || input === null || | ||
input instanceof Date) { | ||
return true; | ||
} | ||
if (typeof input === 'string' && input.indexOf('\n') === -1) { | ||
return true; | ||
} | ||
if (options.inlineArrays && !onlyPrimitives) { | ||
if (Array.isArray(input) && isSerializable(input[0], true)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
var indentLines = function(string, spaces){ | ||
var lines = string.split('\n'); | ||
lines = lines.map(function(line){ | ||
return Utils.indent(spaces) + line; | ||
}); | ||
return lines.join('\n'); | ||
}; | ||
var addColorToData = function(input) { | ||
if (options.noColor) { | ||
return input; | ||
} | ||
if (typeof input === 'string') { | ||
// Print strings in regular terminal color | ||
return options.stringColor ? input[options.stringColor] : input; | ||
} | ||
var sInput = input + ''; | ||
if (input === true) { | ||
return sInput.green; | ||
} | ||
if (input === false) { | ||
return sInput.red; | ||
} | ||
if (input === null) { | ||
return sInput.grey; | ||
} | ||
if (typeof input === 'number') { | ||
return sInput[options.numberColor]; | ||
} | ||
if (Array.isArray(input)) { | ||
return input.join(', '); | ||
} | ||
return sInput; | ||
}; | ||
// Render a string exactly equal | ||
if (isSerializable(data)) { | ||
output.push(Utils.indent(indentation) + addColorToData(data)); | ||
} | ||
else if (typeof data === 'string') { | ||
//unserializable string means it's multiline | ||
output.push(Utils.indent(indentation) + '"""'); | ||
output.push(indentLines(data, indentation + options.defaultIndentation)); | ||
output.push(Utils.indent(indentation) + '"""'); | ||
} | ||
else if (Array.isArray(data)) { | ||
// If the array is empty, render the `emptyArrayMsg` | ||
if (data.length === 0) { | ||
output.push(Utils.indent(indentation) + options.emptyArrayMsg); | ||
} else { | ||
data.forEach(function(element) { | ||
// Prepend the dash at the begining of each array's element line | ||
var line = ('- '); | ||
if (!options.noColor) { | ||
line = line[options.dashColor]; | ||
} | ||
line = Utils.indent(indentation) + line; | ||
// If the element of the array is a string, bool, number, or null | ||
// render it in the same line | ||
if (isSerializable(element)) { | ||
line += exports.render(element, options); | ||
output.push(line); | ||
// If the element is an array or object, render it in next line | ||
} else { | ||
output.push(line); | ||
output.push(exports.render( | ||
element, options, indentation + options.defaultIndentation | ||
)); | ||
} | ||
}); | ||
} | ||
} | ||
else if (typeof data === 'object') { | ||
// Get the size of the longest index to align all the values | ||
var maxIndexLength = Utils.getMaxIndexLength(data); | ||
var key; | ||
var isError = data instanceof Error; | ||
Object.getOwnPropertyNames(data).forEach(function(i) { | ||
// Prepend the index at the beginning of the line | ||
key = (i + ': '); | ||
if (!options.noColor) { | ||
key = key[options.keysColor]; | ||
} | ||
key = Utils.indent(indentation) + key; | ||
// Skip `undefined`, it's not a valid JSON value. | ||
if (data[i] === undefined) { | ||
return; | ||
} | ||
// If the value is serializable, render it in the same line | ||
if (isSerializable(data[i]) && (!isError || i !== 'stack')) { | ||
key += exports.render(data[i], options, maxIndexLength - i.length); | ||
output.push(key); | ||
// If the index is an array or object, render it in next line | ||
} else { | ||
output.push(key); | ||
output.push( | ||
exports.render( | ||
isError && i === 'stack' ? data[i].split('\n') : data[i], | ||
options, | ||
indentation + options.defaultIndentation | ||
) | ||
); | ||
} | ||
}); | ||
} | ||
// Return all the lines as a string | ||
return output.join('\n'); | ||
return renderToArray(data, options, indentation).join('\n'); | ||
}; | ||
@@ -225,3 +251,3 @@ | ||
// Return an error in case of an invalid JSON | ||
return 'Error:'.red + ' Not valid JSON!'; | ||
return colors.red('Error:') + ' Not valid JSON!'; | ||
} | ||
@@ -228,0 +254,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"description": "Package for formatting JSON data in a coloured YAML-style, perfect for CLI output", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"homepage": "http://rafeca.com/prettyjson", | ||
@@ -26,3 +26,3 @@ "keywords": [ | ||
"testwin": "node ./node_modules/mocha/bin/mocha --reporter spec", | ||
"jshint": "jshint lib/*.js", | ||
"jshint": "jshint lib/*.js test/*.js", | ||
"coverage": "istanbul cover _mocha --report lcovonly -- -R spec", | ||
@@ -37,12 +37,12 @@ "coveralls": "npm run coverage && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage", | ||
"colors": "^1.1.2", | ||
"minimist": "^1.1.3" | ||
"minimist": "^1.2.0" | ||
}, | ||
"devDependencies": { | ||
"coveralls": "^2.11.3", | ||
"istanbul": "^0.3.17", | ||
"jshint": "^2.4.4", | ||
"mocha": "^2.2.5", | ||
"mocha-lcov-reporter": "0.0.2", | ||
"should": "^7.0.2" | ||
"coveralls": "^2.11.15", | ||
"istanbul": "^0.4.5", | ||
"jshint": "^2.9.4", | ||
"mocha": "^3.1.2", | ||
"mocha-lcov-reporter": "^1.2.0", | ||
"should": "^11.1.1" | ||
} | ||
} |
@@ -1,8 +0,13 @@ | ||
var prettyjson = process.env.EXPRESS_COV ? require('../lib-cov/prettyjson') : require('../lib/prettyjson'); | ||
var should = require('should'); | ||
'use strict'; | ||
require('should'); | ||
var prettyjson = process.env.EXPRESS_COV | ||
? require('../lib-cov/prettyjson') | ||
: require('../lib/prettyjson'); | ||
var colors = require('colors/safe'); | ||
describe('prettyjson general tests', function() { | ||
it("should output a string exactly equal as the input", function() { | ||
it('should output a string exactly equal as the input', function() { | ||
var input = 'This is a string'; | ||
@@ -14,4 +19,3 @@ var output = prettyjson.render(input); | ||
it("should output a string with indentation", function() { | ||
it('should output a string with indentation', function() { | ||
var input = 'This is a string'; | ||
@@ -23,5 +27,4 @@ var output = prettyjson.render(input, {}, 4); | ||
it("should output a multiline string with indentation", function() { | ||
var input = 'multiple\nlines' | ||
it('should output a multiline string with indentation', function() { | ||
var input = 'multiple\nlines'; | ||
var output = prettyjson.render(input, {}, 4); | ||
@@ -32,4 +35,3 @@ | ||
it("should output an array of strings", function() { | ||
it('should output an array of strings', function() { | ||
var input = ['first string', 'second string']; | ||
@@ -39,9 +41,8 @@ var output = prettyjson.render(input); | ||
output.should.equal([ | ||
'- '.green + input[0], | ||
'- '.green + input[1] | ||
colors.green('- ') + input[0], | ||
colors.green('- ') + input[1] | ||
].join('\n')); | ||
}); | ||
it("should output an array of arrays", function() { | ||
it('should output an array of arrays', function() { | ||
var input = ['first string', ['nested 1', 'nested 2'], 'second string']; | ||
@@ -51,12 +52,11 @@ var output = prettyjson.render(input); | ||
output.should.equal([ | ||
'- '.green + input[0], | ||
'- '.green, | ||
' ' + '- '.green + input[1][0], | ||
' ' + '- '.green + input[1][1], | ||
'- '.green + input[2] | ||
colors.green('- ') + input[0], | ||
colors.green('- '), | ||
' ' + colors.green('- ') + input[1][0], | ||
' ' + colors.green('- ') + input[1][1], | ||
colors.green('- ') + input[2] | ||
].join('\n')); | ||
}); | ||
it("should output a hash of strings", function() { | ||
it('should output a hash of strings', function() { | ||
var input = {param1: 'first string', param2: 'second string'}; | ||
@@ -66,45 +66,56 @@ var output = prettyjson.render(input); | ||
output.should.equal([ | ||
'param1: '.green + 'first string', | ||
'param2: '.green + 'second string' | ||
colors.green('param1: ') + 'first string', | ||
colors.green('param2: ') + 'second string' | ||
].join('\n')); | ||
}); | ||
it("should output a hash of hashes", function() { | ||
var input = {first_param: {subparam: 'first string', subparam2: 'another string'}, second_param: 'second string'}; | ||
it('should output a hash of hashes', function() { | ||
var input = { | ||
firstParam: {subparam: 'first string', subparam2: 'another string'}, | ||
secondParam: 'second string' | ||
}; | ||
var output = prettyjson.render(input); | ||
output.should.equal([ | ||
'first_param: '.green, | ||
' ' + 'subparam: '.green + ' first string', | ||
' ' + 'subparam2: '.green + 'another string', | ||
'second_param: '.green + 'second string' | ||
colors.green('firstParam: '), | ||
' ' + colors.green('subparam: ') + ' first string', | ||
' ' + colors.green('subparam2: ') + 'another string', | ||
colors.green('secondParam: ') + 'second string' | ||
].join('\n')); | ||
}); | ||
it("should indent correctly the hashes keys", function() { | ||
it('should indent correctly the hashes keys', function() { | ||
var input = {very_large_param: 'first string', param: 'second string'}; | ||
var input = {veryLargeParam: 'first string', param: 'second string'}; | ||
var output = prettyjson.render(input); | ||
output.should.equal([ | ||
'very_large_param: '.green + 'first string', | ||
'param: '.green + ' second string' | ||
colors.green('veryLargeParam: ') + 'first string', | ||
colors.green('param: ') + ' second string' | ||
].join('\n')); | ||
}); | ||
it("should output a really nested object", function() { | ||
it('should allow to disable values aligning with longest index', function() { | ||
var input = {veryLargeParam: 'first string', param: 'second string'}; | ||
var output = prettyjson.render(input, {noAlign: true}); | ||
output.should.equal([ | ||
colors.green('veryLargeParam: ') + 'first string', | ||
colors.green('param: ') + 'second string' | ||
].join('\n')); | ||
}); | ||
it('should output a really nested object', function() { | ||
var input = { | ||
first_param: { | ||
firstParam: { | ||
subparam: 'first string', | ||
subparam2: 'another string', | ||
subparam3: ["different", "values", "in an array"] | ||
subparam3: ['different', 'values', 'in an array'] | ||
}, | ||
second_param: 'second string', | ||
an_array: [{ | ||
secondParam: 'second string', | ||
anArray: [{ | ||
param3: 'value', | ||
param10: 'other value' | ||
}], | ||
empty_array: [] | ||
emptyArray: [] | ||
}; | ||
@@ -115,15 +126,15 @@ | ||
output.should.equal([ | ||
'first_param: '.green, | ||
' ' + 'subparam: '.green + ' first string', | ||
' ' + 'subparam2: '.green + 'another string', | ||
' ' + 'subparam3: '.green, | ||
' ' + '- '.green + 'different', | ||
' ' + '- '.green + 'values', | ||
' ' + '- '.green + 'in an array', | ||
'second_param: '.green + 'second string', | ||
'an_array: '.green, | ||
' ' + '- '.green, | ||
' ' + 'param3: '.green + ' value', | ||
' ' + 'param10: '.green + 'other value', | ||
'empty_array: '.green, | ||
colors.green('firstParam: '), | ||
' ' + colors.green('subparam: ') + ' first string', | ||
' ' + colors.green('subparam2: ') + 'another string', | ||
' ' + colors.green('subparam3: '), | ||
' ' + colors.green('- ') + 'different', | ||
' ' + colors.green('- ') + 'values', | ||
' ' + colors.green('- ') + 'in an array', | ||
colors.green('secondParam: ') + 'second string', | ||
colors.green('anArray: '), | ||
' ' + colors.green('- '), | ||
' ' + colors.green('param3: ') + ' value', | ||
' ' + colors.green('param10: ') + 'other value', | ||
colors.green('emptyArray: '), | ||
' (empty array)' | ||
@@ -133,3 +144,3 @@ ].join('\n')); | ||
it("should allow to configure colors for hash keys", function() { | ||
it('should allow to configure colors for hash keys', function() { | ||
var input = {param1: 'first string', param2: 'second string'}; | ||
@@ -139,8 +150,8 @@ var output = prettyjson.render(input, {keysColor: 'blue'}); | ||
output.should.equal([ | ||
'param1: '.blue + 'first string', | ||
'param2: '.blue + 'second string' | ||
colors.blue('param1: ') + 'first string', | ||
colors.blue('param2: ') + 'second string' | ||
].join('\n')); | ||
}); | ||
it("should allow to configure colors for numbers", function() { | ||
it('should allow to configure colors for numbers', function() { | ||
var input = {param1: 17, param2: 22.3}; | ||
@@ -150,29 +161,29 @@ var output = prettyjson.render(input, {numberColor: 'red'}); | ||
output.should.equal([ | ||
'param1: '.green + '17'.red, | ||
'param2: '.green + '22.3'.red | ||
colors.green('param1: ') + colors.red('17'), | ||
colors.green('param2: ') + colors.red('22.3') | ||
].join('\n')); | ||
}); | ||
it("should allow to configure rainbow as color", function() { | ||
var input = {param_long: 'first string', param2: 'second string'}; | ||
it('should allow to configure rainbow as color', function() { | ||
var input = {paramLong: 'first string', param2: 'second string'}; | ||
var output = prettyjson.render(input, {keysColor: 'rainbow'}); | ||
output.should.equal([ | ||
'param_long: '.rainbow + 'first string', | ||
'param2: '.rainbow + ' second string' | ||
colors.rainbow('paramLong: ') + 'first string', | ||
colors.rainbow('param2: ') + ' second string' | ||
].join('\n')); | ||
}); | ||
it("should allow to configure the default indentation", function() { | ||
var input = {param: ['first string', "second string"]}; | ||
it('should allow to configure the default indentation', function() { | ||
var input = {param: ['first string', 'second string']}; | ||
var output = prettyjson.render(input, {defaultIndentation: 4}); | ||
output.should.equal([ | ||
'param: '.green, | ||
' ' + '- '.green + 'first string', | ||
' ' + '- '.green + 'second string' | ||
colors.green('param: '), | ||
' ' + colors.green('- ') + 'first string', | ||
' ' + colors.green('- ') + 'second string' | ||
].join('\n')); | ||
}); | ||
it("should allow to configure the empty message for arrays", function() { | ||
it('should allow to configure the empty message for arrays', function() { | ||
var input = []; | ||
@@ -186,13 +197,16 @@ var output = prettyjson.render(input, {emptyArrayMsg: '(empty)'}); | ||
it("should allow to configure colors for strings", function() { | ||
it('should allow to configure colors for strings', function() { | ||
var input = {param1: 'first string', param2: 'second string'}; | ||
var output = prettyjson.render(input, {keysColor: 'blue', stringColor: 'red'}); | ||
var output = prettyjson.render( | ||
input, | ||
{keysColor: 'blue', stringColor: 'red'} | ||
); | ||
output.should.equal([ | ||
'param1: '.blue + 'first string'.red, | ||
'param2: '.blue + 'second string'.red | ||
colors.blue('param1: ') + colors.red('first string'), | ||
colors.blue('param2: ') + colors.red('second string') | ||
].join('\n')); | ||
}); | ||
it("should allow to not use colors", function() { | ||
it('should allow to not use colors', function() { | ||
var input = {param1: 'first string', param2: ['second string']}; | ||
@@ -208,3 +222,3 @@ var output = prettyjson.render(input, {noColor: true}); | ||
it("should allow to print simple arrays inline", function() { | ||
it('should allow to print simple arrays inline', function() { | ||
var input = {installs: ['first string', 'second string', false, 13]}; | ||
@@ -214,3 +228,3 @@ var output = prettyjson.render(input, {inlineArrays: true}); | ||
output.should.equal( | ||
'installs: '.green + 'first string, second string, false, 13'); | ||
colors.green('installs: ') + 'first string, second string, false, 13'); | ||
@@ -221,9 +235,9 @@ input = {installs: [ ['first string', 'second string'], 'third string']}; | ||
output.should.equal([ | ||
'installs: '.green, | ||
' ' + '- '.green + 'first string, second string', | ||
' ' + '- '.green + 'third string' | ||
].join('\n')); | ||
colors.green('installs: '), | ||
' ' + colors.green('- ') + 'first string, second string', | ||
' ' + colors.green('- ') + 'third string' | ||
].join('\n')); | ||
}); | ||
it("should not print an object prototype", function() { | ||
it('should not print an object prototype', function() { | ||
var Input = function() { | ||
@@ -235,7 +249,7 @@ this.param1 = 'first string'; | ||
var output = prettyjson.render(new Input); | ||
var output = prettyjson.render(new Input()); | ||
output.should.equal([ | ||
'param1: '.green + 'first string', | ||
'param2: '.green + 'second string' | ||
colors.green('param1: ') + 'first string', | ||
colors.green('param2: ') + 'second string' | ||
].join('\n')); | ||
@@ -246,14 +260,14 @@ }); | ||
describe('Printing numbers, booleans and other objects', function() { | ||
it("should print numbers correctly ", function() { | ||
it('should print numbers correctly ', function() { | ||
var input = 12345; | ||
var output = prettyjson.render(input, {}, 4); | ||
output.should.equal(' ' + '12345'.blue); | ||
output.should.equal(' ' + colors.blue('12345')); | ||
}); | ||
it("should print booleans correctly ", function() { | ||
it('should print booleans correctly ', function() { | ||
var input = true; | ||
var output = prettyjson.render(input, {}, 4); | ||
output.should.equal(' ' + 'true'.green); | ||
output.should.equal(' ' + colors.green('true')); | ||
@@ -263,13 +277,13 @@ input = false; | ||
output.should.equal(' ' + 'false'.red); | ||
output.should.equal(' ' + colors.red('false')); | ||
}); | ||
it("should print a null object correctly ", function() { | ||
it('should print a null object correctly ', function() { | ||
var input = null; | ||
var output = prettyjson.render(input, {}, 4); | ||
output.should.equal(' ' + 'null'.grey); | ||
output.should.equal(' ' + colors.grey('null')); | ||
}); | ||
it("should print an Error correctly ", function() { | ||
it('should print an Error correctly ', function() { | ||
Error.stackTraceLimit = 1; | ||
@@ -281,6 +295,6 @@ var input = new Error('foo'); | ||
output.should.equal([ | ||
' ' + 'stack: '.green, | ||
' ' + '- '.green + stack[0], | ||
' ' + '- '.green + stack[1], | ||
' ' + 'message: '.green + 'foo' | ||
' ' + colors.green('message: ') + 'foo', | ||
' ' + colors.green('stack: '), | ||
' ' + colors.green('- ') + stack[0], | ||
' ' + colors.green('- ') + stack[1], | ||
].join('\n')); | ||
@@ -290,12 +304,12 @@ }); | ||
it('should print serializable items in an array inline', function() { | ||
var dt = new Date(); | ||
var dt = new Date(); | ||
var output = prettyjson.render([ 'a', 3, null, true, false, dt]); | ||
output.should.equal([ | ||
'- '.green + 'a', | ||
'- '.green + '3'.blue, | ||
'- '.green + 'null'.grey, | ||
'- '.green + 'true'.green, | ||
'- '.green + 'false'.red, | ||
'- '.green + dt | ||
colors.green('- ') + 'a', | ||
colors.green('- ') + colors.blue('3'), | ||
colors.green('- ') + colors.grey('null'), | ||
colors.green('- ') + colors.green('true'), | ||
colors.green('- ') + colors.red('false'), | ||
colors.green('- ') + dt | ||
].join('\n')); | ||
@@ -305,28 +319,29 @@ }); | ||
it('should print dates correctly', function() { | ||
var input = new Date(); | ||
var expected = input.toString(); | ||
var output = prettyjson.render(input, {}, 4); | ||
var input = new Date(); | ||
var expected = input.toString(); | ||
var output = prettyjson.render(input, {}, 4); | ||
output.should.equal(' ' + expected); | ||
output.should.equal(' ' + expected); | ||
}); | ||
it('should print dates in objects correctly', function() { | ||
var dt1 = new Date(); | ||
var dt2 = new Date(); | ||
var dt1 = new Date(); | ||
var dt2 = new Date(); | ||
var input = { | ||
dt1: dt2, | ||
dt2: dt2 | ||
}; | ||
var input = { | ||
dt1: dt2, | ||
dt2: dt2 | ||
}; | ||
var output = prettyjson.render(input, {}, 4); | ||
var output = prettyjson.render(input, {}, 4); | ||
output.should.equal([ | ||
' ' + 'dt1: '.green + dt1.toString(), | ||
' ' + 'dt2: '.green + dt2.toString()].join('\n')); | ||
output.should.equal([ | ||
' ' + colors.green('dt1: ') + dt1.toString(), | ||
' ' + colors.green('dt2: ') + dt2.toString() | ||
].join('\n')); | ||
}); | ||
}); | ||
describe('prettyjson.renderString() method', function(){ | ||
it('should return an empty string if input is empty', function(){ | ||
describe('prettyjson.renderString() method', function() { | ||
it('should return an empty string if input is empty', function() { | ||
var input = ''; | ||
@@ -339,3 +354,3 @@ | ||
it('should return an empty string if input is not a string', function(){ | ||
it('should return an empty string if input is not a string', function() { | ||
var output = prettyjson.renderString({}); | ||
@@ -345,26 +360,49 @@ output.should.equal(''); | ||
it('should return an error message if the input is an invalid JSON string', function(){ | ||
var output = prettyjson.renderString('not valid!!'); | ||
output.should.equal('Error:'.red + ' Not valid JSON!'); | ||
}); | ||
it( | ||
'should return an error message if the input is an invalid JSON string', | ||
function() { | ||
var output = prettyjson.renderString('not valid!!'); | ||
output.should.equal(colors.red('Error:') + ' Not valid JSON!'); | ||
} | ||
); | ||
it('should return the prettyfied string if it is a valid JSON string', function(){ | ||
var output = prettyjson.renderString('{"test": "OK"}'); | ||
output.should.equal('test: '.green + 'OK'); | ||
}); | ||
it( | ||
'should return the prettyfied string if it is a valid JSON string', | ||
function() { | ||
var output = prettyjson.renderString('{"test": "OK"}'); | ||
output.should.equal(colors.green('test: ') + 'OK'); | ||
} | ||
); | ||
it('should dismiss trailing characters which are not JSON', function(){ | ||
var output = prettyjson.renderString('characters that are not JSON at all... {"test": "OK"}'); | ||
output.should.equal("characters that are not JSON at all... \n" + 'test: '.green + 'OK'); | ||
it('should dismiss trailing characters which are not JSON', function() { | ||
var output = prettyjson.renderString( | ||
'characters that are not JSON at all... {"test": "OK"}' | ||
); | ||
output.should.equal( | ||
'characters that are not JSON at all... \n' + | ||
colors.green('test: ') + | ||
'OK' | ||
); | ||
}); | ||
it('should dismiss trailing characters which are not JSON with an array', function(){ | ||
var output = prettyjson.renderString('characters that are not JSON at all... ["test"]'); | ||
output.should.equal("characters that are not JSON at all... \n" + '- '.green + 'test'); | ||
}); | ||
it( | ||
'should dismiss trailing characters which are not JSON with an array', | ||
function() { | ||
var output = prettyjson.renderString( | ||
'characters that are not JSON at all... ["test"]' | ||
); | ||
output.should.equal( | ||
'characters that are not JSON at all... \n' + | ||
colors.green('- ') + | ||
'test' | ||
); | ||
} | ||
); | ||
it('should be able to accept the options parameter', function(){ | ||
var output = prettyjson.renderString('{"test": "OK"}', {stringColor: 'red'}); | ||
output.should.equal('test: '.green + 'OK'.red); | ||
it('should be able to accept the options parameter', function() { | ||
var output = prettyjson.renderString( | ||
'{"test": "OK"}', {stringColor: 'red'} | ||
); | ||
output.should.equal(colors.green('test: ') + colors.red('OK')); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
625064
555
Updatedminimist@^1.2.0