1.0.0 • Published 6 years ago

@davidfig/json-depth v1.0.0

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

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)
nametypedefaultdescription
dataobjectdata to be prettified
optionsobject
options.depthnumber3the number of depths to expand with options.eol characters
options.indentstring/ta string or the number of spaces to indent each depth
options.spacesnumber0number of spaces to use instead of indent character
options.eolstring\nend of line character
options.spaceAfternumber1spaces after bracket, squiggly bracket, comma, or colon
options.ignoreNullbooleanfalseignore null
options.ignoreUndefinedbooleantrueignore undefined entries
options.emptyArraystring[]replace empty array with this string
options.emptyObjectstring{}replace empty object with this string
options.spaceInlineArraybooleanfalsewhether to add a space at the beginning and end of an inline array
options.spaceInlineObjectbooleantruewhether to add a space at the beginning and end of an inline object

license

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