json-colorizer
Advanced tools
Comparing version 2.1.3 to 2.2.0
{ | ||
"name": "json-colorizer", | ||
"version": "2.1.3", | ||
"version": "2.2.0", | ||
"description": "A library to format JSON with colors for display in the console", | ||
@@ -21,3 +21,3 @@ "main": "src/lib/index.js", | ||
], | ||
"author": "Joe Attardi <joe@attardi.net> (http://www.thinksincode.com)", | ||
"author": "Joe Attardi <jattardi@gmail.com> (http://joeattardi.codes)", | ||
"license": "MIT", | ||
@@ -24,0 +24,0 @@ "bugs": { |
@@ -26,3 +26,12 @@ # json-colorizer | ||
## Pretty-printing output | ||
To pretty-print the resulting JSON, pass the `pretty: true` option to the options object: | ||
```js | ||
const colorize = require('json-colorizer'); | ||
const json = '{"foo": "bar"}'; | ||
console.log(colorize(json, { pretty: true })); | ||
``` | ||
## Specifying colors | ||
@@ -32,3 +41,3 @@ | ||
You can specify a color to use for coloring individual tokens by providing a `colors` object. This should map token types to the names of color functions (see the [chalk styles reference](https://www.npmjs.com/package/chalk#styles)). | ||
You can specify a color to use for coloring individual tokens by providing a `colors` object in the options object. This should map token types to the names of color functions (see the [chalk styles reference](https://www.npmjs.com/package/chalk#styles)). | ||
@@ -35,0 +44,0 @@ A color can also be specified as a hex value starting with the `#` symbol. |
@@ -16,5 +16,4 @@ const chalk = require('chalk'); | ||
exports.colorize = function colorize(tokens, options) { | ||
const opts = options || {}; | ||
const colors = opts.colors || {}; | ||
exports.colorize = function colorize(tokens, options = {}) { | ||
const colors = options.colors || {}; | ||
@@ -21,0 +20,0 @@ return tokens.reduce((acc, token) => { |
@@ -5,3 +5,3 @@ const lexer = require('./lexer'); | ||
module.exports = function colorizeJson(json, options) { | ||
return colorizer.colorize(lexer.getTokens(json), options); | ||
return colorizer.colorize(lexer.getTokens(json, options), options); | ||
}; |
@@ -14,5 +14,12 @@ const tokenTypes = [ | ||
exports.getTokens = function getTokens(json) { | ||
let input = typeof json === 'string' ? json : JSON.stringify(json); | ||
exports.getTokens = function getTokens(json, options = {}) { | ||
let input; | ||
if (options.pretty) { | ||
const inputObj = typeof json === 'string' ? JSON.parse(json) : json; | ||
input = JSON.stringify(inputObj, null, 2); | ||
} else { | ||
input = typeof json === 'string' ? json : JSON.stringify(json); | ||
} | ||
let tokens = []; | ||
@@ -19,0 +26,0 @@ let foundToken; |
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
32382
357
66