Stringify any JavaScript value with customizable formatting, plugins, and terminal colors.
npx @tessl/cli install tessl/npm-pretty-format@30.0.0Pretty Format is a highly configurable JavaScript value stringification library that converts any JavaScript value into a human-readable string format. It handles all built-in JavaScript types including primitives, complex objects, and special values with extensive customization through options for indentation, depth control, escaping, and color highlighting for terminal output.
npm install pretty-formatimport { format } from "pretty-format";
// OR as default export
import format from "pretty-format";For CommonJS:
const { format } = require("pretty-format");
// OR as default export
const format = require("pretty-format");Import with plugins and options:
import { format, plugins, DEFAULT_OPTIONS } from "pretty-format";
const { ReactElement, DOMElement, Immutable } = plugins;import { format } from "pretty-format";
// Basic formatting
const obj = {
name: "John",
age: 30,
hobbies: ["reading", "coding"],
address: { city: "New York", zip: 10001 }
};
console.log(format(obj));
/*
Object {
"address": Object {
"city": "New York",
"zip": 10001,
},
"age": 30,
"hobbies": Array [
"reading",
"coding",
],
"name": "John",
}
*/
// With options
console.log(format(obj, {
indent: 4,
min: false,
maxDepth: 2
}));Pretty Format is built around several key components:
format function that dispatches to appropriate handlers based on value typeMain formatting functionality that converts any JavaScript value to a human-readable string with extensive customization options.
function format(val: unknown, options?: OptionsReceived): string;
export default format;
interface OptionsReceived {
callToJSON?: boolean;
compareKeys?: ((a: string, b: string) => number) | null;
escapeRegex?: boolean;
escapeString?: boolean;
highlight?: boolean;
indent?: number;
maxDepth?: number;
maxWidth?: number;
min?: boolean;
plugins?: Plugin[];
printBasicPrototype?: boolean;
printFunctionName?: boolean;
theme?: Partial<Theme>;
}Extensible plugin architecture for handling custom data types with both modern and legacy interfaces.
interface NewPlugin {
test: (val: any) => boolean;
serialize: (
val: any,
config: Config,
indentation: string,
depth: number,
refs: Refs,
printer: Printer
) => string;
}
interface OldPlugin {
test: (val: any) => boolean;
print: (
val: unknown,
print: (val: unknown) => string,
indent: (str: string) => string,
options: { edgeSpacing: string; min: boolean; spacing: string },
colors: Colors
) => string;
}
type Plugin = NewPlugin | OldPlugin;Comprehensive collection of plugins for common JavaScript frameworks and data structures.
const plugins: {
AsymmetricMatcher: Plugin;
DOMCollection: Plugin;
DOMElement: Plugin;
Immutable: Plugin;
ReactElement: Plugin;
ReactTestComponent: Plugin;
};const DEFAULT_OPTIONS: {
callToJSON: true;
compareKeys: undefined;
escapeRegex: false;
escapeString: true;
highlight: false;
indent: 2;
maxDepth: number; // Number.POSITIVE_INFINITY
maxWidth: number; // Number.POSITIVE_INFINITY
min: false;
plugins: [];
printBasicPrototype: true;
printFunctionName: true;
theme: {
comment: 'gray';
content: 'reset';
prop: 'yellow';
tag: 'cyan';
value: 'green';
};
};class PrettyFormatPluginError extends Error {
constructor(message: string, stack: string);
}type CompareKeys = ((a: string, b: string) => number) | null | undefined;
interface Theme {
comment: string;
content: string;
prop: string;
tag: string;
value: string;
}
interface Colors {
comment: { close: string; open: string };
content: { close: string; open: string };
prop: { close: string; open: string };
tag: { close: string; open: string };
value: { close: string; open: string };
}
type Refs = Array<unknown>;
type Printer = (
val: unknown,
config: Config,
indentation: string,
depth: number,
refs: Refs,
hasCalledToJSON?: boolean
) => string;
interface Config {
callToJSON: boolean;
compareKeys: CompareKeys;
colors: Colors;
escapeRegex: boolean;
escapeString: boolean;
indent: string;
maxDepth: number;
maxWidth: number;
min: boolean;
plugins: Plugin[];
printBasicPrototype: boolean;
printFunctionName: boolean;
spacingInner: string;
spacingOuter: string;
}
type Plugin = NewPlugin | OldPlugin;
type Plugins = Array<Plugin>;
interface Options extends Omit<PrettyFormatOptions, 'compareKeys' | 'theme'> {
compareKeys: CompareKeys;
theme: Required<PrettyFormatOptions['theme']>;
}
interface PrettyFormatOptions {
callToJSON?: boolean;
compareKeys?: CompareKeys;
escapeRegex?: boolean;
escapeString?: boolean;
highlight?: boolean;
indent?: number;
maxDepth?: number;
maxWidth?: number;
min?: boolean;
plugins?: Plugins;
printBasicPrototype?: boolean;
printFunctionName?: boolean;
theme?: Partial<Theme>;
}
type OptionsReceived = PrettyFormatOptions;