json-stringify-pretty-compact

WebJar for json-stringify-pretty-compact

License

License

MIT
Categories

Categories

JSON Data
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

json-stringify-pretty-compact
Last Version

Last Version

2.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

json-stringify-pretty-compact
WebJar for json-stringify-pretty-compact
Project URL

Project URL

https://www.webjars.org
Source Code Management

Source Code Management

https://github.com/lydell/json-stringify-pretty-compact

Download json-stringify-pretty-compact

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/json-stringify-pretty-compact/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>json-stringify-pretty-compact</artifactId>
    <version>2.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/json-stringify-pretty-compact/
implementation 'org.webjars.npm:json-stringify-pretty-compact:2.0.0'
// https://jarcasting.com/artifacts/org.webjars.npm/json-stringify-pretty-compact/
implementation ("org.webjars.npm:json-stringify-pretty-compact:2.0.0")
'org.webjars.npm:json-stringify-pretty-compact:jar:2.0.0'
<dependency org="org.webjars.npm" name="json-stringify-pretty-compact" rev="2.0.0">
  <artifact name="json-stringify-pretty-compact" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='json-stringify-pretty-compact', version='2.0.0')
)
libraryDependencies += "org.webjars.npm" % "json-stringify-pretty-compact" % "2.0.0"
[org.webjars.npm/json-stringify-pretty-compact "2.0.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

json-stringify-pretty-compact Build Status

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.

Installation

npm install json-stringify-pretty-compact
const stringify = require("json-stringify-pretty-compact");

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:

  • 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.
  • replacer: Defaults to undefined. Works exactly like the second parameter of JSON.stringify.

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!

Development

You need Node.js 10 and npm 6.

npm scripts

  • npm run eslint: Run ESLint (including Prettier).
  • npm run eslint:fix: Autofix ESLint errors.
  • npm run prettier: Run Prettier for files other than JS.
  • npm run jest: Run unit tests. During development, npm run jest -- --watch is nice.
  • npm run coverage: Run unit tests with code coverage.
  • npm test: Check that everything works.

License

MIT.

Versions

Version
2.0.0
1.2.0