Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
json-stringify-pretty-compact
Advanced tools
Changelog
Version 4.0.0 (2022-05-14)
Changed: This is now an [ESM only package]. (I haven’t written that gist, but it’s a great resource.)
If you need CommonJS, install version 3.0.0. You won’t be missing out on anything: This package is done. No more features will be added, and no bugs have been found in years.
Readme
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 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.
npm install json-stringify-pretty-compact
import stringify from "json-stringify-pretty-compact";
Note: This is an ESM only package. (I haven’t written that gist, but it’s a great resource.)
If you need CommonJS, install version 3.0.0. You won’t be missing out on anything: This package is done. No more features will be added, and no bugs have been found in years.
stringify(obj, options = {})
It’s like JSON.stringify(obj, options.replacer, options.indent)
, except that objects and arrays are on one line if they fit (according to options.maxLength
).
options
:
stringify(obj, {maxLength: 0, indent: indent})
gives the exact same result as JSON.stringify(obj, null, indent)
. (However, if you use a replacer
, integer keys might be moved first.)
stringify(obj, {maxLength: Infinity})
gives the exact same result as JSON.stringify(obj)
, except that there are spaces after colons and commas.
Want more options? Check out @aitodotai/json-stringify-pretty-compact!
MIT.
FAQs
The best of both `JSON.stringify(obj)` and `JSON.stringify(obj, null, indent)`.
The npm package json-stringify-pretty-compact receives a total of 720,335 weekly downloads. As such, json-stringify-pretty-compact popularity was classified as popular.
We found that json-stringify-pretty-compact demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.