Stringify any JavaScript value with customizable formatting, plugins, and terminal colors.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Core formatting functionality that converts any JavaScript value to a human-readable string with extensive customization options for indentation, depth control, escaping, and terminal colors.
Main entry point for converting any JavaScript value to a formatted string.
/**
* Returns a presentation string of your `val` object
* @param val - Any potential JavaScript object
* @param options - Custom settings to control formatting behavior
* @returns Formatted string representation
*/
function format(val: unknown, options?: OptionsReceived): string;Usage Examples:
import { format } from "pretty-format";
// OR as default export
import format from "pretty-format";
// Basic usage
const result = format({ name: "Alice", age: 25 });
// With custom options
const formatted = format(
{ items: [1, 2, 3], nested: { deep: true } },
{
indent: 4,
maxDepth: 2,
min: false,
highlight: true
}
);
// Circular reference handling
const obj = { name: "test" };
obj.self = obj;
const circular = format(obj);
// Result includes [Circular] notationSame format function available as default export.
export default format;Complete configuration interface for customizing formatting behavior.
interface OptionsReceived {
/** Call toJSON method (if it exists) on objects */
callToJSON?: boolean;
/** Compare function used when sorting object keys, null can be used to skip over sorting */
compareKeys?: ((a: string, b: string) => number) | null;
/** Escape special characters in regular expressions */
escapeRegex?: boolean;
/** Escape special characters in strings */
escapeString?: boolean;
/** Highlight syntax with colors in terminal (some plugins) */
highlight?: boolean;
/** Spaces in each level of indentation */
indent?: number;
/** Levels to print in arrays, objects, elements, and so on */
maxDepth?: number;
/** Number of elements to print in arrays, sets, and so on */
maxWidth?: number;
/** Minimize added space: no indentation nor line breaks */
min?: boolean;
/** Plugins to serialize application-specific data types */
plugins?: Plugin[];
/** Print the prototype for plain objects and arrays */
printBasicPrototype?: boolean;
/** Include or omit the name of a function */
printFunctionName?: boolean;
/** Colors to highlight syntax in terminal */
theme?: Partial<Theme>;
}Pre-configured default options used when no custom options are provided.
const DEFAULT_OPTIONS: {
callToJSON: true;
compareKeys: undefined;
escapeRegex: false;
escapeString: true;
highlight: false;
indent: 2;
maxDepth: number; // Infinity
maxWidth: number; // Infinity
min: false;
plugins: Plugin[];
printBasicPrototype: true;
printFunctionName: true;
theme: {
comment: 'gray';
content: 'reset';
prop: 'yellow';
tag: 'cyan';
value: 'green';
};
};Usage Examples:
// Control indentation
format(data, { indent: 4 });
// Minimize output
format(data, { min: true });
// Limit depth
format(data, { maxDepth: 2 });
// Custom key sorting
format(data, {
compareKeys: (a, b) => a.localeCompare(b)
});
// Disable function names
format(fn, { printFunctionName: false });
// Enable terminal colors
format(data, {
highlight: true,
theme: { prop: 'blue', value: 'red' }
});Pretty Format handles all JavaScript value types:
true, false42, -0, Infinity, NaN123n"hello" (with optional escaping)Symbol(description)nullundefined[1, 2, 3] and typed arrays{ key: "value" } with property sortingMap { "key" => "value" }Set { 1, 2, 3 }WeakMap {}WeakSet {}Date { NaN }/pattern/flags[Error: message][Function name] or [Function][Circular]Arguments [...][Window] (safe handling in jsdom)Usage Examples:
// Complex object
const complex = {
num: 42,
big: 123n,
sym: Symbol('test'),
date: new Date(),
regex: /test/gi,
map: new Map([['key', 'value']]),
set: new Set([1, 2, 3]),
fn: function namedFn() {},
arrow: () => {}
};
const formatted = format(complex, {
printFunctionName: true,
indent: 2
});The internal configuration object used during formatting (read-only).
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;
}Install with Tessl CLI
npx tessl i tessl/npm-pretty-format