prettyjson
Advanced tools
Comparing version 0.6.0 to 0.7.0
@@ -0,1 +1,8 @@ | ||
### 0.7.0 — *October 25, 2012* | ||
* Allow having non-JSON characters at the beginning of the input string (ideal for curl -i) | ||
* Add a renderString() method to be used by the CLI | ||
* Change test reporter style to spec | ||
* Upgrade dependencies to the last versions | ||
### 0.6.0 — *June 29, 2012* | ||
@@ -2,0 +9,0 @@ |
@@ -24,3 +24,3 @@ // Package for formatting JSON data in a coloured | ||
// *Example of options hash:* | ||
// | ||
// | ||
// { | ||
@@ -37,3 +37,3 @@ // emptyArrayMsg: '(empty)', // Rendered message on empty strings | ||
indentation = indentation || 0; | ||
// Default values for the options | ||
@@ -45,6 +45,6 @@ options = options || {}; | ||
options.defaultIndentation = options.defaultIndentation || 2; | ||
// Initialize the output (it's an array of lines) | ||
var output = []; | ||
// Helper function to detect if an object can be serializable directly | ||
@@ -133,2 +133,45 @@ var isSerializable = function(input) { | ||
return output.join('\n'); | ||
}; | ||
// ### Render from string function | ||
// *Parameters:* | ||
// | ||
// * **`data`**: Data to render as a string | ||
// * **`options`**: Hash with different options to configure the parser | ||
// * **`indentation`**: Base indentation of the parsed output | ||
// | ||
// *Example of options hash:* | ||
// | ||
// { | ||
// emptyArrayMsg: '(empty)', // Rendered message on empty strings | ||
// keysColor: 'blue', // Color for keys in hashes | ||
// dashColor: 'red', // Color for the dashes in arrays | ||
// defaultIndentation: 2 // Indentation on nested objects | ||
// } | ||
exports.renderString = function renderString(data, options, indentation) { | ||
"use strict"; | ||
var output = ''; | ||
var parsedData; | ||
// If the input is not a string or if it's empty, just return an empty string | ||
if (typeof data !== 'string' || data === '') { | ||
return ''; | ||
} | ||
// Remove non-JSON characters from the beginning string | ||
if (data.indexOf('{') !== 0) { | ||
output += data.substr(0, data.indexOf('{')) + "\n"; | ||
data = data.substr(data.indexOf('{')); | ||
} | ||
try { | ||
parsedData = JSON.parse(data); | ||
} catch (e) { | ||
// Return an error in case of an invalid JSON | ||
return 'Error:'.red + ' Not valid JSON!'; | ||
} | ||
// Call the real render() method | ||
output += exports.render(parsedData); | ||
return output; | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"description": "Package for formatting JSON data in a coloured YAML-style, perfect for CLI output", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"homepage": "http://rafeca.com/prettyjson", | ||
@@ -20,3 +20,3 @@ "keywords": [ | ||
"scripts": { | ||
"test": "./node_modules/mocha/bin/mocha" | ||
"test": "./node_modules/mocha/bin/mocha --reporter spec" | ||
}, | ||
@@ -33,9 +33,9 @@ "bin": { | ||
"devDependencies": { | ||
"releasetools": "0.3.0", | ||
"releasetools": "0.4.0", | ||
"step": "0.0.5", | ||
"mocha": "1.2.2", | ||
"should": "0.6.3", | ||
"jake": "0.3.7", | ||
"jshint": "0.7.1" | ||
"mocha": "1.6.0", | ||
"should": "1.2.0", | ||
"jake": "0.5.5", | ||
"jshint": "0.9.1" | ||
} | ||
} | ||
} |
@@ -48,3 +48,3 @@ # prettyjson [![Build Status](https://secure.travis-ci.org/rafeca/prettyjson.png)](http://travis-ci.org/rafeca/prettyjson) | ||
```bash | ||
$ PRETTYJSON_KEYS=red PRETTYJSON_DASH=blue bin/prettyjson package.json | ||
$ PRETTYJSON_KEYS=red PRETTYJSON_DASH=blue prettyjson package.json | ||
``` | ||
@@ -51,0 +51,0 @@ |
@@ -6,6 +6,6 @@ var prettyjson = process.env.EXPRESS_COV | ||
describe('prettyjson general tests', function(){ | ||
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' | ||
@@ -17,4 +17,4 @@ 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' | ||
@@ -25,5 +25,5 @@ var output = prettyjson.render(input, {}, 4); | ||
}); | ||
it("should output an array of strings", function(){ | ||
it("should output an array of strings", function() { | ||
var input = ['first string', 'second string']; | ||
@@ -37,5 +37,5 @@ var output = prettyjson.render(input); | ||
}); | ||
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']; | ||
@@ -52,5 +52,5 @@ var output = prettyjson.render(input); | ||
}); | ||
it("should output a hash of strings", function(){ | ||
it("should output a hash of strings", function() { | ||
var input = {param1: 'first string', param2: 'second string'}; | ||
@@ -64,5 +64,5 @@ var output = prettyjson.render(input); | ||
}); | ||
it("should output a hash of hashes", function(){ | ||
it("should output a hash of hashes", function() { | ||
var input = {first_param: {subparam: 'first string', subparam2: 'another string'}, second_param: 'second string'}; | ||
@@ -79,4 +79,4 @@ var output = prettyjson.render(input); | ||
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'}; | ||
@@ -90,5 +90,5 @@ var output = prettyjson.render(input); | ||
}); | ||
it("should output a really nested object", function(){ | ||
it("should output a really nested object", function() { | ||
var input = { | ||
@@ -107,3 +107,3 @@ first_param: { | ||
}; | ||
var output = prettyjson.render(input); | ||
@@ -128,4 +128,4 @@ | ||
}); | ||
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,4 +139,4 @@ var output = prettyjson.render(input, {keysColor: 'blue'}); | ||
}); | ||
it("should allow to configure rainbow as color", function(){ | ||
it("should allow to configure rainbow as color", function() { | ||
var input = {param_long: 'first string', param2: 'second string'}; | ||
@@ -150,4 +150,4 @@ var output = prettyjson.render(input, {keysColor: 'rainbow'}); | ||
}); | ||
it("should allow to configure the default indentation", function(){ | ||
it("should allow to configure the default indentation", function() { | ||
var input = {param: ['first string', "second string"]}; | ||
@@ -162,4 +162,4 @@ var output = prettyjson.render(input, {defaultIndentation: 4}); | ||
}); | ||
it("should allow to configure the empty message for arrays", function(){ | ||
it("should allow to configure the empty message for arrays", function() { | ||
var input = []; | ||
@@ -174,5 +174,4 @@ var output = prettyjson.render(input, {emptyArrayMsg: '(empty)'}); | ||
describe('Printing numbers, booleans and other objects', function(){ | ||
it("should print numbers correctly ", function(){ | ||
describe('Printing numbers, booleans and other objects', function() { | ||
it("should print numbers correctly ", function() { | ||
var input = 12345; | ||
@@ -184,3 +183,3 @@ var output = prettyjson.render(input, {}, 4); | ||
it("should print booleans correctly ", function(){ | ||
it("should print booleans correctly ", function() { | ||
var input = true; | ||
@@ -197,3 +196,3 @@ var output = prettyjson.render(input, {}, 4); | ||
it("should print a null object correctly ", function(){ | ||
it("should print a null object correctly ", function() { | ||
var input = null; | ||
@@ -204,2 +203,32 @@ var output = prettyjson.render(input, {}, 4); | ||
}); | ||
}); | ||
describe('prettyjson.renderString() method', function(){ | ||
it('should return an empty string if input is empty', function(){ | ||
var input = ''; | ||
var output = prettyjson.renderString(input); | ||
output.should.equal(''); | ||
}); | ||
it('should return an empty string if input is not a string', function(){ | ||
var output = prettyjson.renderString({}); | ||
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 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 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'); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
43
915
1377910