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

@aitodotai/json-stringify-pretty-compact

v1.3.0

Published

The best of both `JSON.stringify(obj)` and `JSON.stringify(obj, null, indent)`.

Downloads

19,507

Readme

Fork information: This is a maintained fork which implements these additional options: maxNesting, arrayMargins, and objectMargins.

Overview Build Status JavaScript Style Guide

The output of JSON.stringify comes in two flavors: compact and pretty. The former is usually too compact to be read by humans, while the latter sometimes is too spacious. This module trades performance (and the “replacer” argument) for a compromise between the two. The result is a pretty compact string, where “pretty” means both “kind of” and “nice”.

{
  "bool": true,
  "short array": [1, 2, 3],
  "long array": [
    {"x": 1, "y": 2},
    {"x": 2, "y": 1},
    {"x": 1, "y": 1},
    {"x": 2, "y": 2}
  ]
}

While the “pretty” mode of JSON.stringify puts every item of arrays and objects on its own line, this module puts the whole array or object on a single line, unless the line becomes too long (the default maximum is 80 characters). Making arrays and objects multi-line is the only attempt made to enforce the maximum line length; if that doesn’t help then so be it.

Installation

npm install @aitodotai/json-stringify-pretty-compact

var stringify = require("json-stringify-pretty-compact")

Usage

stringify(obj, [options])

It’s like JSON.stringify(obj, null, options.indent), except that objects and arrays are on one line if they fit (according to options.maxLength).

options:

  • indent: Defaults to 2. Works exactly like the third parameter of JSON.stringify.

  • maxLength: Defaults to 80. Lines will be tried to be kept at maximum this many characters long.

  • maxNesting: Defaults to Infinity. The maximum amount of allowed inline nesting for objects or arrays

    By default, the output can contain arbitrary amount of nested structures (as long as the maxLength is not exceeded), like below:

    {"a": [{"b": ["test1", "test2"]}, {"c": true}]}}

    When maxNesting is set to 0, the output doesn't allow any nesting:

    {
      "a": [
        {
          "b": [
            "test1",
            "test2"
          ]
        },
        {
          "c": true
        }
      ]
    }
  • margins: Defaults to false. Whether or not to add “margins” around brackets and braces:

    • false: {"a": [1]}
    • true: { "a": [ 1 ] }
  • arrayMargins: Defaults to false. Whether or not to add “margins” around brackets:

    • false: {"a": [1]}
    • true: {"a": [ 1 ]}
  • arrayMargins: Defaults to true. Whether or not to add “margins” around braces:

    • false: {"a": [1]}
    • true: { "a": [1] }

stringify(obj, {maxLength: 0, indent: indent}) gives the exact same result as JSON.stringify(obj, null, indent).

stringify(obj, {maxLength: Infinity}) gives the exact same result as JSON.stringify(obj), except that there are spaces after colons and commas.

Releasing

Example of publishing new release with patch change:

npm run publish -- patch

License

MIT.