@aitodotai/json-stringify-pretty-compact

中文文档
1.3.0发布于 2019/01/30MITCommonJS零依赖近 30 天下载 5.8万

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

原始使用文档

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.

版本与发布记录

@aitodotai/json-stringify-pretty-compact 共发布 1 个版本,首次发布于 2019/01/30;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.0latest2019/01/307 年前

关于 @aitodotai/json-stringify-pretty-compact 的常见问题

@aitodotai/json-stringify-pretty-compact 是什么?

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

如何安装 @aitodotai/json-stringify-pretty-compact?

在项目目录运行 npm install @aitodotai/json-stringify-pretty-compact,或使用 pnpm add / yarn add / bun add。

@aitodotai/json-stringify-pretty-compact 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/aitodotai__json-stringify-pretty-compact 是否存在。

@aitodotai/json-stringify-pretty-compact 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@aitodotai/json-stringify-pretty-compact 可以商用吗?

@aitodotai/json-stringify-pretty-compact 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@aitodotai/json-stringify-pretty-compact 的下载量有多少?

根据 npm 官方下载统计,@aitodotai/json-stringify-pretty-compact 在 2026/10/05 – 2026/11/03 这 30 天内被下载 57,959 次,日均约 1,932 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aitodotai/json-stringify-pretty-compact 还在维护吗?

最新版本 1.3.0 发布于 2019/01/30(7 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/01/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。