json-colorizer
Advanced tools
Comparing version 1.0.2 to 1.1.0
{ | ||
"name": "json-colorizer", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "A library to format JSON with colors for display in the console", | ||
@@ -5,0 +5,0 @@ "main": "src/lib/index.js", |
@@ -8,8 +8,45 @@ # json-colorizer | ||
# Installation | ||
## Installation | ||
`npm install --save json-colorizer` | ||
# Usage | ||
var colorize = require('json-colorizer'); | ||
console.log(colorize( { "foo": "bar" }); | ||
## Usage | ||
```js | ||
var colorize = require('json-colorizer'); | ||
console.log(colorize({ "foo": "bar" }); | ||
``` | ||
If you pass a string to the colorize function, it will treat it as pre-serialized JSON. This can be used in order to colorize pretty-printed JSON: | ||
```js | ||
var colorize = require('json-colorizer'); | ||
var json = JSON.stringify({"foo": "bar"}, null, 2) | ||
console.log(colorize(json); | ||
``` | ||
## Specifying colors | ||
You can specify a function to use for coloring individual tokens by providing a `colors` object: | ||
```js | ||
var colorize = require('json-colorizer'); | ||
var chalk = require('chalk') | ||
console.log(colorize({ "foo": "bar" }, { | ||
colors: { | ||
STRING_KEY: chalk.green | ||
} | ||
}); | ||
``` | ||
The tokens available are: | ||
* `BRACE` | ||
* `BRACKET` | ||
* `COLON` | ||
* `COMMA` | ||
* `STRING_KEY` | ||
* `STRING_LITERAL` | ||
* `NUMBER_LITERAL` | ||
* `BOOLEAN_LITERAL` | ||
* `NULL_LITERAL` |
var chalk = require('chalk'); | ||
var colors = { | ||
var defaultColors = { | ||
BRACE: chalk.gray, | ||
@@ -11,6 +11,9 @@ BRACKET: chalk.gray, | ||
NUMBER_LITERAL: chalk.green, | ||
BOOLEAN_LITERAL: chalk.cyan | ||
BOOLEAN_LITERAL: chalk.cyan, | ||
NULL_LITERAL: chalk.white | ||
}; | ||
exports.colorize = function colorize(tokens) { | ||
exports.colorize = function colorize(tokens, options) { | ||
var opts = options || {}; | ||
var colors = opts.colors || {}; | ||
var str = ''; | ||
@@ -20,3 +23,3 @@ var colorFn; | ||
tokens.forEach(function (token) { | ||
colorFn = colors[token.type]; | ||
colorFn = colors[token.type] || defaultColors[token.type]; | ||
str += colorFn ? colorFn(token.value) : token.value; | ||
@@ -23,0 +26,0 @@ }); |
const lexer = require('./lexer'); | ||
const colorizer = require('./colorizer'); | ||
module.exports = function colorizeJson(json) { | ||
return colorizer.colorize(lexer.getTokens(json)); | ||
module.exports = function colorizeJson(json, options) { | ||
return colorizer.colorize(lexer.getTokens(json), options); | ||
}; |
@@ -11,3 +11,3 @@ const tokenTypes = [ | ||
{ regex: /^true|false/, tokenType: 'BOOLEAN_LITERAL' }, | ||
{ regex: /^null/, tokenType: 'NULL' } | ||
{ regex: /^null/, tokenType: 'NULL_LITERAL' } | ||
]; | ||
@@ -14,0 +14,0 @@ |
@@ -83,3 +83,3 @@ var chai = require('chai'); | ||
var result = lexer.getTokens('null'); | ||
expect(result).to.deep.equal([{ type: 'NULL', value: 'null' }]); | ||
expect(result).to.deep.equal([{ type: 'NULL_LITERAL', value: 'null' }]); | ||
}); | ||
@@ -86,0 +86,0 @@ |
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
29592
12
207
52