Miss any of our Open RFC calls?Watch the recordings here! »

@davidfig/json-depth

1.0.0 • Public • Published

json-depth

Prettify JSON for output with a number of options, including inlining after a set depth

rationale

My OCD forced me to create this. I wanted to edit JSON files prettified to a certain depth and I couldn't find an appropriate npm package. I added other options as well.

installation

npm i @davidfig/json-depth

simple usage

const fs = require('fs')
const jsonDepth = require('json-depth')
 
const data = { vehicles: ['cars', 'trucks', 'motorcycles', 'bicycles'], colors: ['red', 'green', 'blue']}
 
console.log(jsonDepth(data, { depth: 1 }))
/* RESULT:
{
    "vehicles": ["cars", "trucks", "motorcycles", "bicycles"],
    "colors": ["red", "green", "blue"]
}
*/
 
fs.writeFile('test/test-output.json', jsonDepth(DATA, { depth: 2 }), () =>
{
    console.log('wrote file: test-output.json')
    process.exit(0)
})

API

// prettify JSON with more options
function jsondepth(data, options)
name type default description
data object data to be prettified
options object
options.depth number 3 the number of depths to expand with options.eol characters
options.indent string /t a string or the number of spaces to indent each depth
options.spaces number 0 number of spaces to use instead of indent character
options.eol string \n end of line character
options.spaceAfter number 1 spaces after bracket, squiggly bracket, comma, or colon
options.ignoreNull boolean false ignore null
options.ignoreUndefined boolean true ignore undefined entries
options.emptyArray string [] replace empty array with this string
options.emptyObject string {} replace empty object with this string
options.spaceInlineArray boolean false whether to add a space at the beginning and end of an inline array
options.spaceInlineObject boolean true whether to add a space at the beginning and end of an inline object

license

MIT License
(c) 2018 YOPEY YOPEY LLC by David Figatner

Install

npm i @davidfig/json-depth

DownloadsWeekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

55.4 kB

Total Files

9

Last publish

Collaborators

  • avatar