4.0.0 • Published 3 years ago

json-stringify-pretty-compact v4.0.0

Weekly downloads
212,415
License
MIT
Repository
github
Last release
3 years ago

json-stringify-pretty-compact

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
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:

  • 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!

License

MIT.

gemini-grammarblockysite-cliidheditblockysitecogoportutils@infinitebrahmanuniverse/nolb-json-srenovate@everything-registry/sub-chunk-1985webfile-packertap-nirvanatap-nirvana-extendedteach-cliwilly-clivue-matrix-vizspotify-exporttest-ui-storybookthepian-elementsvsm-json-prettyvega-embedvega-embed-lassovega-embed-mattijnvega-litevega-lite-lassovue-cli-plugin-ice-buildervue-cli-plugin-ut-builderjhalljstorejsjson-spcjson-stringify-pretty-compact-clijson-pretty-compact-clikeyrunnerkeyrunner-cli-testjwtfedjsx-to-stringjsx-to-string-2iclone-clii4-js-commonsinitai-cliinitai-dev-serverhtmlpresshls-docjson-data-extenderiohelperinvokeuimapbox-gl-style-spec-module-exportmd-docloggy-logldebugkomodo-sdkmetalogger@websulik/cypress-autorecord-sulik@websulik/cypress-autorecord@xbenjii/renovate@twinski/kickstart-cli@tommy2gis/maplibre-gl-style-spec@trackasia/trackasia-gl-style-spec@vidavidorra/bunyan-pretty-stream@vckit/app@utrad-ical/circus-api-util@zsylvia/renovateairtable-exporta_codestyleapiary-preprocessoras-jsx-string@gorules/jdm-editor@cismet/csconf@ckatzorke/renovate@hackello/fs@acalcutt/maplibre-gl-style-spec@adam.biltcliffe/board-state-playground@eidetiq/vega-embed@constructor-io/constructorio-connect-cli@americana/maplibre-gl-style-specspectaqlspectaql-custom-json-responsesubtree-helpertslintxtimeld-cliui-sbtrackasia-style-specvue-openapi@captum/captum-liquid@chlorophytum/hint-store-provider-vttyumemixxxx-clizigniszqlts-cell@lintest/cli@newbiz/admin-script-libs@nerdworks/vega-embed@moredhel/renovate@mornya/cli-libs@mornya/vessel@mornya/lintest@parisholley/vega-lite@retorquere/zotero-sync@semo/coremultigrainmqtt-meta
4.0.0

3 years ago

3.0.0

5 years ago

2.0.0

7 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.4

8 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

11 years ago

1.0.0

11 years ago