npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

perfect-json

v0.3.1

Published

Utility function to beautify JSON string...like JSON.stringify() but better

Downloads

165

Readme

node-perfect-json

NPM version CircleCI codecov Downloads/month License

Utility function to beautify JSON string...like JSON.stringify() but better

Installation

npm install perfect-json

or

yarn add perfect-json

Usage

perfectJson(obj, options)
  • obj — JSON to beautify;
  • options — optional parameters:
    • indent — count of indentation spaces (defaults to 2);
    • compact — tells whether close and open brackets of object array items must be placed on the same line (defaults to true);
    • singleLine — tells whether values of object properties must be placed on a single line, it can be of boolean type or a function returning a boolean result and being invoked for each property of an object recursively — the function receives an object argument with the following properties:
      • key — name of the current property (zero-based index in case of array);
      • value — value of the current property;
      • path — array consisting of names of all ascendant properties including the current one;
      • items — array of references to all ascendant objects and arrays;
      • line — stringified array or object value placed on a single line;
      • depth — zero-based depth level (equals to path.length and items.length);
      • indent — count of indentation spaces per level ((depth + 1) * indent results in a summary indentation on a given level).
    • maxLineLength — places objects and arrays on a single line if resulting line's length is less than or equal to specified value;
    • arrayMargin — characters after opening and before closing array brackets when array is placed on a single line (defaults to empty string meaning no gap: ["Javascript", "Node.js", "ES6"]);
    • objectMargin — characters after opening and before closing object brackets when object is placed on a single line (defaults to ' ' meaning a gap: { "node": "14.0.0", "eslint": true, "babel": true, "typescript": false });
    • split — function to split the resulting JSON into several nested JSONs, it accepts the same properties as singleLine function excepting line and should return a string placeholder to replace the current property by; JSON parts replaced by placeholders are stored in a separate object (where keys are placeholders) that can be accessed in splitResult function;
    • splitResult — function being called at the end of transformation with an object of splitted JSONs as an argument.

Basic example

Just pass an object to stringify:

const perfectJson = require('perfect-json');
console.log(perfectJson({
  name: 'Dmitriy',
  surname: 'Pushkov',
  skills: ['JavaScript', 'Node.js', 'ES6'],
  env: { node: '14.0.0', eslint: true, babel: true, typescript: false }
}));

Result:

{
  "name": "Dmitriy",
  "surname": "Pushkov",
  "skills": [
    "JavaScript",
    "Node.js",
    "ES6"
  ],
  "env": {
    "node": "14.0.0",
    "eslint": true,
    "babel": true,
    "typescript": false
  }
}

Incompact object array items

Use compact option:

const perfectJson = require('perfect-json');
console.log(perfectJson([{
  name: 'Dmitriy',
  surname: 'Pushkov'
}, {
  name: 'Tamara',
  surname: 'Pushkova'
}], {
  compact: false
}));

Result:

[
  {
    "name": "Dmitriy",
    "surname": "Pushkov"
  },
  {
    "name": "Tamara",
    "surname": "Pushkova"
  }
]

Set indentation size

Use indent option:

const perfectJson = require('perfect-json');
console.log(perfectJson({
  name: 'Dmitriy',
  surname: 'Pushkov',
  skills: ['JavaScript', 'Node.js', 'ES6'],
  env: { node: '14.0.0', eslint: true, babel: true, typescript: false }
}, {
  indent: 4
}));

Result:

{
    "name": "Dmitriy",
    "surname": "Pushkov",
    "skills": [
        "JavaScript",
        "Node.js",
        "ES6"
    ],
    "env": {
        "node": "14.0.0",
        "eslint": true,
        "babel": true,
        "typescript": false
    }
}

Place specific props on a single line

Use singleLine option:

const perfectJson = require('perfect-json');
console.log(perfectJson({
  name: 'Dmitriy',
  surname: 'Pushkov',
  skills: ['JavaScript', 'Node.js', 'ES6'],
  env: { node: '14.0.0', eslint: true, babel: true, typescript: false }
}, {
  singleLine: ({ key }) => {
    return ['skills', 'env'].includes(key);
  }
}));

Result:

{
  "name": "Dmitriy",
  "surname": "Pushkov",
  "skills": ["JavaScript", "Node.js", "ES6"],
  "env": { "node": "14.0.0", "eslint": true, "babel": true, "typescript": false }
}

Limit single line length

Use maxLineLength option:

const perfectJson = require('perfect-json');
const obj = {
  name: 'Dmitriy',
  surname: 'Pushkov',
  skills: ['JavaScript', 'Node.js', 'ES6'],
  env: { node: "14.0.0", eslint: true, babel: true, typescript: false }
};
console.log(perfectJson(obj, {
  maxLineLength: 40
}));
console.log(perfectJson(obj, {
  maxLineLength: 80
}));

Result:

{
  "name": "Dmitriy",
  "surname": "Pushkov",
  "skills": ["JavaScript", "Node.js", "ES6"],
  "env": {
    "node": "14.0.0",
    "eslint": true,
    "babel": true,
    "typescript": false
  }
}
{
  "name": "Dmitriy",
  "surname": "Pushkov",
  "skills": ["JavaScript", "Node.js", "ES6"],
  "env": { "node": "14.0.0", "eslint": true, "babel": true, "typescript": false }
}

Splitting

Use split and splitResult options together:

const perfectJson = require('perfect-json');
console.log(perfectJson({
  name: 'Dmitriy',
  surname: 'Pushkov',
  skills: ['JavaScript', 'Node.js', 'ES6'],
  env: { node: '14.0.0', eslint: true, babel: true, typescript: false }
}, {
  split: ({ key, depth }) => {
    if (depth !== 1) {
      return null;
    }
    switch (key) {
      case 'skills': return '#skills';
      case 'env': return '#env';
      default: return null;
    }
  },
  splitResult: splitted => {
    console.log(splitted['#skills']);
    console.log(splitted['#env']);
  }
}));

Result:

[
  "JavaScript",
  "Node.js",
  "ES6"
]
{
  "node": "14.0.0",
  "eslint": true,
  "babel": true,
  "typescript": false
}
{
  "name": "Dmitriy",
  "surname": "Pushkov",
  "skills": "#skills",
  "env": "#env"
}