@base2/pretty-print-object

中文文档
1.0.4发布于 2026/10/18BSD-2-ClauseTypeScriptCommonJS零依赖近 30 天下载 771.1万

Convert an object or array into a formatted string

原始使用文档
文档目录4 节
  1. Install
  2. Usage
  3. API
  4. prettyPrint(input, [options])

Pretty print object

License CI

Convert an object or array into a formatted string

This is a re-write of stringify-object in Typescript, modified to inline the dependencies and make it compatible with ES5 out of the box.

Useful for when you want to get the string representation of an object in a formatted way.

It also handles circular references and lets you specify quote type.

Install

$ npm install @base2/pretty-print-object

Usage

import { prettyPrint } from '@base2/pretty-print-object';

const obj = {
    foo: 'bar',
    arr: [1, 2, 3],
    nested: {
        hello: 'world'
    }
};

const pretty = prettyPrint(obj, {
    indent: '  ',
    singleQuotes: false
});

console.log(pretty);
/*
{
  foo: "bar",
  arr: [
    1,
    2,
    3
  ],
  nested: {
    hello: "world"
  }
}
*/

API

prettyPrint(input, [options])

Invalid dates are printed as Invalid Date instead of throwing an exception. Valid dates retain the new Date('...') representation.

Circular references will be replaced with "[Circular]".

Object keys are only quoted when necessary, for example, {'foo-bar': true}.

input

Type: Object Array

options

Type: Object

indent

Type: string
Default: \t

Preferred indentation.

singleQuotes

Type: boolean
Default: true

Set to false to get double-quoted strings.

filter(obj, prop)

Type: Function

Expected to return a boolean of whether to include the property prop of the object obj in the output.

transform(obj, prop, originalResult)

Type: Function
Default: undefined

Expected to return a string that transforms the string that resulted from stringifying obj[prop]. This can be used to detect special types of objects that need to be stringified in a particular way. The transform function might return an alternate string in this case, otherwise returning the originalResult.

Here's an example that uses the transform option to mask fields named "password":

import { prettyPrint } from '@base2/pretty-print-object';

const obj = {
    user: 'becky',
    password: 'secret'
};

const pretty = prettyPrint(obj, {
    transform: (obj, prop, originalResult) => {
        if (prop === 'password') {
            return originalResult.replace(/\w/g, '*');
        }

        return originalResult;
    }
});

console.log(pretty);
/*
{
    user: 'becky',
    password: '******'
}
*/
inlineCharacterLimit

Type: number

When set, will inline values up to inlineCharacterLimit length for the sake of more terse output.

For example, given the example at the top of the README:

import { prettyPrint } from '@base2/pretty-print-object';

const obj = {
    foo: 'bar',
    arr: [1, 2, 3],
    nested: {
        hello: 'world'
    }
};

const pretty = prettyPrint(obj, {
    indent: '  ',
    singleQuotes: false,
    inlineCharacterLimit: 12
});

console.log(pretty);
/*
{
  foo: "bar",
  arr: [1, 2, 3],
  nested: {
    hello: "world"
  }
}
*/

As you can see, arr was printed as a one-liner because its string was shorter than 12 characters.

版本与发布记录

@base2/pretty-print-object 共发布 11 个版本,首次发布于 2019/09/02;过去 12 个月发布了 2 个版本。

版本发布日期距今
1.0.4latest2026/10/1820 天前
1.0.32026/10/071 个月前
1.0.22022/04/284 年前
1.0.12021/03/315 年前
1.0.02019/09/027 年前
0.0.62019/09/027 年前

关于 @base2/pretty-print-object 的常见问题

@base2/pretty-print-object 是什么?

Convert an object or array into a formatted string

如何安装 @base2/pretty-print-object?

在项目目录运行 npm install @base2/pretty-print-object,或使用 pnpm add / yarn add / bun add。

@base2/pretty-print-object 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@base2/pretty-print-object 支持 ESM 还是 CommonJS?

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

@base2/pretty-print-object 可以商用吗?

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

@base2/pretty-print-object 的下载量有多少?

根据 npm 官方下载统计,@base2/pretty-print-object 在 2026/10/08 – 2026/11/06 这 30 天内被下载 7,710,569 次,日均约 257,019 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@base2/pretty-print-object 还在维护吗?

最新版本 1.0.4 发布于 2026/10/18(20 天前);至今共发布 11 个版本,其中过去 12 个月发布了 2 个;首次发布于 2019/09/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。