function inspect
inspect(
value: unknown,
depth?: number,
color?: boolean
): string

Renders a value the way the REPL prints it.

Deliberately close to util.inspect — quoted strings, [Function: name], Map(1) {…} — because the audience is someone who types into a prompt and expects a JavaScript prompt's answers. Composites that do not fit on one line are broken across several rather than truncated: a REPL that hides the tail of your object is a REPL you stop trusting.

color paints the leaves the way node and deno paint theirs — strings green, numbers and booleans yellow, functions cyan — and is off by default, so a piped session stays plain text that a script can compare. The caller decides: this function has no idea whether anything is attached to the other end, and the process that renders half these values is a browser.

import { inspect } from './inspect.ts';

inspect('hi'); // "'hi'" — quoted, so it cannot be confused with a bare identifier
inspect({ a: 1, b: [2, 3] }); // '{ a: 1, b: [ 2, 3 ] }'
inspect(new Map([['k', 1]])); // "Map(1) { 'k' => 1 }"
inspect(1, 2, true); // '\u001b[33m1\u001b[39m' — yellow, like every other JavaScript prompt

Parameters

value: unknown
optional
depth: number = 2
optional
color: boolean = false

Return Type

string

Usage

import { inspect } from "lib/repl/inspect.ts";