2.2.4 • Published 4 years ago

json-chalker v2.2.4

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

json-chalker

A library for colorizing JSON strings, based on json-colorizer.

This package is a simple console syntax highlighter for JSON.

Installation

npm install --save json-chalker

Usage

const { colorize } = require('json-chalker');
console.log(colorize({ "foo": "bar" }));

Specifying colors

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).

A color can also be specified as a hex value starting with the # symbol.

const { colorize } = require('json-chalker');
console.log(colorize({ "foo": "bar" }, {
  colors: {
    STRING_KEY: 'green',
    STRING_LITERAL: 'magenta.bold',
    NUMBER_LITERAL: '#FF0000'
  }
}));

The tokens available are:

  • BRACE
  • BRACKET
  • COLON
  • COMMA
  • STRING_KEY
  • STRING_LITERAL
  • NUMBER_LITERAL
  • BOOLEAN_LITERAL
  • NULL_LITERAL

Custom formatting

You may specify custom formatting like this:

const { colorize } = require('json-chalker');
console.log(colorize({ "foo": "I'm blue", "bar": "I'm red" }, {
  colors: {
    CUSTOM: (key, value, parent) => {
        if(key == "foo") return 'blue';
        if(value == "I'm red") return 'red';
    }
  }
}));
2.2.3

4 years ago

2.2.4

4 years ago

2.2.2

4 years ago