CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-pretty-format

Stringify any JavaScript value with customizable formatting, plugins, and terminal colors.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

core-formatting.mddocs/

Core Formatting

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.

Capabilities

Format Function

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] notation

Default Export

Same format function available as default export.

export default format;

Configuration Options

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>;
}

Default Options

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' }
});

Supported Value Types

Pretty Format handles all JavaScript value types:

Primitives

  • Boolean: true, false
  • Number: 42, -0, Infinity, NaN
  • BigInt: 123n
  • String: "hello" (with optional escaping)
  • Symbol: Symbol(description)
  • Null: null
  • Undefined: undefined

Objects and Collections

  • Arrays: [1, 2, 3] and typed arrays
  • Objects: { key: "value" } with property sorting
  • Maps: Map { "key" => "value" }
  • Sets: Set { 1, 2, 3 }
  • WeakMap: WeakMap {}
  • WeakSet: WeakSet {}
  • Date: ISO string or Date { NaN }
  • RegExp: /pattern/flags
  • Error: [Error: message]
  • Function: [Function name] or [Function]

Special Handling

  • Circular References: [Circular]
  • Arguments Object: Arguments [...]
  • ArrayBuffer/DataView: Byte-level formatting
  • Global Window: [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 
});

Internal Configuration

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

docs

built-in-plugins.md

core-formatting.md

index.md

plugin-system.md

tile.json