prettyjson
Advanced tools
Comparing version 0.12.1 to 0.13.0
@@ -35,2 +35,3 @@ 'use strict'; | ||
options.defaultIndentation = options.defaultIndentation || 2; | ||
options.noColor = !!options.noColor; | ||
@@ -51,2 +52,6 @@ options.stringColor = options.stringColor || null; | ||
var addColorToData = function(input) { | ||
if (options.noColor) { | ||
return input; | ||
} | ||
if (typeof input === 'string') { | ||
@@ -57,15 +62,17 @@ // Print strings in regular terminal color | ||
var sInput = input + ''; | ||
if (input === true) { | ||
return (input+'').green; | ||
return sInput.green; | ||
} | ||
if (input === false) { | ||
return (input+'').red; | ||
return sInput.red; | ||
} | ||
if (input === null) { | ||
return (input+'').grey; | ||
return sInput.grey; | ||
} | ||
if (typeof input === 'number') { | ||
return (input+'')[options.numberColor]; | ||
return sInput[options.numberColor]; | ||
} | ||
return (input+''); | ||
return sInput; | ||
}; | ||
@@ -84,3 +91,7 @@ | ||
// Prepend the dash at the begining of each array's element line | ||
var line = Utils.indent(indentation) + ('- ')[options.dashColor]; | ||
var line = ('- '); | ||
if (!options.noColor) { | ||
line = line[options.dashColor]; | ||
} | ||
var line = Utils.indent(indentation) + line; | ||
@@ -111,3 +122,7 @@ // If the element of the array is a string, bool, number, or null | ||
// Prepend the index at the beginning of the line | ||
key = Utils.indent(indentation) + (i + ': ')[options.keysColor]; | ||
key = (i + ': '); | ||
if (!options.noColor) { | ||
key = key[options.keysColor]; | ||
} | ||
key = Utils.indent(indentation) + key; | ||
@@ -114,0 +129,0 @@ // Skip `undefined`, it's not a valid JSON value. |
@@ -5,3 +5,3 @@ { | ||
"description": "Package for formatting JSON data in a coloured YAML-style, perfect for CLI output", | ||
"version": "0.12.1", | ||
"version": "0.13.0", | ||
"homepage": "http://rafeca.com/prettyjson", | ||
@@ -34,3 +34,4 @@ "keywords": [ | ||
"dependencies": { | ||
"colors": "0.6.2" | ||
"colors": "0.6.2", | ||
"minimist": "0.0.8" | ||
}, | ||
@@ -37,0 +38,0 @@ "devDependencies": { |
@@ -15,6 +15,7 @@ # prettyjson [![Build Status](https://secure.travis-ci.org/rafeca/prettyjson.png)](http://travis-ci.org/rafeca/prettyjson) [![NPM version](https://badge.fury.io/js/prettyjson.png)](http://badge.fury.io/js/prettyjson) [![Coverage Status](https://coveralls.io/repos/rafeca/prettyjson/badge.png?branch=master)](https://coveralls.io/r/rafeca/prettyjson?branch=master) | ||
This package installs a command line interface to render JSON data in a more convenient way. You can use the CLI | ||
in three different ways: | ||
This package installs a command line interface to render JSON data in a more | ||
convenient way. You can use the CLI in three different ways: | ||
**Decode a JSON file:** If you want to see the contents of a JSON file, just pass it as the first argument to the CLI: | ||
**Decode a JSON file:** If you want to see the contents of a JSON file, just pass | ||
it as the first argument to the CLI: | ||
@@ -27,4 +28,4 @@ ```bash | ||
**Decode the stdin:** You can also pipe the result of a command (for example an HTTP request) to the CLI to see | ||
the JSON result in a clearer way: | ||
**Decode the stdin:** You can also pipe the result of a command (for example an | ||
HTTP request) to the CLI to see the JSON result in a clearer way: | ||
@@ -37,23 +38,32 @@ ```bash | ||
**Decode random strings:** if you call the CLI with no arguments, you'll get a prompt where you can past JSON strings | ||
and they'll be automatically displayed in a clearer way: | ||
**Decode random strings:** if you call the CLI with no arguments, you'll get a | ||
prompt where you can past JSON strings and they'll be automatically displayed in a clearer way: | ||
![Example 3](https://raw.github.com/rafeca/prettyjson/master/images/example5.png) | ||
If you install the package globally (with `npm install -g prettyjson`), the CLI will be installed automatically in your PATH | ||
thanks to npm. | ||
If you install the package globally (with `npm install -g prettyjson`), the CLI | ||
will be installed automatically in your PATH thanks to npm. | ||
### Customizing colors via command line | ||
### Command line options | ||
Now it's possible to customize the colors of the output via environment variables, thanks to @bahamas10: | ||
It's possible to customize the output through some command line options: | ||
```bash | ||
$ PRETTYJSON_KEYS=red PRETTYJSON_DASH=blue PRETTYJSON_STRING=yellow prettyjson package.json | ||
# Change colors | ||
$ prettyjson --string=red --keys=blue --dash=yellow --number=green package.json | ||
# Do not use colors | ||
$ prettyjson --nocolor=1 package.json | ||
# Change indentation | ||
$ prettyjson --indent=4 package.json | ||
``` | ||
The available options are `PRETTYJSON_KEYS`, `PRETTYJSON_DASH`, `PRETTYJSON_STRING` and `PRETTYJSON_INDENT`. | ||
**Deprecation Notice**: The old configuration through environment variables is | ||
deprecated and it will be removed in the next major version (1.0.0). | ||
## Using it (from Node.js) | ||
It's pretty easy to use it... you just have to include it in your script and call the `render()` method: | ||
It's pretty easy to use it. You just have to include it in your script and call | ||
the `render()` method: | ||
@@ -70,3 +80,7 @@ ```javascript | ||
console.log(prettyjson.render(data)); | ||
var options = { | ||
noColors: true | ||
}; | ||
console.log(prettyjson.render(data, options)); | ||
``` | ||
@@ -104,3 +118,4 @@ | ||
To run the test suite first invoke the following command within the repo, installing the development dependencies: | ||
To run the test suite first invoke the following command within the repo, | ||
installing the development dependencies: | ||
@@ -107,0 +122,0 @@ ```bash |
@@ -178,2 +178,13 @@ var prettyjson = process.env.EXPRESS_COV ? require('../lib-cov/prettyjson') : require('../lib/prettyjson'); | ||
it("should allow to not use colors", function() { | ||
var input = {param1: 'first string', param2: ['second string']}; | ||
var output = prettyjson.render(input, {noColor: true}); | ||
output.should.equal([ | ||
'param1: first string', | ||
'param2: ', | ||
' - second string' | ||
].join('\n')); | ||
}); | ||
it("should not print an object prototype", function() { | ||
@@ -180,0 +191,0 @@ var Input = function() { |
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
620867
428
132
2
+ Addedminimist@0.0.8
+ Addedminimist@0.0.8(transitive)