1.2.0 • Published 9 years ago

its-json v1.2.0

Weekly downloads
155
License
MIT
Repository
github
Last release
9 years ago

its-json

NPM Version Build Status Test Coverage

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

1.2.0

9 years ago

1.1.0

9 years ago

1.0.0

9 years ago