Pretty print object
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.
Development
Use Node.js 24 (nvm use), then run:
npm run check:dependencies
npm ci
npm run check
check runs ESLint, Prettier, TypeScript checking and all Jest tests. CI runs
these checks on every pull request and on pushes to master, using Node.js
22, 24 and 26. Tests parse the built JavaScript as ES5 and exercise it with
ES2015 built-ins removed from an isolated runtime.
SWC produces the CommonJS ES5 build. TypeScript emits declarations separately; its target setting does not control the published JavaScript. TypeScript stays on the latest version supported by typescript-eslint. The published package has no runtime dependencies. Symbol properties are included where supported and skipped in runtimes without symbols.
Run npm run build to rebuild dist, or npm pack --dry-run to inspect the
package contents. Packing also rebuilds the package automatically.
Dependency security
npm run check:dependencies checks publication dates for every locked dependency
(including optional packages) and runs the full npm audit. CI runs this before
installing dependencies and fails on any reported vulnerability or package less
than seven days old. Unknown publication dates also fail the check.
The project .npmrc sets min-release-age=7 for npm versions that support it
(including npm 11.16). The independent lockfile check also covers older npm
versions and updates that preserve already-locked versions.
A scoped override uses js-yaml 4.3.2 for @istanbuljs/load-nyc-config, removing
its old argparse / sprintf-js chain and advisory
GHSA-hp3w-g68c-fv3c.
A regression test checks YAML configuration inheritance through the real loader.
Remove the override when the upstream loader updates its dependency.