its-json
A configurable json stringify utility.
INSTALLATION
npm install its-json --save
USAGE
Usage example with default options:
const itsJson = require('its-json');
const json = {
somekey: 'with value'
};
const opts = {
quotes: '"', // quotes for string
colon: ':', // colon between key-value pair
space: ' ', // space
comma: ',', // comma between array and object elements
pretty: false, // pretty print, if number given it calculated as space count
braces: {
open: '{', // open and close braces for object
close: '}'
},
brackets: {
open: '[', // open and close brackets for array
close: ']'
}
};
const result = itsJson(json, opts);
console.log(result);
// {"somekey":"with value"}
You can use hook functions for string
, number
, null
or boolean
values.
Hook function will be called everytime those value emitted. The hook function
will be passed current value as the only one argument and will outprint
whatever value it return. This comes very handy for coloring when using
library like chalk.
Example:
const itsJson = require('its-json');
const json = {
somekey: 'with value'
};
const opts = {
keyHook: chalk.bold,
stringHook: chalk.green,
numberHook: chalk.blue,
booleanHook: chalk.red,
nullHook: chalk.yellow,
bracesHook: chalk.black.bold,
bracketsHook: chalk.black.bold,
pretty: true
};
const result = itsJson(json, opts);
console.log(result);
// you will get this ...
//
// {
// "somekey": "with value"
// }
//
// colored. Thanks to chalk.
LICENSE
MIT