Skip to content

gera2ld/format-json

Repository files navigation

@gera2ld/format-json

NPM License Downloads

Format JSON in different flavors.

Features

  • Format as JSON or JavaScript, either compact or indented
  • Highlight nodes by changing the output on render

Installation

$ yarn add @gera2ld/format-json

Usage

import { format, ItemTypes } from '@gera2ld/format-json';

const data = {/* … */};
const jsonOptions = {
  indent: 0,
  quoteAsNeeded: false,
  quote: '"',
  trailing: false,
  template: false,
};
console.log('format as JSON:', format(data, jsonOptions));

const jsOptions = {
  indent: 2,
  quoteAsNeeded: true,
  quote: '\'',
  trailing: true,
  template: true,
};
console.log('format as JavaScript', format(data, jsOptions));

const highlightOptions = {
  onData(item) {
    // if the property name is 'highlight', wrap it with `<em>...</em>`
    if (item.path[item.path.length - 1] === 'highlight' && item.type === ItemTypes.KEY) {
      item.data = [
        { value: '<em>' },
        ...item.data,
        { value: '</em>' },
      ];
    }
  },
};
console.log('highlight values', format(data, highlightOptions));